缓存队列系列二(订阅/发布实现)

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPubSub;

public class Test2 {

Jedis jedis = new Jedis("127.0.0.1");

public Jedis getJedis() {

return jedis;

}

public void publish() {

for (int i = 0; i < 1000; i++) {

jedis.publish("quepub", "test" + i);

try {

Thread.sleep(1000);

} catch (Exception e) {

e.printStackTrace();

}

}

}

public void lpush() {

for (int i = 0; i < 1000000; i++) {

try {

jedis.lpush("quemax", "test" + i);

} catch (Exception e) {

e.printStackTrace();

}

}

}

public String getQueMsg() {

String msg = "";

for (int i = 0; i < 1000000; i++) {

msg = jedis.rpop("quemax");

System.out.println(msg);

}

return msg;

}

public void lpushall() {

try {

for(int i=0;i<20;i++){

jedis.publish("quemq", "test");

}

} catch (Exception e) {

e.printStackTrace();

}

}

public void subscribe() {

try {

MyListener listener = new MyListener();

jedis.psubscribe(listener, new String[]{"*"});//使用模式匹配的方式设置频道

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

Test2 test = new Test2();

test.lpushall();

MyListener listener = new MyListener();

test.getJedis().psubscribe(listener, new String[]{"*"});//使用模式匹配的方式设置频道

}

}

你可能感兴趣的:(缓存队列系列二(订阅/发布实现))