1.将数据放到redis队列中
public long lpush(String key, String value) { Jedis jedis=jedisPool.getResource(); try{ return jedis.lpush(key, value); }finally{ jedis.close(); } }
/** * 添加发送消息到redis队列 * @param jsonObject */ public void addMsgToRedisQueue(JSONObject jsonObject) { String msg = jsonObject.toJSONString(); redisCacheClient.lpush(SEND_MSG_QUEUE, msg); }
定时任务遍历redis队列,将取出的数据放到一个线程安全的队列。
//启动定时任务单线程获取数据 ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); scheduledExecutorService.scheduleAtFixedRate(new GetRedisSendMsgInfo(), 5, 10, TimeUnit.SECONDS);
//启动多线程消费数据 ExecutorService executorService = Executors.newFixedThreadPool(netEaseMsgSendThreadPoolSize); // 消费线程个数 int count = (int)netEaseMsgSendThreadPoolSize/2; for (int i = 0; i < count; i++) { executorService.submit(new ConsumeQueueMsg()); }线程安全的队列
private ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
//将返回结果以及参数放到mongoDb netEaseService.sendCardMsgLog(sendCardMsgLogInfo);