RedisThread.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.yihu.wlyy.redis;
  2. import com.yihu.wlyy.service.app.prescription.PrescriptionService;
  3. import com.yihu.wlyy.util.SystemConf;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.data.redis.core.StringRedisTemplate;
  10. import org.springframework.stereotype.Component;
  11. import redis.clients.jedis.Jedis;
  12. import redis.clients.jedis.JedisPool;
  13. import redis.clients.jedis.JedisPoolConfig;
  14. /**
  15. * Created by Trick on 2017/8/7.
  16. */
  17. @Component
  18. public class RedisThread implements Runnable {
  19. private static Logger logger = LoggerFactory.getLogger(RedisThread.class);
  20. @Value("${channel.redis.host}")
  21. private String host;
  22. @Value("${channel.redis.port}")
  23. private Integer port;
  24. @Value("${channel.redis.password}")
  25. private String password;
  26. @Autowired
  27. private StringRedisTemplate redisTemplate;
  28. @Autowired
  29. private PrescriptionService prescriptionService;
  30. @Override
  31. public void run() {
  32. String key = SystemConf.getInstance().getSystemProperties().getProperty("redis_prescription_title");
  33. JedisPoolConfig poolConfig = new JedisPoolConfig();
  34. poolConfig.setMaxIdle(8);
  35. poolConfig.setMaxTotal(8);
  36. poolConfig.setMaxWaitMillis(-1);
  37. poolConfig.setMinIdle(1);
  38. JedisPool pool = null;
  39. if(StringUtils.isNotBlank(password)){
  40. pool = new JedisPool(poolConfig,host,port,100000,password);
  41. }else {
  42. pool = new JedisPool(poolConfig,host,port,100000);
  43. }
  44. while (true){
  45. try {
  46. Jedis jedis = pool.getResource();
  47. String message = jedis.rpop(key);
  48. if(StringUtils.isEmpty(message)){
  49. Thread.sleep(1000L);//如果没有读取到记录,等待1秒
  50. }else {
  51. prescriptionService.redisMessage(message);
  52. }
  53. }catch (Exception e){
  54. e.printStackTrace();
  55. logger.info(e.getMessage());
  56. }
  57. }
  58. }
  59. }