一  配置application.yml

spring:
redis:
jedis:
pool:
max-active: 10
min-idle: 5
max-idle: 10
max-wait: 2000
port: 6379
host: 192.168.1.88
timeout: 1000

二  实现监听

package com.example.demo.common;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component; /**
* @author Tyler
* @date 2019/7/11
*/ @Component
public class RedisMessageListener implements MessageListener { @Override
public void onMessage(Message message, byte[] bytes) {
String body=new String(message.getBody());
String topic=new String(bytes);
System.out.println(body);
System.out.println(topic);
}
}

三  注入spring

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Repository; @SpringBootApplication
@MapperScan(basePackages ="com.example.demo.common", annotationClass = Repository.class)
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} @Autowired
private RedisConnectionFactory connectionFactory;
@Autowired
private MessageListener redisMsgListener; private ThreadPoolTaskScheduler taskScheduler; @Bean
public ThreadPoolTaskScheduler initTaskScheduler()
{
if(taskScheduler!=null)
{
return taskScheduler;
}
taskScheduler=new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(20);
return taskScheduler;
} @Bean
public RedisMessageListenerContainer initRedisContainer()
{
RedisMessageListenerContainer container=new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setTaskExecutor(initTaskScheduler());
Topic topic=new ChannelTopic("topic1");
container.addMessageListener(redisMsgListener,topic);
return container;
} }

四  结果:

1  接收exe消息

2 接收controller消息

controller:

@Controller
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate; @RequestMapping("/stringAndHash")
@ResponseBody
public Map<String,Object> testStringAndHash()
{
stringRedisTemplate.convertAndSend("topic1","hello"); Map<String,Object> map=new HashMap<>();
map.put("success",true);
return map;
}
}

最新文章

  1. sso demo 取消https (cas)
  2. PHP功能齐全的发送邮件类
  3. PowerDesigner连接SqlServer数据库
  4. 销售 &gt;&gt; 当今社会生产力最大的源泉为 &gt;&gt;自助服务 与推销员随之消失
  5. PHP 类和继承
  6. HDU2088JAVA2
  7. ASP.Net MVC4排序检索分页的实现
  8. OpenStack Neutron DVR L2 Agent的初步解析 (一)
  9. 2014.12.13 ASP.NET文件上传
  10. php汉字生成首字母
  11. 无法启动outlook mapi
  12. AngularJS 动画总结
  13. IO 调优
  14. 《github一天一道算法题》:并归排序
  15. Linux显示历史记录
  16. WPF 自定义DataGrid控件样式
  17. bzoj 1854: [Scoi2010]游戏 (并查集||二分图最大匹配)
  18. 前端 ---client、offset、scroll系列
  19. 2018acm-icpc宁夏邀请赛后记
  20. shell编程变量赋值

热门文章

  1. POJ 3805 Separate Points (判断凸包相交)
  2. 依赖Anaconda环境安装TensorFlow库,避免采坑
  3. Java高级进阶:自定义ClassLoader
  4. Python的一些列表方法
  5. android sdk 下载 最新版。。4.l
  6. Summary 报告
  7. netstat -pa --unix &gt;&gt;test.txt
  8. svg圆环缓冲动画
  9. SolidWorks直线命令快捷转换为圆弧命令
  10. @ControllerAdvice + @ExceptionHandler全局处理Controller层异常