zoukankan      html  css  js  c++  java
  • spring-data-redis 订阅发布实例

    spring-data-redis 怎么监听消息队列有消息来了

    pom.xml中添加如下配置
    添加版本配置
    <properties>
      <jedis.version>2.8.1</jedis.version>
      <spring-data-redis.version>1.7.2.RELEASE</spring-data-redis.version>
      <commons-pool2.version>2.2</commons-pool2.version>
    </properties>
        <!-- jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-pool2</artifactId>
          <version>${commons-pool2.version}</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
        <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>${spring-data-redis.version}</version>
        </dependency>
    
    properties文件中添加如下配置
    #redis配置
    redis.host=192.168.1.150
    redis.port=6379
    redis.password=redis
    redis.timeout=2000
    redis.max_total=100
    redis.max_idle=20
    redis.min_idle=5
    redis.test_on_borrow=true
    redis.test_on_return=true
    
    applicationContext.xml中添加如下配置:
    <!-- Jedis 连接池配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.max_idle}" />
        <property name="maxTotal" value="${redis.max_total}"/>
        <property name="minIdle" value="${redis.min_idle}"/>
        <property name="testOnBorrow" value="${redis.test_on_borrow}" />
        <property name="testOnReturn" value="${redis.test_on_return}"/>
    </bean>
    
    <!-- Jedis ConnectionFactory 数据库连接配置,注意id名称必须为redisConnectionFactory-->
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.password}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>
    
    定义消息发送者(生产者):
    @Component
    public class SendMessage {
    
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
        public void sendMessage(String channel, Serializable message) {
            redisTemplate.convertAndSend(channel, message);
        }
    }
    
    定义消息处理者(消费者):
    public class ListenMessage {
        public void handleMessage(Serializable message){
            System.out.println(message);
        }
    }
    
    调用:/queue/redis
    @Controller
    @RequestMapping(value = "/queue")
    public class QueueController {
    
        @Autowired
        SendMessage sendMessage;
    
        @RequestMapping(value="/redis")
        public void redis(){
            for (int i = 0; i <1000; i++) {
                sendMessage.sendMessage("java",i);
            }
        }
    }
    
  • 相关阅读:
    把影响集中到一个点
    How to avoid Over-fitting using Regularization?
    适定性问题
    Numerical Differentiation 数值微分
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
    What is the difference between iterations and epochs in Convolution neural networks?
    Every norm is a convex function
    Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
    perl 类里的函数调用其他类的函数
  • 原文地址:https://www.cnblogs.com/mjzhang/p/5980065.html
Copyright © 2011-2022 走看看