前言:发现redis的list可以解决消息队列问题,(先进先出)
使用lpush插入数据,使用brpop取出数据。。。
pom文件引入
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.3</version>
</dependency>
xml
<?xml version="1.0" encoding="gbk"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大分配的对象数 -->
<property name="maxTotal" value="1024" />
<!-- 最大能够保持idel状态的对象数 -->
<property name="maxIdle" value="100" />
<!-- 当池内没有返回对象时,最大等待时间(毫秒) -->
<property name="maxWaitMillis" value="60000" />
<!-- 当调用borrow Object方法时,是否进行有效性检查 -->
<property name="testOnBorrow" value="true" />
<!-- 当调用return Object方法时,是否进行有效性检查 -->
<property name="testOnReturn" value="true" />
</bean>
<bean id="jedisshardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.ip}" />
<constructor-arg index="1" value="${redis.port}" />
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedisshardInfo" />
</list>
</constructor-arg>
</bean>
<bean id="jedisClient" class="redis.clients.jedis.Jedis">
<constructor-arg ref="jedisshardInfo"></constructor-arg>
</bean>
</beans>
实体
public class EmailSend {
private String to;
private String content;
public EmailSend(){
}
public EmailSend(String to,String content){
this.to = to;
this.content = content;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return this.to+"---"+this.content;
}
}
封装redis工具类,这里只封装了lpush和brpop
/**
* Redis工具类
* @author xuchangcheng
* 2019年9月19日
*/
public class RedisUtil {
private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
private static ShardedJedisPool shardedJedisPool = null;
static {
if(shardedJedisPool == null){
shardedJedisPool = (ShardedJedisPool)WebContextListener.getBean(ShardedJedisPool.class);
}
}
/**
* 从队列的左边入队一个或多个元素
* @param key
* @param times 超时时间(单位秒),0为不超时
* @param strings
* @return
*/
public static Long lpush(String key, int times, String str) {
Long result = null;
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
result = shardedJedis.lpush(key, str);
if(times > 0){
shardedJedis.expire(key, times);
}
} catch (RuntimeException e) {
logger.error(e.getMessage(), e);
} finally {
if (shardedJedis != null) {
shardedJedisPool.returnResource(shardedJedis);
}
}
return result;
}
/**
* 取出数据
* @param key
* @return list
*/
public static List<String> brpop(String key){
List<String> list = null;
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
list = shardedJedis.brpop(30,key);
} catch (RuntimeException e) {
logger.error(e.getMessage(), e);
} finally {
if (shardedJedis != null) {
shardedJedisPool.returnResource(shardedJedis);
}
}
return list;
}
}
实现
Gson gson = new Gson();
/**
* 插入list
* @return
*/
@RequestMapping(value="push", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public ResponseData pushList(){
ResponseData responseData=new ResponseData();
try{
RedisUtil.lpush("email1", 60,gson.toJson(new EmailSend("xcc01", "redis01")));
RedisUtil.lpush("email1", 60,gson.toJson(new EmailSend("xcc02", "redis03")));
RedisUtil.lpush("email1", 60,gson.toJson(new EmailSend("xcc03", "redis04")));
responseData.setStatus(0);
responseData.setMsg("OK");
} catch (Exception e) {
e.printStackTrace();
responseData.setStatus(1);
responseData.setMsg("系统异常");
}
return responseData;
}
/**
* getlist
* @return
*/
@RequestMapping(value="brpop", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public ResponseData brpop(){
ResponseData responseData=new ResponseData();
try{
Thread t = new Thread(){
public void run(){
while(true){
List<String> brpop = RedisUtil.brpop("email1");
List<EmailSend> list = new ArrayList<>();
if(brpop.size()>0){
EmailSend es = new EmailSend();
es = gson.fromJson(brpop.get(1), EmailSend.class);
list.add(es);
System.out.println(list);
}
}
}
};
t.start();
responseData.setStatus(0);
responseData.setMsg("OK");
//responseData.setData(brpop);
} catch (Exception e) {
e.printStackTrace();
responseData.setStatus(1);
responseData.setMsg("系统异常");
}
return responseData;
}
例子: 首先访问http://127.0.0.1/wx_public_number/wx_public/api/brpop.action用来开启消费消息
接着访问http://127.0.0.1/wx_public_number/wx_public/api/push.action 用来生产消息
这样就实现了消息队列的生产和消费。