您的位置:首页 > 数据库 > Redis

java+redis+spring mvc实现发布订阅(不同项目间)

2017-01-19 16:20 786 查看
项目1:利用redis做消息队列发布消息

项目2:利用redis订阅项目1发布的消息

项目1(发布):

properties配置文件中redis配置:

redis.hostName=192.168.1.1

redis.port=6379

redis.timeout=15000

redis.usePool=true

redis.maxIdle=6

redis.minEvictableIdleTimeMillis=300000

redis.numTestsPerEvictionRun=3

redis.timeBetweenEvictionRunsMillis=60000

spring配置文件添加redis配置(只列出了redis相关配置):

xmlns:redis="http://www.springframework.org/schema/redis"
http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd

<util:properties id="config" location="classpath:../conf/config.properties"/>

<!-- redis配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 

        <property name="maxActive" value="3000"/> 

       <property name="maxIdle" value="100"/> 

        <property name="maxWait" value="10000"/> 

        <property name="testOnBorrow" value="true"/> 
</bean> 
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  

<bean  id='jedisConnectionFactory'  

        class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory' >
  <property name="hostName" value="${redis.hostName}"></property>
  <property name="port" value="${redis.port}"></property>
  <property name="usePool" value="${redis.usePool}"></property>
  <property name="poolConfig" ref="jedisPoolConfig"></property> 

      </bean>

       

     <bean id='redisTemplate' class='org.springframework.data.redis.core.RedisTemplate'>  

         <property name="connectionFactory" ref="jedisConnectionFactory"></property>

        <!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 -->    

        <property name="keySerializer">    

           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    

        </property>    

        <property name="hashKeySerializer">    

           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    

        </property>  

        <property name="valueSerializer">  

            <bean  

                class="org.springframework.data.redis.serializer.StringRedisSerializer" />  

        </property>

     </bean>

RedisDao接口:

import java.util.Map;

import org.springframework.data.redis.core.ValueOperations;

public interface RedisDao {
/*
 * 设置频道
 */
public static final String TQCHANNEL = "channel_message";
public void sendMessage(String channel, String message);

}

RedisDaoImpl实现类:

public class RedisDaoImpl implements RedisDao {
@Autowired
public RedisTemplate<String, Object> redisTemplate;
@Override
public void sendMessage(String channel, String message) {
System.out.println("开始发布消息。。。");
redisTemplate.convertAndSend(channel, message); 
System.out.println("发布成功!");
}

}

程序中调用RedisDaoImpl发布消息:

redisDao.sendMessage(“channel_message”, "要发布的字符串");

项目2(订阅):

properties配置文件中redis配置:

redis.hostName=192.168.1.1

redis.port=6379

redis.timeout=15000

redis.usePool=true

redis.maxIdle=6

redis.minEvictableIdleTimeMillis=300000

redis.numTestsPerEvictionRun=3

redis.timeBetweenEvictionRunsMillis=60000

spring配置文件添加redis配置(只列出了redis相关配置):

xmlns:redis="http://www.springframework.org/schema/redis"
http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd

<util:properties id="config" location="classpath:../conf/config.properties"/>

<!-- redis配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 

        <property name="maxActive" value="3000"/> 

       <property name="maxIdle" value="100"/> 

        <property name="maxWait" value="10000"/> 

        <property name="testOnBorrow" value="true"/> 
</bean> 
<bean  

        id='jedisConnectionFactory'  

        class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory' >
  <property name="hostName" value="${redis.hostName}"></property>
  <property name="port" value="${redis.port}"></property>
  <property name="usePool" value="${redis.usePool}"></property>
  <property name="poolConfig" ref="jedisPoolConfig"></property> 

      </bean>

       

     <bean id='redisTemplate' class='org.springframework.data.redis.core.RedisTemplate'>  

         <property name="connectionFactory" ref="jedisConnectionFactory"></property>

        <!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 -->    

        <property name="keySerializer">    

           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    

        </property>    

        <property name="hashKeySerializer">    

           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    

        </property>  

        <property name="valueSerializer">  

            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  

        </property>

        <property name="hashValueSerializer">  

         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  

    </property>

     </bean>

     

     <!-- redis发布订阅配置 -->

   <bean id="serialization" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  

   

  <!-- 消息监听适配器  delegate属性指定真正的目标处理器-->  

    <bean id="smsMessageListener"

        class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">

        <property name="delegate" ref="tQMessageDelegateListener" />

       <!--  <property name="serializer" ref="serialization" />   -->

    </bean>

    <bean id="tQMessageDelegateListener" class="com.cn.listener.TQMessageDelegateListener"/>

    <!-- 消息监听适配器对应的监听容器 -->

     <redis:listener-container  connection-factory="jedisConnectionFactory">

        <redis:listener ref="smsMessageListener" method="handleMessage"

            topic="tq_message" /> 

    </redis:listener-container>

监听器TQMessageDelegateListener代码:

public class TQMessageDelegateListener  {
@Autowired
protected JdbcTemplate jdbcTemplate ;
@Autowired
private JdbcTemplate jdbc;
//private UserController user = new UserController();

    public void  handleMessage(String message){

System.out.println("监听到的消息为:"+message);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis spring 发布