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

项目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);

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值