论坛首页 入门技术论坛

生而为程序员,真对不起,Redis的队列简单实现

浏览 11821 次
该帖已经被评为新手帖
作者 正文
   发表时间:2012-04-27   最后修改:2012-04-27
希望在被投新手帖之前能收到一些高手的指责
玩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);
        }
    }

    public Long size() {
        ShardedJedis jedis = shardedJedisPool.getResource();
        try {
            if (!jedis.exists(name))
                return 0l;
            return jedis.llen(name);
        } finally {
            shardedJedisPool.returnResource(jedis);
        }
    }

    public T poll() {
        ShardedJedis jedis = shardedJedisPool.getResource();
        try {
            if (!jedis.exists(name))
                return null;
            String jsonT = jedis.rpop(name);

            return jsonT == null ? null : (T) JsonUtils.toT(jsonT, clazz);
        } finally {
            shardedJedisPool.returnResource(jedis);
        }
    }

    public T poll(long timeout, TimeUnit unit) {
        ShardedJedis jedis = shardedJedisPool.getResource();
        try {
            long nanos = unit.toNanos(timeout);
            while (true) {
                long lastTime = System.nanoTime();
                Threads.sleep(300l);
                if (!jedis.exists(name))
                    continue;
                String jsonT = jedis.rpop(name);
                if (jsonT != null) {
                    return (T) JsonUtils.toT(jsonT, clazz);
                }
                if (nanos <= 0)
                    return null;
                nanos -= (System.nanoTime() - lastTime);
            }
        } finally {
            shardedJedisPool.returnResource(jedis);
        }
    }

    public ShardedJedisPool getShardedJedisPool() {
        return shardedJedisPool;
    }

    public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
        this.shardedJedisPool = shardedJedisPool;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public static <T> JedisQueue<T> newQ(ShardedJedisPool shardedJedisPool, Class<T> clazz) {
        return new JedisQueue<T>(shardedJedisPool, clazz);
    }

    public static <T> JedisQueue<T> newQ(ShardedJedisPool shardedJedisPool, Class<T> clazz, String suffix) {
        return new JedisQueue<T>(shardedJedisPool, clazz, suffix);
    }
}


调用Queue
    @Autowired
    public void init(ShardedJedisPool shardedJedisPool){
        proxyQueue = JedisQueue.newQ(shardedJedisPool, Proxy.class);
        tiredQueue = JedisQueue.newQ(shardedJedisPool, Proxy.class, "tired");
    }
   发表时间:2012-04-27  
哥们可以写写 项目中使用redis的一些经验和一些小技巧
0 请登录后投票
   发表时间:2012-04-27  
jinnianshilongnian 写道
哥们可以写写 项目中使用redis的一些经验和一些小技巧


下午测试过要是有心得再写吧

在RabbitMQ和Redis中选用Redis的原因是

1Redis我没玩过
2消息总量不大,但需要传输速度够快
3感觉用不上RabbitMQ的exchange
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics