spring-data-redis 怎么监听消息队列有消息来了
1个回答
展开全部
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> <!-- --> <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); } } }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询