RedisThread.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.yihu.wlyy.redis;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.yihu.wlyy.service.app.prescription.PrescriptionService;
  4. import com.yihu.wlyy.task.PushMsgTask;
  5. import com.yihu.wlyy.util.SystemConf;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.data.redis.core.StringRedisTemplate;
  12. import org.springframework.stereotype.Component;
  13. import redis.clients.jedis.Jedis;
  14. import redis.clients.jedis.JedisPool;
  15. import redis.clients.jedis.JedisPoolConfig;
  16. /**
  17. * Created by Trick on 2017/8/7.
  18. */
  19. @Component
  20. public class RedisThread implements Runnable {
  21. private static Logger logger = LoggerFactory.getLogger(RedisThread.class);
  22. @Value("${channel.redis.host}")
  23. private String host;
  24. @Value("${channel.redis.port}")
  25. private Integer port;
  26. @Value("${channel.redis.password}")
  27. private String password;
  28. @Autowired
  29. private StringRedisTemplate redisTemplate;
  30. @Autowired
  31. private PrescriptionService prescriptionService;
  32. @Autowired
  33. private PushMsgTask pushMsgTask;
  34. //redis链接池
  35. private JedisPool pool;
  36. @Override
  37. public void run() {
  38. String key = SystemConf.getInstance().getSystemProperties().getProperty("redis_prescription_title");
  39. //初始化链接池
  40. initPool();
  41. while (true){
  42. try {
  43. Jedis jedis = pool.getResource();
  44. String message = jedis.rpop(key);
  45. if(StringUtils.isEmpty(message)){
  46. sleep(1000L);//如果没有读取到记录,等待1秒
  47. }else {
  48. redisMessage(message);
  49. }
  50. }catch (Exception e){
  51. logger.info(e.getMessage());
  52. sleep(5000L);
  53. initPool();
  54. }
  55. }
  56. }
  57. public void sleep(Long time){
  58. try {
  59. Thread.sleep(time);
  60. }catch (Exception e){
  61. logger.error(e.getMessage());
  62. }
  63. }
  64. public void initPool(){
  65. JedisPoolConfig poolConfig = new JedisPoolConfig();
  66. poolConfig.setMaxIdle(8);
  67. poolConfig.setMaxTotal(8);
  68. poolConfig.setMaxWaitMillis(-1);
  69. poolConfig.setMinIdle(1);
  70. if(StringUtils.isNotBlank(password)){
  71. pool = new JedisPool(poolConfig,host,port,100000,password);
  72. }else {
  73. pool = new JedisPool(poolConfig,host,port,100000);
  74. }
  75. }
  76. public void redisMessage(String message){
  77. logger.info("redis_onMessage...:"+message);
  78. //this.unsubscribe();
  79. try{
  80. JSONObject json = JSONObject.parseObject(message);
  81. String title = json.getString("title");
  82. if("dispensingComplete".equals(title)){//配药完成
  83. //药品配送完成,提醒取药
  84. String prescriptionCode = json.getString("prescription");
  85. prescriptionService.dispensingComplete(prescriptionCode);
  86. }else if("wechat".equals(title)){
  87. String data = json.getString("value");
  88. pushMsgTask.getQueue().put(new org.json.JSONObject(data));
  89. }else{
  90. }
  91. }catch (Exception e){
  92. logger.error("redis_error...",e);
  93. }
  94. }
  95. }