redis 대기 열 초살 실현

package demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RedisSpike {

    // redis          
    // eg: 30   10   
    public static void main(String[] args) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMaxWaitMillis(3000);
        jedisPoolConfig.setMaxTotal(1024);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 63799);
        Jedis client = jedisPool.getResource();
        // lpush productlist 1 2 3 4 5 6 7 8 9 10   ID 
        client.lpush("productlist","1","2","3","4","5","6","7","8","9","10");
        ExecutorService executor = Executors.newFixedThreadPool(30);
        for (int i = 0; i < 30; i++) {
            executor.execute(new SpikeTask(i, jedisPool));
        }
        executor.shutdown();
    }
}

class SpikeTask implements Runnable {

    private int customerId;

    private JedisPool jedisPool;

    public SpikeTask (int customerId, JedisPool jedisPool) {
        this.customerId = customerId;
        this.jedisPool = jedisPool;
    }

    @Override
    public void run() {
        //     
        Jedis client = jedisPool.getResource();
        String productId = client.lpop("productlist");
        if (productId != null && productId.length() != 0) {
            System.out.println("  " + customerId + "   " + productId + "   ");
        } else {
            System.out.println("  " + customerId + "      ");
        }
    }
}

좋은 웹페이지 즐겨찾기