springboot干货——(十三【三】)整合redis实现消息队列

redis除了作为一般的nosql数据存储之外,还能实现简单的消息队列的功能。

存储功能可以参考我的上篇博客:springboot整合redis

 

项目结构

springboot干货——(十三【三】)整合redis实现消息队列_第1张图片

 

1.pom.xml如下

 



	4.0.0

	com.gwd
	springboot-redis-provider_reciver
	0.0.1-SNAPSHOT
	jar

	springboot-redis-provider_reciver
	Demo project for springboot-provider_reciver

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-data-redis
		
		
		    org.springframework.boot
		    spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


2.properties文件

 

 

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.30.103
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000

server.port=8088

 

 

 

 

 

3.Receiver.java

 

package com.gwd.config;

import java.util.concurrent.CountDownLatch;

import org.springframework.beans.factory.annotation.Autowired;

/** 
* @FileName Receiver.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年3月16日 下午4:44:08 
* 修改历史:
* 时间           作者          版本        描述
*====================================================  
*
*/
public class Receiver {
	private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }
}

 

 

 

 

 

4.启动类

 

package com.gwd;

import java.util.concurrent.CountDownLatch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import com.gwd.config.Receiver;

@SpringBootApplication
public class SpringbootRedisReciverApplication {

	@Bean
	Receiver receiver(CountDownLatch latch) {
	return new Receiver(latch);
	}
	 
	//必要的redis消息队列连接工厂
	@Bean
	CountDownLatch latch() {
	return new CountDownLatch(1);
	}
	 
	//redis模板
	@Bean
	StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
	return new StringRedisTemplate(connectionFactory);
	}
	 
	//注入消息监听器容器
	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
	RedisMessageListenerContainer container = new RedisMessageListenerContainer();
	container.setConnectionFactory(connectionFactory);
	container.addMessageListener(listenerAdapter, new PatternTopic("msg"));
	return container;
	}
	 
	//注入消息监听器容器
	@Bean
	MessageListenerAdapter listenerAdapter(Receiver receiver) {
	return new MessageListenerAdapter(receiver, "receiveMessage");
	}
	
	
	public static void main(String[] args) {
		SpringApplication.run(SpringbootRedisReciverApplication.class, args);
	}
}

 

 

 

 

 

5.controller

 

package com.gwd.controller;

import java.util.concurrent.CountDownLatch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/** 
* @FileName TestController.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年3月16日 下午5:47:20 
* 修改历史:
* 时间           作者          版本        描述
*====================================================  
*
*/
@RestController
public class TestController {
	@Autowired
	CountDownLatch latch;
	@Autowired
	StringRedisTemplate template;
	
	@RequestMapping("/testProvider")
	@ResponseBody
	public String testProvider() {
		System.out.println("我要发送消息咯...");
		template.convertAndSend("msg", "欢迎使用redis的消息队列!");
		try {
		//发送消息连接等待中
			System.out.println("消息正在发送...");
		latch.await();
		} catch (InterruptedException e) {
			System.out.println("消息发送失败...");
		}
		return null;
	}
}


6.测试结果:

 

springboot干货——(十三【三】)整合redis实现消息队列_第2张图片

 

 

注意:配置文件中设置连接超时时间spring.redis.timeout不要为0,否则会报:

org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: Command timed out

你可能感兴趣的:(Spring,Boot,Springboot干货系列)