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文件

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
        xmlns:redis="http://www.springframework.org/schema/redis"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-34.0.xsd     
                            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd
                            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
                            http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
                            http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd">
                            
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1"></property>
        <property name="port" value="6379"></property>
        <property name="usePool" value="true"></property>
    </bean> 
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory"></property>
    </bean>     
    <bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    <bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
        <property name="delegate" ref="messageDelegateListener" /> <!--这里的messageDelegateListener在后面的文件中注解的-->
        <property name="serializer" ref="jdkSerializer" />
    </bean>  
    
    <redis:listener-container>
        <redis:listener ref="messageListener" method="handleMessage" serializer="jdkSerializer" topic="java"/>
    </redis:listener-container>
               
</beans>    
  • 接受消息回调的类

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<String, Object> 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);
        }
    }
}
posted @ 2018-05-03 16:05  游园拾忆  阅读(40)  评论(0编辑  收藏  举报