spring +redis 消息队列


jar:jedis-2.6.2.jar , spring+data+redis-1.4.2.jar
download:https://pan.baidu.com/s/1sJ-Nejk9j7OlHOavIgBDQQ
注:jedis和spring+data+redis版本不匹配的话,会报错。。。。


  • 配置下 spring支持 redis的xml文件



                            
    
        
        
        
     
    
        
         
    
    
         
        
      
    
    
        
    
               
    
  • 接受消息回调的类

package com.moensun.laipengtou.webapi.redis;

import java.io.Serializable;

import org.springframework.stereotype.Component;

@Component(value="messageDelegateListener")
public class ListenMessage {
    public void handleMessage(Serializable message){
        System.out.println(message);
    }
}
  • 发送消息的类

package com.moensun.laipengtou.webapi.redis;

import java.io.Serializable;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class SendMessage {
    @Resource(name="redisTemplate")
    private RedisTemplate redisTemplate;
    
    public void sendMessage(String channel, Serializable message) {
        redisTemplate.convertAndSend(channel, message);
    }
}
  • controller里调用

package com.moensun.laipengtou.webapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.moensun.laipengtou.webapi.redis.SendMessage;

@Controller
@RequestMapping(value="/test")
public class TestController {
    @Autowired SendMessage sendMessage;
    
    @RequestMapping(value="/redis")
    public void redis(){
        for (int i = 0; i <1000; i++) {
            sendMessage.sendMessage("java",i);
        }
    }
}

你可能感兴趣的:(spring +redis 消息队列)