RedisThread.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.data.redis.core.StringRedisTemplate;
  8. import org.springframework.stereotype.Component;
  9. import redis.clients.jedis.Jedis;
  10. /**
  11. * Created by Trick on 2017/8/7.
  12. */
  13. @Component
  14. public class RedisThread implements Runnable {
  15. @Value("${channel.redis.host}")
  16. private String url;
  17. @Value("${channel.redis.port}")
  18. private Integer port;
  19. @Value("${channel.redis.password}")
  20. private String password;
  21. @Autowired
  22. private StringRedisTemplate redisTemplate;
  23. @Autowired
  24. private PrescriptionService prescriptionService;
  25. @Override
  26. public void run() {
  27. String key = SystemConf.getInstance().getSystemProperties().getProperty("redis_prescription_title");
  28. Jedis jedis = new Jedis(url,port);
  29. if(StringUtils.isNotBlank(password)){
  30. jedis.auth(password);
  31. }
  32. while (true){
  33. String message = jedis.rpop(key);
  34. if(StringUtils.isEmpty(message)){
  35. try{
  36. Thread.sleep(1000L);//如果没有读取到记录,等待1秒
  37. }catch (Exception e){
  38. e.printStackTrace();
  39. }
  40. }else {
  41. prescriptionService.redisMessage(message);
  42. }
  43. }
  44. }
  45. }