希望在被投新手帖之前能收到一些高手的指责
玩java3年多了,仍然只敢在新手区发帖的人伤不起……
想找自信,就去面试别人,想立志,还是要在eye发帖子
先是Redis的bean定义,@Configuration大赞!xml都见鬼去!
主要的@Configuration
Queue实现
调用Queue
玩java3年多了,仍然只敢在新手区发帖的人伤不起……
想找自信,就去面试别人,想立志,还是要在eye发帖子
先是Redis的bean定义,@Configuration大赞!xml都见鬼去!
@Configuration
@PropertySource("classpath:mgr.properties")
//@ImportResource("classpath*:spring-config.xml")
public class RedisConf {
@Value("${redis.url}")
String redisUrl;
@Value("${redis.port}")
String redisPort;
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxActive(50);
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMaxWait(1000l);
jedisPoolConfig.setTestOnBorrow(true);
return jedisPoolConfig;
}
@Bean
public JedisShardInfo jedisShardInfo(){
return new JedisShardInfo(redisUrl, redisPort);
}
@Bean
public ShardedJedisPool shardedJedisPool(){
List<JedisShardInfo> jedisList = new ArrayList<JedisShardInfo>();
jedisList.add(jedisShardInfo());
return new ShardedJedisPool(jedisPoolConfig(), jedisList);
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}
主要的@Configuration
@Configuration
@Import(RedisConf.class)
public class MgrConf {...}
Queue实现
public class JedisQueue<T> {
private ShardedJedisPool shardedJedisPool;
private String suffix = "_";
private String name;
private Class<T> clazz;
public JedisQueue(ShardedJedisPool shardedJedisPool, Class<T> clazz) {
this.shardedJedisPool = shardedJedisPool;
this.clazz = clazz;
this.name = clazz.getName();
}
public JedisQueue(ShardedJedisPool shardedJedisPool, Class<T> clazz, String suffix) {
this.shardedJedisPool = shardedJedisPool;
this.name = clazz.getName();
this.clazz = clazz;
this.suffix += suffix;
this.name += this.suffix;
}
public void push(T... ts) {
ShardedJedis jedis = shardedJedisPool.getResource();
try {
for (T t : ts)
jedis.lpush(name, JsonUtils.toJson(t));
} finally {
shardedJedisPool.returnResource(jedis);
}
}
public void push(Collection<T> collection) {
ShardedJedis jedis = shardedJedisPool.getResource();
try {
for (T t : collection)
jedis.lpush(name, JsonUtils.toJson(t));
} finally {
shardedJedisPool.returnResource(jedis);
}
}
public T take() {
ShardedJedis jedis = shardedJedisPool.getResource();
try {
while (true) {
Threads.sleep(300l);
if (!jedis.exists(name))
continue;
String jsonT = jedis.rpop(name);
if (jsonT != null) {
return (T) JsonUtils.toT(jsonT, clazz);
}
}
} finally {
shardedJedisPool.returnResource(jedis);
}
}
}
调用Queue
@Autowired
public void init(ShardedJedisPool shardedJedisPool){
proxyQueue = JedisQueue.newQ(shardedJedisPool, Proxy.class);
tiredQueue = JedisQueue.newQ(shardedJedisPool, Proxy.class, "tired");
}