java使用jeids实现redis2.6的发布订阅

本文介绍了一个使用Java实现的Redis发布订阅功能的示例代码。包括如何建立连接、发送消息、订阅频道等操作。

代码:

    https://github.com/viakiba/redis

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

/**
 * @description: redis 发布/定阅 相关的演示
 * 					PSUBSCRIBE,PUBLISH,PUNSUBSCRIBE,SUBSCRIBE,UNSUBSCRIBE
 * @author viakiba
 * @date 2017年8月17日
 */
public class App25 {
	static interface RedisConfig {
		public final static String HOST = "127.0.0.1";
		public final static int PORT = 6379;
	}
	
	/**
	 * @description: 测试连接是否通过
	 * @author: viakiba
	 * @throws Exception
	 */
	@Test
	public void test1() throws Exception {
		//连接测试是否通过
		Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT);
		System.out.println(jedis.ping());//pong则通过
	}
	
	/**
	 * @description: 订阅
	 * 		PUNSUBSCRIBE/PSUBSCRIBE
	 * 		SUBSCRIBE/UNSUBSCRIBE
	 * @author: viakiba
	 * @throws Exception
	 */
	@Test
	public void test2() throws Exception {
		//连接测试是否通过
		Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT);
		JedisPubSub jedisPubSub = new JedisPubSub() {
			/**
			 * 系统发送一条订阅命令
			 */
			@Override
			public void onSubscribe(String str, int str1) {
				// TODO Auto-generated method stub
				System.out.println("onSubscribe"+str);
			}
			
			/**
			 * str1:本次消息  接受自的频道
			 * str2:本次消息的内容
			 */
			@Override
			public void onMessage(String str, String str1) {
				// TODO Auto-generated method stub
				System.out.println("onMessage"+str+str1);
			}
			
			/**
			 * 关闭此订阅时执行的命令
			 */
			@Override
			public void onUnsubscribe(String str, int str1) {
				// TODO Auto-generated method stub
				System.out.println("onUnsubscribe"+str);
			}
			
			/**
			 * 系统发送一条订阅命令(通配符版本)
			 */
			@Override
			public void onPSubscribe(String str, int str1) {
				// TODO Auto-generated method stub
				System.out.println("onPSubscribe"+str);
			}
			
			/**
			 * str:订阅的频道,支持通配符
			 * str1:本次消息  接受自的频道
			 * str2:本次消息的内容
			 */
			@Override
			public void onPMessage(String str, String str1, String str2) {
				// TODO Auto-generated method stub
				System.out.println("onPMessage"+str+str1+str2);
			}
			
			/**
			 * 关闭此订阅时执行的命令
			 */
			@Override
			public void onPUnsubscribe(String str, int str1) {
				// TODO Auto-generated method stub
				System.out.println("onPUnsubscribe"+str+str1);
			}
		};
		//一个线程只能订阅一个,会被阻塞
//		jedis.subscribe(jedisPubSub, "he");
		jedis.psubscribe(jedisPubSub, "hel*");
	}
	
	/**
	 * @description: PUBLISH
	 * 		发布一条消息
	 * @author: viakiba
	 * @throws Exception
	 */
	@Test
	public void test3() throws Exception {
		Jedis jedis = new Jedis(RedisConfig.HOST,RedisConfig.PORT);
//		jedis.publish("demo", "haha");
	}
	
	/**
	 * PUBSUB:是一个查看订阅与发布系统状态的内省命令, 它由数个不同格式的子命令组成, 以下将分别对这些子命令进行介绍。
	 */
}

 

转载于:https://my.oschina.net/viakiba/blog/1512502

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值