天天看点

Jedis的Publish/Subscribe功能的运用

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

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

Xml代码

Jedis的Publish/Subscribe功能的运用
  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代码

Jedis的Publish/Subscribe功能的运用
  1. <!-- pool配置 -->
  2. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  3. <property name="maxActive" value="20" />
  4. <property name="maxIdle" value="10" />
  5. <property name="maxWait" value="1000" />
  6. <property name="testOnBorrow" value="true" />
  7. </bean>
  8. <!-- jedis pool配置 -->
  9. <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
  10. <constructor-arg index="0" ref="jedisPoolConfig" />
  11. <constructor-arg index="1" value="10.8.9.237" />
  12. <constructor-arg index="2" value="6379" />
  13. </bean>
  14. <!-- 包装类 -->
  15. <bean id="redisUtil" class="demo.RedisUtil">
  16. <property name="jedisPool" ref="jedisPool" />
  17. </bean>

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

Java代码

Jedis的Publish/Subscribe功能的运用
  1. package demo;
  2. import redis.clients.jedis.Jedis;
  3. import redis.clients.jedis.JedisPool;
  4. public class RedisUtil {
  5. private JedisPool jedisPool;
  6. public Jedis getConnection() {
  7. Jedis jedis=null;
  8. try {
  9. jedis=jedisPool.getResource();
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. return jedis;
  14. }
  15. public void closeConnection(Jedis jedis) {
  16. if (null != jedis) {
  17. try {
  18. jedisPool.returnResource(jedis);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  24. public void setJedisPool(JedisPool JedisPool) {
  25. this.jedisPool = JedisPool;
  26. }
  27. public JedisPool getJedisPool() {
  28. return jedisPool;
  29. }
  30. }

4.编写Lister

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

Java代码

Jedis的Publish/Subscribe功能的运用
  1. package demo;
  2. import redis.clients.jedis.JedisPubSub;
  3. public class MyListener extends 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代码

Jedis的Publish/Subscribe功能的运用
  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代码

Jedis的Publish/Subscribe功能的运用
  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步的发布端的代码,订阅端就可以得到发布端发布的结果。控制台输出结果如下:

输出代码

Jedis的Publish/Subscribe功能的运用
  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/

继续阅读