Jedis的Publish/Subscribe功能的运用
来源:互联网 发布:合川广电网络收费标准 编辑:程序博客网 时间:2024/05/13 16:05
由于redis内置了发布/订阅功能,可以作为消息机制使用。所以这里主要使用Jedis的Publish/Subscribe功能。
1.添加Spring核心包,主要使用其最核心的IoC功能。如果使用Maven,配置如下:
Xml代码
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>3.1.1.RELEASE</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>3.1.1.RELEASE</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>3.1.1.RELEASE</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>3.1.1.RELEASE</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
2.使用Spring来配置Jedis连接池和RedisUtil的注入,写在bean-config.xml中。
Xml代码
- <!-- pool配置 -->
- <beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
- <propertyname="maxActive" value="20" />
- <propertyname="maxIdle" value="10" />
- <propertyname="maxWait" value="1000" />
- <propertyname="testOnBorrow"value="true" />
- </bean>
- <!-- jedis pool配置 -->
- <beanid="jedisPool" class="redis.clients.jedis.JedisPool">
- <constructor-argindex="0" ref="jedisPoolConfig" />
- <constructor-argindex="1" value="10.8.9.237" />
- <constructor-argindex="2" value="6379" />
- </bean>
- <!-- 包装类 -->
- <beanid="redisUtil" class="demo.RedisUtil">
- <propertyname="jedisPool" ref="jedisPool" />
- </bean>
3.编写RedisUtil,这里只是简单的包装,不做解释。
Java代码
- package demo;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- /**
- * 连接和使用redis资源的工具类
- * @author watson
- * @version 0.5
- */
- public class RedisUtil {
- /**
- * 数据源
- */
- private JedisPool jedisPool;
- /**
- * 获取数据库连接
- * @return conn
- */
- public Jedis getConnection() {
- Jedis jedis=null;
- try {
- jedis=jedisPool.getResource();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return jedis;
- }
- /**
- * 关闭数据库连接
- * @param conn
- */
- public void closeConnection(Jedis jedis) {
- if (null != jedis) {
- try {
- jedisPool.returnResource(jedis);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 设置连接池
- * @param 数据源
- */
- public void setJedisPool(JedisPool JedisPool) {
- this.jedisPool = JedisPool;
- }
- /**
- * 获取连接池
- * @return 数据源
- */
- public JedisPool getJedisPool() {
- return jedisPool;
- }
- }
4.编写Lister
要使用Jedis的Publish/Subscribe功能,必须编写对JedisPubSub的自己的实现,其中的函数的功能如下:
Java代码
- package demo;
- import redis.clients.jedis.JedisPubSub;
- public class MyListenerextends JedisPubSub {
- // 取得订阅的消息后的处理
- public void onMessage(String channel, String message) {
- System.out.println(channel + "=" + message);
- }
- // 初始化订阅时候的处理
- public void onSubscribe(String channel,int subscribedChannels) {
- // System.out.println(channel + "=" + subscribedChannels);
- }
- // 取消订阅时候的处理
- public void onUnsubscribe(String channel,int subscribedChannels) {
- // System.out.println(channel + "=" + subscribedChannels);
- }
- // 初始化按表达式的方式订阅时候的处理
- public void onPSubscribe(String pattern,int subscribedChannels) {
- // System.out.println(pattern + "=" + subscribedChannels);
- }
- // 取消按表达式的方式订阅时候的处理
- public void onPUnsubscribe(String pattern,int subscribedChannels) {
- // System.out.println(pattern + "=" + subscribedChannels);
- }
- // 取得按表达式的方式订阅的消息后的处理
- public void onPMessage(String pattern, String channel, String message) {
- System.out.println(pattern + "=" + channel + "=" + message);
- }
- }
5.实现订阅动能
Jedis有两种订阅模式:subsribe(一般模式设置频道)和psubsribe(使用模式匹配来设置频道)。不管是那种模式都可以设置个数不定的频道。订阅得到信息在将会lister的onMessage(...)方法或者onPMessage(...)中进行进行处理,这里我们只是做了简单的输出。
Java代码
- ApplicationContext ac = <span style="background-color: #ffffff;">new ClassPathXmlApplicationContext("beans-config.xml");</span>
- RedisUtil ru = (RedisUtil) ac.getBean("redisUtil");
- final Jedis jedis = ru.getConnection();
- final MyListener listener = new MyListener();
- //可以订阅多个频道
- //订阅得到信息在lister的onMessage(...)方法中进行处理
- //jedis.subscribe(listener, "foo", "watson");
- //也用数组的方式设置多个频道
- //jedis.subscribe(listener, new String[]{"hello_foo","hello_test"});
- //这里启动了订阅监听,线程将在这里被阻塞
- //订阅得到信息在lister的onPMessage(...)方法中进行处理
- jedis.psubscribe(listener, new String[]{"hello_*"});//使用模式匹配的方式设置频道
6.实现发布端代码
发布消息只用调用Jedis的publish(...)方法即可。
Java代码
- ApplicationContext ac = new ClassPathXmlApplicationContext("beans-config.xml");
- RedisUtil ru = (RedisUtil) ac.getBean("redisUtil");
- Jedis jedis = ru.getConnection();
- jedis.publish("hello_foo", "bar123");
- jedis.publish("hello_test", "hello watson");
7.分别运行上面的第5步的订阅端代码和第6步的发布端的代码,订阅端就可以得到发布端发布的结果。控制台输出结果如下:
输出代码
- hello_*=hello_foo=bar123
- hello_*=hello_test=hello watson
至此Jedis的Publish/Subscribe功能的使用基本展示完成,该使用方法稍作完善和修改后即可用于生产环境。
redis的安装参考:
- 官方:http://redis.io/topics/quickstart
- 中文:http://www.cnblogs.com/redcreen/archive/2011/02/15/1955523.html
参考:
- Jedis的高级使用:https://github.com/xetorthio/jedis/wiki/AdvancedUsage
- netty里集成spring注入jedis:http://yifangyou.blog.51cto.com/900206/628163
- Redis的Publish/Subscribe命令的使用:http://redis.io/topics/pubsub
- Redis的使用:http://redis.io/
0 0
- Jedis的Publish/Subscribe功能的运用
- Jedis的Publish/Subscribe功能的运用
- Jedis的Publish/Subscribe功能的运用
- Jedis的Publish/Subscribe功能的运用
- Jedis的Publish/Subscribe功能的运用
- Jedis的Publish/Subscribe功能的运用
- jedis的publish/subscribe
- Jedis的Publish/Subscribe
- Jedis实现Publish/Subscribe功能
- Jedis实现Publish/Subscribe功能
- ActiveMQ的publish/subscribe
- Jedis实现Publish/Subscribe功能(发布和订阅)
- 使用jedis实现Redis消息队列(MQ)的发布(publish)和消息监听(subscribe)
- 基于tornado的publish/subscribe模块
- Redis2.6 - publish/subscribe的BUG
- jedis实现订阅发布-publish/subscribe
- A Simple Jedis Publish / Subscribe Example
- jedis实现订阅发布-publish/subscribe
- ubuntu重启网络
- elasticsearch customScoreQuery 实例
- python web应用开发中获取各种参数的方式
- java设计模式(三)
- ios TableView那些事(三十一)UITableViewController ios8 删除时添加多个按钮
- Jedis的Publish/Subscribe功能的运用
- JAVA开发环境的安装及设定
- Redis的pipeline方式
- 为Hadoop集群选择合适的硬件配置
- Android小知识点—ProgressBar、SharedPreferences
- STL底层数据结构实现
- std::string用法总结
- Android内存泄漏分析及调试
- svn clear失败的解决方法