zoukankan      html  css  js  c++  java
  • SpringBoot Redis 订阅发布

    一  配置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;
        }
    }

  • 相关阅读:
    Highcharts 环境配置
    Highcharts 配置语法
    tsql语句分析工具 转
    C#编码规范 转 http://www.cnblogs.com/wulinfeng/archive/2012/08/31/2664720.html
    改善C#程序,提高程序运行效率的50种方法
    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
    配置handler vs2013 iis8.0
    14、正则表达式
    13、cookie
    11、事件(上)
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/11212310.html
Copyright © 2011-2022 走看看