博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring data redis pubsub demo
阅读量:6700 次
发布时间:2019-06-25

本文共 3389 字,大约阅读时间需要 11 分钟。

hot3.png

创建监听器

package com.ssyouxia.listener;/** * Created by lianfangfang on 2019/2/28. */import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import java.util.concurrent.atomic.AtomicInteger;public class RedisMessageListener implements MessageListener {    private AtomicInteger count = new AtomicInteger( 0 );    @Override    public void onMessage(Message message, byte[] pattern) {        count.incrementAndGet();    }    public int getCount() {        return count.get();    }}

加载监听器

package com.ssyouxia.config;import com.ssyouxia.listener.RedisMessageListener;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;import org.springframework.data.redis.connection.MessageListener;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.listener.ChannelTopic;import org.springframework.data.redis.listener.RedisMessageListenerContainer;/** * Created by lianfangfang on 2019/2/28. */@Configuration@Import(SpringDataRedisConfig.class)public class RedisPubsubConfiguration {    @Bean    @Autowired    public RedisMessageListenerContainer container(            final JedisConnectionFactory connectionFactory) {        final RedisMessageListenerContainer container =                new RedisMessageListenerContainer();        container.setConnectionFactory(connectionFactory);        container.addMessageListener(listener(), new ChannelTopic("test-channel"));        return container;    }    @Bean    public MessageListener listener() {        return new RedisMessageListener();    }}

测试类

package com.ssyouxia.config;import com.ssyouxia.listener.RedisMessageListener;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.concurrent.Callable;import static com.jayway.awaitility.Awaitility.await;import static java.util.concurrent.TimeUnit.SECONDS;import static org.hamcrest.CoreMatchers.equalTo;import static org.hamcrest.MatcherAssert.assertThat;/** * Created by lianfangfang on 2019/2/28. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = RedisPubsubConfiguration.class)public class RedisPublishSubscriberTestCase {    @Autowired    private RedisTemplate
template; @Autowired private RedisMessageListener listener; @Test public void testPublishSubscribe() { assertThat(listener.getCount(), equalTo(0)); template.convertAndSend("test-channel", "Test Message 1!"); template.convertAndSend("test-channel", "Test Message 2!"); template.convertAndSend("test-channel", "Test Message 3!"); await().atMost(1, SECONDS).until( new Callable
() { @Override public Integer call() throws Exception { return listener.getCount(); } }, equalTo(3) ); }}

代码下载地址

转载于:https://my.oschina.net/u/3238650/blog/3016397

你可能感兴趣的文章
JAVA四种遍历Map的方法
查看>>
Mongo DB 简单搭建和部署
查看>>
详解Nagios配置文件的逻辑关系
查看>>
Ubuntu安装php7.2
查看>>
好程序员Web前端分享无法忽视的JavaScript技巧
查看>>
神奇的pdfkit工具——将字符串保存为pdf文件
查看>>
Oracle的resetlogs机制浅析
查看>>
AIX samba服务器配置
查看>>
Think in AngularJS:对比jQuery和AngularJS的不同思维模式
查看>>
redhat oracle 开机自启动
查看>>
安装iproute2
查看>>
删除rz上传失败乱码的文件
查看>>
zabbix4
查看>>
10种顶级javascript框架比较-The Top 10 Javascript MVC Frameworks
查看>>
HAproxy + Keepalive实现LDAP代理服务
查看>>
plone进行 用户和权限管理
查看>>
利用ACS来实现AAA服务
查看>>
VMware Workstation 8下Ubuntu 13.04中安装VMware Tools出错
查看>>
Tokyo Tyrant安装和配置
查看>>
php调试
查看>>