jedis实现订阅发布-publish/subscribe (转 redis)

本文由larrylgq编写,转载请注明出处:http://blog.csdn.net/larrylgq/article/details/7395261

作者:吕桂强

邮箱:larry.lv.word@gmail.com

本例包括

jedis_demo:入口类

jedis_control:jedis控制器(jedis的连接池)

jedis_pub_sub_listener:订阅的监听器

singleton_agent:单例的代理类(连接池配置)

[java] view plaincopy

  1. package com.larry.jedis;?
  2. import redis.clients.jedis.Jedis;?
  3. /**
  4. * 入口类
  5. * @author 吕桂强
  6. * @email larry.lv.word@gmail.com
  7. * @version 创建时间:2012-3-28 下午12:12:41
  8. */
  9. public class jedis_demo {?
  10. ??? jedis_control redis_util = jedis_control.get_singleton();?
  11. public static void main(String[] args) {?
  12. ??????? jedis_demo jedis_demo = new jedis_demo();?
  13. new Thread(new Runnable(){?
  14. @Override
  15. public void run() {?
  16. ??????????????? jedis_control redis_util = jedis_control.get_singleton();?
  17. ??????????????? Jedis jedis = redis_util.get_connection();?
  18. ??????????????? jedis_pub_sub_listener pub_sub_listener = new jedis_pub_sub_listener();?
  19. // 可以订阅多个频道
  20. // 订阅得到信息在lister的onMessage(...)方法中进行处理
  21. // jedis.subscribe(listener, "news.share", "news.log");
  22. // jedis.subscribe(listener, new String[]{"news.share","news.log"});
  23. ??????????????? jedis.psubscribe(pub_sub_listener, new String[] { "news.share" });// 使用模式匹配的方式设置频道
  24. ??????????? }?
  25. ??????? }).start();?
  26. ??????? jedis_demo.publish();?
  27. ??? }?
  28. /**
  29. ???? * 发布
  30. ???? */
  31. public void publish() {?
  32. ??????? Jedis jedis = redis_util.get_connection();???
  33. ??????? jedis.publish("news.share", "ok");???
  34. ??????? jedis.publish("news.share", "hello word");??
  35. ??? }?
  36. }?

[java] view plaincopy

  1. package com.larry.jedis;?
  2. import redis.clients.jedis.Jedis;?
  3. /**
  4. * jedis控制器
  5. * @author 吕桂强
  6. * @email larry.lv.word@gmail.com
  7. * @version 创建时间:2012-3-28 下午12:03:40
  8. */
  9. public final class jedis_control {?
  10. //单例
  11. private static jedis_control _jedis_control;?
  12. public static jedis_control get_singleton(){?
  13. if(_jedis_control == null){?
  14. ??????????? _jedis_control = new jedis_control();?
  15. ??????? }?
  16. return _jedis_control;?
  17. ??? }?
  18. /**?????
  19. ???? * 获取连接实例??????
  20. ???? * @return jedis??????
  21. ???? */
  22. public Jedis get_connection() {?
  23. ??????? Jedis jedis = null;???????????
  24. try {???????????????
  25. ??????????? jedis = singleton_agent.get_jedispool().getResource();???????????
  26. ??????? } catch (Exception e) {???????????????
  27. ??????????? e.printStackTrace();???????????
  28. ??????? }???????????
  29. return jedis;???????
  30. ??? }????
  31. /**??????
  32. ???? * 释放数据库连接??????
  33. ???? * @param conn??????
  34. ???? */
  35. public void close_connection(Jedis jedis) {???????????
  36. if (null != jedis) {???????????????
  37. try {???????????????????
  38. ??????????????? singleton_agent.get_jedispool().returnResource(jedis);???????????????
  39. ??????????? } catch (Exception e) {?
  40. ??????????????????? e.printStackTrace();???????????????
  41. ??????????? }???????????
  42. ??????? }???????
  43. ??? }???
  44. }??

[java] view plaincopy

  1. package com.larry.jedis;?
  2. import redis.clients.jedis.JedisPubSub;?
  3. /**
  4. * 监听订阅事件
  5. * @author 吕桂强
  6. * @email larry.lv.word@gmail.com
  7. * @version 创建时间:2012-3-28 下午12:09:20
  8. */
  9. public class jedis_pub_sub_listener extends JedisPubSub {?
  10. // 取得订阅的消息后的处理
  11. public void onMessage(String channel, String message) {?
  12. ??????? System.out.println(channel + "=" + message);?
  13. ??? }?
  14. // 初始化订阅时候的处理
  15. public void onSubscribe(String channel, int subscribedChannels) {?
  16. ??????? System.out.println(channel + "=" + subscribedChannels);?
  17. ??? }?
  18. // 取消订阅时候的处理
  19. public void onUnsubscribe(String channel, int subscribedChannels) {?
  20. ??????? System.out.println(channel + "=" + subscribedChannels);?
  21. ??? }?
  22. // 初始化按表达式的方式订阅时候的处理
  23. public void onPSubscribe(String pattern, int subscribedChannels) {?
  24. ??????? System.out.println(pattern + "=" + subscribedChannels);?
  25. ??? }?
  26. // 取消按表达式的方式订阅时候的处理
  27. public void onPUnsubscribe(String pattern, int subscribedChannels) {?
  28. ??????? System.out.println(pattern + "=" + subscribedChannels);?
  29. ??? }?
  30. // 取得按表达式的方式订阅的消息后的处理
  31. public void onPMessage(String pattern, String channel, String message) {?
  32. ??????? System.out.println(pattern + "=" + channel + "=" + message);?
  33. ??? }?
  34. }?

[java] view plaincopy

  1. package com.larry.jedis;?
  2. import redis.clients.jedis.JedisPool;?
  3. import redis.clients.jedis.JedisPoolConfig;?
  4. /**
  5. * 所有单例的代理类
  6. * @author 吕桂强
  7. * @email larry.lv.word@gmail.com
  8. * @version 创建时间:2012-3-28 下午12:30:42
  9. */
  10. public class singleton_agent {?
  11. //****************单例一个连接池***************
  12. private static JedisPool jedispool = null;?
  13. ??? /**????????
  14. ???? * 获取连接池????????
  15. ???? * @return 数据源????????
  16. ???? */??????
  17. public static JedisPool get_jedispool() {?
  18. if(jedispool == null){?
  19. ??????????? JedisPoolConfig jedispool_config = new JedisPoolConfig();?
  20. ??????????? jedispool_config.maxActive = 20;?
  21. ??????????? jedispool_config.maxIdle = 0;?
  22. ??????????? jedispool_config.maxWait = 1000;?
  23. ??????????? jedispool_config.testOnBorrow = true;?
  24. ??????????? jedispool = new JedisPool(jedispool_config, "localhost", 6379);?
  25. ??????? }?
  26. return jedispool;??????
  27. ??? }?
  28. //end****************单例一个连接池***************
  29. }?

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

jedis实现监控-JedisMonitor

修改jedis_demo类:

[java] view plaincopy

  1. package com.larry.jedis;?
  2. import redis.clients.jedis.Jedis;?
  3. import redis.clients.jedis.JedisMonitor;?
  4. /**
  5. * 入口类
  6. *
  7. * @author 吕桂强
  8. * @email larry.lv.word@gmail.com
  9. * @version 创建时间:2012-3-28 下午12:12:41
  10. */
  11. public class jedis_demo {?
  12. ??? jedis_control redis_util = jedis_control.get_singleton();?
  13. public static void main(String[] args) {?
  14. ??????? jedis_demo jedis_demo = new jedis_demo();?
  15. //设置监控
  16. new Thread(new Runnable() {?
  17. @Override
  18. public void run() {?
  19. ??????????????? jedis_control redis_util = jedis_control.get_singleton();?
  20. ??????????????? Jedis jedis = redis_util.get_connection();?
  21. ??????????????? jedis.monitor(new JedisMonitor() {?
  22. @Override
  23. public void onCommand(String command) {?
  24. ??????????????????????? System.out.println("#monitor: " + command);?
  25. ??????????????????? }?
  26. ??????????????? });?
  27. ??????????? }?
  28. ??????? }).start();?
  29. new Thread(new Runnable() {?
  30. @Override
  31. public void run() {?
  32. ??????????????? jedis_control redis_util = jedis_control.get_singleton();?
  33. ??????????????? Jedis jedis = redis_util.get_connection();?
  34. ??????????????? jedis_pub_sub_listener pub_sub_listener = new jedis_pub_sub_listener();?
  35. // 可以订阅多个频道
  36. // 订阅得到信息在lister的onMessage(...)方法中进行处理
  37. // jedis.subscribe(listener, "news.share", "news.log");
  38. // jedis.subscribe(listener, new String[]{"news.share","news.log"});
  39. ??????????????? jedis.psubscribe(pub_sub_listener, new String[] { "news.share" });// 使用模式匹配的方式设置频道
  40. ??????????? }?
  41. ??????? }).start();?
  42. ??????? jedis_demo.publish();?
  43. ??? }?
  44. /**
  45. ???? * 发布
  46. ???? */
  47. public void publish() {?
  48. ??????? Jedis jedis = redis_util.get_connection();?
  49. ??????? jedis.publish("news.share", "ok");?
  50. ??????? jedis.publish("news.share", "hello word");?
  51. ??? }?
  52. }?

打印结果为:

#monitor: OK
#monitor: 1332918508.311191 "MONITOR"
#monitor: 1332918508.311921 "PUBLISH" "news.share" "hello word"
news.share=1
news.share=news.share=ok
news.share=news.share=hello word

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值