温馨提示×

redis如何实现异步队列

小新
198
2021-01-21 12:10:32
栏目: 云计算

redis如何实现异步队列

redis实现异步队列的方法:

1.一般使用list结构作为队列,rpush生产消息,lpop消费消息,当lpop没有消息时,需适当sleep一会再重试。

示例:

public class RedisClient {

@Resource

private JedisPool jedisPool;

/**

* 向List头部追加记录

* @param key

* @param value

* @return 记录总数

*/

public void rpush(String key, String value) {

Jedis jedis = null;

try {

jedis = jedisPool.getResource();

jedis.rpush(key,value);

} catch (Exception e) {

throw e;

} finally {

if(jedis != null){

jedis.close();

}

}

}

}

//controller写一个写入队列的方法

@PostMapping("insert")

public void setList(@RequestBody QueueTest queueTest){

System.out.println("塞入一条数据");

redisClient.rpush("queueTest", JSON.toJSONString(queueTest));

}


0