- /**
- *消息阻塞队列测试类
- */
- @Controller
- @RequestMapping(value="/queue")
- public class TestControlller {
- @Autowired
- private RedisTemplate redisService;
- static {
- new TestControlller().initThread("msgQueue");
- }
- /**启动消费数据线程**/
- public void initThread(String key){
- new Thread(new blpopMessageHandler(key)).start();
- }
- /**往队列里写数据**/
- @RequestMapping(value="/brpush")
- @UnSession
- @ResponseBody
- public boolean brpush(String key,String name,int age) throws Exception{
- boolean flag = redisService.rpush(key,JSON.toJSONString(new Edu(name,age)));
- return flag;
- }
- /**线程实时监听获取数据**/
- class blpopMessageHandler implements Runnable{
- private String key;
- private RedisTemplate redisTemplate= (RedisTemplate)SpringContextUtil.getBean("redisTemplate");
- public String getKey() {
- return key;
- }
- public void setKey(String key) {
- this.key = key;
- }
- public blpopMessageHandler(String key){
- this.key = key;
- }
- @Override
- public void run() {
- do{
- String result = redisTemplate.blpop(1000,key);
- if( EmptyUtil.isNotEmpty(result) ){
- //do something
- Edu edu = JSON.parseObject(result,Edu.class);
- System.out.println("【线程一】"+edu.getName()+"---->"+edu.getAge());
- }
- }while (true);
- }
- }
- }
model类
- package com.qianxiang.web.www.test.controller;
- import java.io.Serializable;
- public class Edu implements Serializable {
- private static final long serialVersionUID = -6336530413316596246L;
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Edu(String name,int age){
- this.name = name;
- this.age = age;
- }
- @Override
- public String toString() {
- return "Edu{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
操作redis类
- package com.qianxiang.v1.common.redis;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import com.qianxiang.v1.common.util.EmptyUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Component;
- import com.qianxiang.v1.common.util.DateUtils;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.exceptions.JedisConnectionException;
- import redis.clients.jedis.exceptions.JedisException;
- import redis.clients.util.Pool;
- /**
- * 实现Jedis对象的封装,实现操作
- */
- @Component
- public class RedisTemplate {
- private static Logger logger = LoggerFactory.getLogger(RedisTemplate.class);
- // 封装一个pool池用于jedis对象的管理
- private Pool<Jedis> jedisPool;
- public RedisTemplate(Pool<Jedis> jedisPool) {
- this.jedisPool = jedisPool;
- }
- /**
- * 存储REDIS队列 顺序存储
- * @param key reids键名
- * @param value 键值
- */
- public boolean rpush(String key,String value) {
- Jedis jedis = null;
- boolean flag = false;
- try {
- jedis = jedisPool.getResource();
- jedis.rpush(key.getBytes("utf-8"),value.getBytes("utf-8"));
- flag = true;
- } catch (Exception e) {
- //释放redis对象
- jedisPool.returnBrokenResource(jedis);
- e.printStackTrace();
- } finally {
- //返还到连接池
- close(jedis);
- }
- return flag;
- }
- /**
- * 获取队列数据
- * @param key 键名
- * @return
- */
- public String lpop(String key) {
- byte[] bytes = null;
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- bytes = jedis.lpop(key.getBytes("utf-8"));
- } catch (Exception e) {
- //释放redis对象
- jedisPool.returnBrokenResource(jedis);
- e.printStackTrace();
- } finally {
- //返还到连接池
- close(jedis);
- }
- if( EmptyUtil.isNotEmpty(bytes)){
- try {
- return new String(bytes,"utf-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * 获取阻塞list中的数据
- * @param timeout
- * @param key
- * @return
- */
- public String blpop(int timeout,String key) {
- List<String> list = new ArrayList<>();
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- list = jedis.blpop(timeout,key);
- } catch (Exception e) {
- //释放redis对象
- jedisPool.returnBrokenResource(jedis);
- e.printStackTrace();
- } finally {
- //返还到连接池
- close(jedis);
- }
- return EmptyUtil.isNotEmpty(list) && list.size() > 1 ? list.get(1):null;
- }
- private void close(Jedis jedis) {
- try{
- jedisPool.returnResource(jedis);
- }catch (Exception e){
- if(jedis.isConnected()){
- jedis.quit();
- jedis.disconnect();
- }
- }
- }
- }