Spring Data Redis实现订阅与发布

Spring Data Redis实现订阅与发布

参考:
【Spring Data Redis实现一个订阅/发布系统】-- https://my.oschina.net/zhzhenqin/blog/85422
Spring Data Redis 】-- https://docs.spring.io/spring-data/redis/docs/1.7.8.RELEASE/reference/html/#redis:pubsub:subscribe

1、redis.propertie配置
Spring Data Redis实现订阅与发布_第1张图片
2、spring-pool.xml配置


    
    
        
            
                classpath:jdbc.properties
                classpath:redis.properties
            
        
    


    
    
        状态过滤器
        
        
        
        
        
        
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        
        
            
                
            
        
        
        
        
        
        
        
    
3、spring-redis.xml配置



    
        
        
        
    
    
    
    
        
        
        
        
        
    
    
    
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        
            
        
    
	
	
        
    

	
	  
	    
	    
	  
	
	
	
	  
	  
	    
	      
	      	
		        
		        
		          	
		        
		        
		        
	        
	      
	    
	  
	
	 
4、监听器接口MessageDelegate
public interface MessageDelegate {

    void handleMessage(String message);

    void handleMessage(Map message);

    void handleMessage(byte[] message);

    void handleMessage(Serializable message);

    void handleMessage(Serializable message, String channel);
	
}
5、监听器实现类MessageDelegateImpl
public class MessageDelegateImpl implements MessageDelegate {

	/* (non-Javadoc)
	 * @see com.csun.dryer.service.MessageDelegate#handleMessage(java.lang.String)
	 */
	@Override
	public void handleMessage(String message) {
		// TODO Auto-generated method stub
		System.out.println("handleMessage 1 : " + message);
	}

	/* (non-Javadoc)
	 * @see com.csun.dryer.service.MessageDelegate#handleMessage(java.util.Map)
	 */
	@Override
	public void handleMessage(Map message) {
		// TODO Auto-generated method stub
		System.out.println("handleMessage 2 : " + message.toString());
	}

	/* (non-Javadoc)
	 * @see com.csun.dryer.service.MessageDelegate#handleMessage(byte[])
	 */
	@Override
	public void handleMessage(byte[] message) {
		// TODO Auto-generated method stub
		System.out.println("handleMessage 3 : " + message.toString());
	}

	/* (non-Javadoc)
	 * @see com.csun.dryer.service.MessageDelegate#handleMessage(java.io.Serializable)
	 */
	@Override
	public void handleMessage(Serializable message) {
		// TODO Auto-generated method stub
		System.out.println("handleMessage 4 : " + message.toString());
	}

	/* (non-Javadoc)
	 * @see com.csun.dryer.service.MessageDelegate#handleMessage(java.io.Serializable, java.lang.String)
	 */
	@Override
	public void handleMessage(Serializable message, String channel) {
		// TODO Auto-generated method stub
		System.out.println("handleMessage 5 : " + ((String)message) + "channel: " + channel);
	}
	
}
6、订阅程序SubMain
public class SubMain {

	public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring-pool.xml","spring-redis.xml");
        while (true) { //这里是一个死循环,目的就是让进程不退出,用于接收发布的消息
            try {
                System.out.println("current time: " + new Date());
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
7、发布程序PubMain
public class PubMain {

	private RedisUtil redisUtil;
	
	@Before
	public void before() {
        
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring-pool.xml","spring-redis.xml");
        
		redisUtil = (RedisUtil)ac.getBean("redisUtil", RedisUtil.class);
	}
	
	@Test
    public void testPublishMessage() throws Exception {
        
		String msg = "Hello, Redis!";
		redisUtil.sendMessage("ChatRoom", msg);
    }
}
8、测试结果
先运行订阅程序 Spring Data Redis实现订阅与发布_第2张图片
运行发布程序
Spring Data Redis实现订阅与发布_第3张图片

你可能感兴趣的:(Redis,Java)