プログラマーとして生まれて、本当に申し訳ありません.Redisのキューは簡単に実現します.

4139 ワード

初心者に投稿される前に上級者から非難されることを望んでいます.
Javaを游んで3年余りになって、依然として新米区で投稿する人だけが伤つけることができません......
自信を探したいなら、面接に行って、志を立てたいなら、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");
    }