12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package com.yihu.wlyy.redis;
- import com.yihu.wlyy.service.app.prescription.PrescriptionService;
- import com.yihu.wlyy.util.SystemConf;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.stereotype.Component;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- /**
- * Created by Trick on 2017/8/7.
- */
- @Component
- public class RedisThread implements Runnable {
- private static Logger logger = LoggerFactory.getLogger(RedisThread.class);
- @Value("${channel.redis.host}")
- private String host;
- @Value("${channel.redis.port}")
- private Integer port;
- @Value("${channel.redis.password}")
- private String password;
- @Autowired
- private StringRedisTemplate redisTemplate;
- @Autowired
- private PrescriptionService prescriptionService;
- @Override
- public void run() {
- String key = SystemConf.getInstance().getSystemProperties().getProperty("redis_prescription_title");
- JedisPoolConfig poolConfig = new JedisPoolConfig();
- poolConfig.setMaxIdle(8);
- poolConfig.setMaxTotal(8);
- poolConfig.setMaxWaitMillis(-1);
- poolConfig.setMinIdle(1);
- JedisPool pool = null;
- if(StringUtils.isNotBlank(password)){
- pool = new JedisPool(poolConfig,host,port,100000,password);
- }else {
- pool = new JedisPool(poolConfig,host,port,100000);
- }
- while (true){
- try {
- Jedis jedis = pool.getResource();
- String message = jedis.rpop(key);
- if(StringUtils.isEmpty(message)){
- Thread.sleep(1000L);//如果没有读取到记录,等待1秒
- }else {
- prescriptionService.redisMessage(message);
- }
- }catch (Exception e){
- e.printStackTrace();
- logger.info(e.getMessage());
- }
- }
- }
- }
|