redis发布订阅

==

==

一:redis命令行中的发布订阅

     1:SUBSCRIBE(订阅)

     2:PUBLISH(发布)

一次性订阅一个消息。

==

user1@ubuntu:~/redis/redis-3.2.3/src$ ./redis-cli 
127.0.0.1:6379> SUBSCRIBE subwang
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "subwang"
3) (integer) 1
1) "message"
2) "subwang"
3) "11111111111"


user1@ubuntu:~/redis/redis-3.2.3/src$ ./redis-cli 
127.0.0.1:6379> PUBLISH subwang 11111111111
(integer) 1
127.0.0.1:6379> 

==


一次性还可以订阅多个。

==

user1@ubuntu:~/redis/redis-3.2.3/src$ ./redis-cli 
127.0.0.1:6379> SUBSCRIBE wang11  wang22  wang33
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "wang11"
3) (integer) 1
1) "subscribe"
2) "wang22"
3) (integer) 2
1) "subscribe"
2) "wang33"
3) (integer) 3
1) "message"
2) "wang11"
3) "ccccccccc"
1) "message"
2) "wang22"
3) "dddddddddd"



user1@ubuntu:~/redis/redis-3.2.3/src$ ./redis-cli 
127.0.0.1:6379> PUBLISH subwang 11111111111
(integer) 1
127.0.0.1:6379> PUBLISH wang11 ccccccccc
(integer) 1
127.0.0.1:6379> PUBLISH wang22 dddddddddd
(integer) 1


==Java中

1 Publish

public class Publish {
	public static void main(String[] args) {
		Jedis jedis = new Jedis("192.168.40.129", 6379);
		jedis.auth("123456");
		jedis.publish("test_123", "bar123");
	}
}


2 Subscribe

==

public class Subscribe {
	public static void main(String[] args) {
		Jedis jedis = new Jedis("192.168.40.129", 6379);
		jedis.auth("123456");
		MyPubSub listener = new MyPubSub();
		jedis.psubscribe(listener, new String[] { "test_*" });
		System.out.println("Subscribe开始运行");
	}
}

class MyPubSub extends JedisPubSub {

	@Override
	public void onMessage(String channel, String message) {
		System.out.println("onMessage");
		super.onMessage(channel, message);
	}

	@Override
	public void onPMessage(String pattern, String channel, String message) {
		System.out.println("onPMessage");
		super.onPMessage(pattern, channel, message);
	}

	@Override
	public void onSubscribe(String channel, int subscribedChannels) {
		System.out.println("onSubscribe");
		super.onSubscribe(channel, subscribedChannels);
	}

	@Override
	public void onUnsubscribe(String channel, int subscribedChannels) {
		System.out.println("onUnsubscribe");
		super.onUnsubscribe(channel, subscribedChannels);
	}

	@Override
	public void onPUnsubscribe(String pattern, int subscribedChannels) {
		System.out.println("onPUnsubscribe");
		super.onPUnsubscribe(pattern, subscribedChannels);
	}

	@Override
	public void onPSubscribe(String pattern, int subscribedChannels) {
		System.out.println("onPSubscribe");
		super.onPSubscribe(pattern, subscribedChannels);
	}

	@Override
	public void onPong(String pattern) {
		System.out.println("onPong");
		super.onPong(pattern);
	}

	@Override
	public void unsubscribe() {
		System.out.println("unsubscribe");
		super.unsubscribe();
	}

	@Override
	public void unsubscribe(String... channels) {
		System.out.println("unsubscribe");
		super.unsubscribe(channels);
	}

	@Override
	public void subscribe(String... channels) {
		System.out.println("subscribe");
		super.subscribe(channels);
	}

	@Override
	public void psubscribe(String... patterns) {
		System.out.println("psubscribe");
		super.psubscribe(patterns);
	}

	@Override
	public void punsubscribe() {
		System.out.println("punsubscribe");
		super.punsubscribe();
	}

	@Override
	public void punsubscribe(String... patterns) {
		System.out.println("punsubscribe");
		super.punsubscribe(patterns);
	}

	@Override
	public void ping() {
		// TODO Auto-generated method stub
		System.out.println("ping");
		super.ping();
	}

	@Override
	public boolean isSubscribed() {
		// TODO Auto-generated method stub
		System.out.println("isSubscribed");
		return super.isSubscribed();
	}

	@Override
	public void proceedWithPatterns(Client client, String... patterns) {
		// TODO Auto-generated method stub
		System.out.println("proceedWithPatterns");
		super.proceedWithPatterns(client, patterns);
	}

	@Override
	public void proceed(Client client, String... channels) {
		System.out.println("proceed");
		super.proceed(client, channels);
	}

	@Override
	public int getSubscribedChannels() {
		System.out.println("getSubscribedChannels");
		return super.getSubscribedChannels();
	}

}


==

相关文章
相关标签/搜索