Jedis的Publish/Subscribe功能的运用

来源:互联网 发布:合川广电网络收费标准 编辑:程序博客网 时间:2024/05/13 16:05

由于redis内置了发布/订阅功能,可以作为消息机制使用。所以这里主要使用Jedis的Publish/Subscribe功能。

1.添加Spring核心包,主要使用其最核心的IoC功能。如果使用Maven,配置如下:

Xml代码 收藏代码
  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-context</artifactId>
  4. <version>3.1.1.RELEASE</version>
  5. <type>jar</type>
  6. <scope>compile</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context-support</artifactId>
  11. <version>3.1.1.RELEASE</version>
  12. <type>jar</type>
  13. <scope>compile</scope>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-beans</artifactId>
  18. <version>3.1.1.RELEASE</version>
  19. <type>jar</type>
  20. <scope>compile</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-core</artifactId>
  25. <version>3.1.1.RELEASE</version>
  26. <type>jar</type>
  27. <scope>compile</scope>
  28. </dependency>

2.使用Spring来配置Jedis连接池和RedisUtil的注入,写在bean-config.xml中。

Xml代码 收藏代码
  1. <!-- pool配置 -->
  2. <beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
  3. <propertyname="maxActive" value="20" />
  4. <propertyname="maxIdle" value="10" />
  5. <propertyname="maxWait" value="1000" />
  6. <propertyname="testOnBorrow"value="true" />
  7. </bean>
  8. <!-- jedis pool配置 -->
  9. <beanid="jedisPool" class="redis.clients.jedis.JedisPool">
  10. <constructor-argindex="0" ref="jedisPoolConfig" />
  11. <constructor-argindex="1" value="10.8.9.237" />
  12. <constructor-argindex="2" value="6379" />
  13. </bean>
  14. <!-- 包装类 -->
  15. <beanid="redisUtil" class="demo.RedisUtil">
  16. <propertyname="jedisPool" ref="jedisPool" />
  17. </bean>

3.编写RedisUtil,这里只是简单的包装,不做解释。

Java代码 收藏代码
  1. package demo;

  2. import redis.clients.jedis.Jedis;
  3. import redis.clients.jedis.JedisPool;


  4. /**
  5. * 连接和使用redis资源的工具类
  6. * @author watson
  7. * @version 0.5
  8. */
  9. public class RedisUtil {

  10. /**
  11. * 数据源
  12. */
  13. private JedisPool jedisPool;

  14. /**
  15. * 获取数据库连接
  16. * @return conn
  17. */
  18. public Jedis getConnection() {
  19. Jedis jedis=null;
  20. try {
  21. jedis=jedisPool.getResource();
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. return jedis;
  26. }

  27. /**
  28. * 关闭数据库连接
  29. * @param conn
  30. */
  31. public void closeConnection(Jedis jedis) {
  32. if (null != jedis) {
  33. try {
  34. jedisPool.returnResource(jedis);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }

  40. /**
  41. * 设置连接池
  42. * @param 数据源
  43. */
  44. public void setJedisPool(JedisPool JedisPool) {
  45. this.jedisPool = JedisPool;
  46. }

  47. /**
  48. * 获取连接池
  49. * @return 数据源
  50. */
  51. public JedisPool getJedisPool() {
  52. return jedisPool;
  53. }
  54. }

4.编写Lister

要使用Jedis的Publish/Subscribe功能,必须编写对JedisPubSub的自己的实现,其中的函数的功能如下:

Java代码 收藏代码
  1. package demo;

  2. import redis.clients.jedis.JedisPubSub;

  3. public class MyListenerextends JedisPubSub {
  4. // 取得订阅的消息后的处理
  5. public void onMessage(String channel, String message) {
  6. System.out.println(channel + "=" + message);
  7. }

  8. // 初始化订阅时候的处理
  9. public void onSubscribe(String channel,int subscribedChannels) {
  10. // System.out.println(channel + "=" + subscribedChannels);
  11. }

  12. // 取消订阅时候的处理
  13. public void onUnsubscribe(String channel,int subscribedChannels) {
  14. // System.out.println(channel + "=" + subscribedChannels);
  15. }

  16. // 初始化按表达式的方式订阅时候的处理
  17. public void onPSubscribe(String pattern,int subscribedChannels) {
  18. // System.out.println(pattern + "=" + subscribedChannels);
  19. }

  20. // 取消按表达式的方式订阅时候的处理
  21. public void onPUnsubscribe(String pattern,int subscribedChannels) {
  22. // System.out.println(pattern + "=" + subscribedChannels);
  23. }

  24. // 取得按表达式的方式订阅的消息后的处理
  25. public void onPMessage(String pattern, String channel, String message) {
  26. System.out.println(pattern + "=" + channel + "=" + message);
  27. }
  28. }

5.实现订阅动能

Jedis有两种订阅模式:subsribe(一般模式设置频道)和psubsribe(使用模式匹配来设置频道)。不管是那种模式都可以设置个数不定的频道。订阅得到信息在将会lister的onMessage(...)方法或者onPMessage(...)中进行进行处理,这里我们只是做了简单的输出。

Java代码 收藏代码
  1. ApplicationContext ac = <span style="background-color: #ffffff;">new ClassPathXmlApplicationContext("beans-config.xml");</span>
  2. RedisUtil ru = (RedisUtil) ac.getBean("redisUtil");
  3. final Jedis jedis = ru.getConnection();
  4. final MyListener listener = new MyListener();
  5. //可以订阅多个频道
  6. //订阅得到信息在lister的onMessage(...)方法中进行处理
  7. //jedis.subscribe(listener, "foo", "watson");

  8. //也用数组的方式设置多个频道
  9. //jedis.subscribe(listener, new String[]{"hello_foo","hello_test"});

  10. //这里启动了订阅监听,线程将在这里被阻塞
  11. //订阅得到信息在lister的onPMessage(...)方法中进行处理
  12. jedis.psubscribe(listener, new String[]{"hello_*"});//使用模式匹配的方式设置频道

6.实现发布端代码

发布消息只用调用Jedis的publish(...)方法即可。

Java代码 收藏代码
  1. ApplicationContext ac = new ClassPathXmlApplicationContext("beans-config.xml");
  2. RedisUtil ru = (RedisUtil) ac.getBean("redisUtil");
  3. Jedis jedis = ru.getConnection();
  4. jedis.publish("hello_foo", "bar123");
  5. jedis.publish("hello_test", "hello watson");

7.分别运行上面的第5步的订阅端代码和第6步的发布端的代码,订阅端就可以得到发布端发布的结果。控制台输出结果如下:

输出代码 收藏代码
  1. hello_*=hello_foo=bar123
  2. hello_*=hello_test=hello watson

至此Jedis的Publish/Subscribe功能的使用基本展示完成,该使用方法稍作完善和修改后即可用于生产环境。

redis的安装参考:

  1. 官方:http://redis.io/topics/quickstart
  2. 中文:http://www.cnblogs.com/redcreen/archive/2011/02/15/1955523.html

参考:

  1. Jedis的高级使用:https://github.com/xetorthio/jedis/wiki/AdvancedUsage
  2. netty里集成spring注入jedis:http://yifangyou.blog.51cto.com/900206/628163
  3. Redis的Publish/Subscribe命令的使用:http://redis.io/topics/pubsub
  4. Redis的使用:http://redis.io/
0 0
原创粉丝点击