消息阻塞队列测试类

  1.  
  2. /**
  3. *消息阻塞队列测试类
  4. */
  5. @Controller
  6. @RequestMapping(value="/queue")
  7. public class TestControlller {
  8. @Autowired
  9. private RedisTemplate redisService;
  10. static {
  11. new TestControlller().initThread("msgQueue");
  12. }
  13. /**启动消费数据线程**/
  14. public void initThread(String key){
  15. new Thread(new blpopMessageHandler(key)).start();
  16. }
  17. /**往队列里写数据**/
  18. @RequestMapping(value="/brpush")
  19. @UnSession
  20. @ResponseBody
  21. public boolean brpush(String key,String name,int age) throws Exception{
  22. boolean flag = redisService.rpush(key,JSON.toJSONString(new Edu(name,age)));
  23. return flag;
  24. }
  25. /**线程实时监听获取数据**/
  26. class blpopMessageHandler implements Runnable{
  27. private String key;
  28. private RedisTemplate redisTemplate= (RedisTemplate)SpringContextUtil.getBean("redisTemplate");
  29. public String getKey() {
  30. return key;
  31. }
  32. public void setKey(String key) {
  33. this.key = key;
  34. }
  35. public blpopMessageHandler(String key){
  36. this.key = key;
  37. }
  38. @Override
  39. public void run() {
  40. do{
  41. String result = redisTemplate.blpop(1000,key);
  42. if( EmptyUtil.isNotEmpty(result) ){
  43. //do something
  44. Edu edu = JSON.parseObject(result,Edu.class);
  45. System.out.println("【线程一】"+edu.getName()+"---->"+edu.getAge());
  46. }
  47. }while (true);
  48. }
  49. }
  50. }

model类

  1. package com.qianxiang.web.www.test.controller;
  2. import java.io.Serializable;
  3. public class Edu implements Serializable {
  4. private static final long serialVersionUID = -6336530413316596246L;
  5. private String name;
  6. private int age;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public int getAge() {
  14. return age;
  15. }
  16. public void setAge(int age) {
  17. this.age = age;
  18. }
  19. public Edu(String name,int age){
  20. this.name = name;
  21. this.age = age;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Edu{" +
  26. "name='" + name + '\'' +
  27. ", age=" + age +
  28. '}';
  29. }
  30. }

操作redis类

  1. package com.qianxiang.v1.common.redis;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import com.qianxiang.v1.common.util.EmptyUtil;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.stereotype.Component;
  11. import com.qianxiang.v1.common.util.DateUtils;
  12. import redis.clients.jedis.Jedis;
  13. import redis.clients.jedis.exceptions.JedisConnectionException;
  14. import redis.clients.jedis.exceptions.JedisException;
  15. import redis.clients.util.Pool;
  16. /**
  17. * 实现Jedis对象的封装,实现操作
  18. */
  19. @Component
  20. public class RedisTemplate {
  21. private static Logger logger = LoggerFactory.getLogger(RedisTemplate.class);
  22. // 封装一个pool池用于jedis对象的管理
  23. private Pool<Jedis> jedisPool;
  24. public RedisTemplate(Pool<Jedis> jedisPool) {
  25. this.jedisPool = jedisPool;
  26. }
  27. /**
  28. * 存储REDIS队列 顺序存储
  29. * @param key reids键名
  30. * @param value 键值
  31. */
  32. public boolean rpush(String key,String value) {
  33. Jedis jedis = null;
  34. boolean flag = false;
  35. try {
  36. jedis = jedisPool.getResource();
  37. jedis.rpush(key.getBytes("utf-8"),value.getBytes("utf-8"));
  38. flag = true;
  39. } catch (Exception e) {
  40. //释放redis对象
  41. jedisPool.returnBrokenResource(jedis);
  42. e.printStackTrace();
  43. } finally {
  44. //返还到连接池
  45. close(jedis);
  46. }
  47. return flag;
  48. }
  49. /**
  50. * 获取队列数据
  51. * @param key 键名
  52. * @return
  53. */
  54. public String lpop(String key) {
  55. byte[] bytes = null;
  56. Jedis jedis = null;
  57. try {
  58. jedis = jedisPool.getResource();
  59. bytes = jedis.lpop(key.getBytes("utf-8"));
  60. } catch (Exception e) {
  61. //释放redis对象
  62. jedisPool.returnBrokenResource(jedis);
  63. e.printStackTrace();
  64. } finally {
  65. //返还到连接池
  66. close(jedis);
  67. }
  68. if( EmptyUtil.isNotEmpty(bytes)){
  69. try {
  70. return new String(bytes,"utf-8");
  71. } catch (UnsupportedEncodingException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. return null;
  76. }
  77. /**
  78. * 获取阻塞list中的数据
  79. * @param timeout
  80. * @param key
  81. * @return
  82. */
  83. public String blpop(int timeout,String key) {
  84. List<String> list = new ArrayList<>();
  85. Jedis jedis = null;
  86. try {
  87. jedis = jedisPool.getResource();
  88. list = jedis.blpop(timeout,key);
  89. } catch (Exception e) {
  90. //释放redis对象
  91. jedisPool.returnBrokenResource(jedis);
  92. e.printStackTrace();
  93. } finally {
  94. //返还到连接池
  95. close(jedis);
  96. }
  97. return EmptyUtil.isNotEmpty(list) && list.size() > 1 ? list.get(1):null;
  98. }
  99. private void close(Jedis jedis) {
  100. try{
  101. jedisPool.returnResource(jedis);
  102. }catch (Exception e){
  103. if(jedis.isConnected()){
  104. jedis.quit();
  105. jedis.disconnect();
  106. }
  107. }
  108. }
  109. }

猜你喜欢

转载自blog.csdn.net/qq_33243164/article/details/89531544
今日推荐