1. 程式人生 > >java+redis+spring mvc實現釋出訂閱(不同專案間)

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

}