--消费者
package com.common.mq.esmessage;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class MessageListener {
private static Logger logger = LoggerFactory.getLogger(EsMessageListener.class);
@Autowired
EsMessageComsumer esMessageComsumer;
@RabbitListener(queues = "queue_name")
public void process(Message msg, Channel channel) {
try {
String jsonStr = new String(msg.getBody(), "UTF-8");
com.dao.Message message = JSON.parseObject(jsonStr, com.dao.Message.class);
logger.info("收到rabbitmq数据message:{}", message);
esMessageComsumer.addMessage(message);
// Thread.sleep(5);
} catch (Exception e) {
logger.error("【插入es】rabbit获取消息失败:{}", e);
} finally {
try {
channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
} catch (IOException e) {
logger.error("【离线状态MQ消费发生异常】{}", e);
}
}
}
}
package com.common.mq.esmessage;
import com.common.controller.elastic.ElasticHandler;
import com.dao.Message;
import com.service.MessageService;
import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.*;
@Component
@EnableScheduling
public class MessageComsumer {
private static Logger logger = LoggerFactory.getLogger(MessageListener.class);
@Autowired
MessageService messageService;
@Value("${insert.es.numelements}")
int numelements;
@Value("${insert.es.timeout}")
int timeout;
ExecutorService executorService;
public volatile List<Message> messageList;
final BlockingQueue<Message> q = new LinkedBlockingQueue<Message>(10000);
@PostConstruct
public void init() {
messageList = new CopyOnWriteArrayList<>();
executorService = Executors.newCachedThreadPool();
}
@Scheduled(fixedRate = 100)
public void consumer() {
try {
int size = q.size();
logger.info("本地现有消息数: {}", size);
if (size != 0) {
List<Message> messages = Lists.newArrayList();
try {
Queues.drain(q, messages, numelements, timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.submit(new TaskWithResult(esMessageService, messages));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void addMessage(Message message) {
try {
this.q.add(message);
} catch (Exception e) {
logger.error("addMessage error: {}", e);
}
}
}
class TaskWithResult implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(ElasticHandler.class);
private ESMessageService esMessageService;
public volatile List<Message> messages;
public TaskWithResult(MessageService messageService, List<Message> messages) {
this.esMessageService = esMessageService;
this.messages = messages;
}
@Override
public void run() {
try {
logger.info("开始插入es数据");
Long l1 = System.currentTimeMillis();
messageService.insertMessage(this.messages);
Long l2 = System.currentTimeMillis();
Long stopWatch = l2 - l1;
logger.info("插入{}条数据, 耗时: {}", this.messages.size(), stopWatch);
} catch (Exception e) {
e.printStackTrace();
}
}
}
--生产者
public void convertAndSend(String exchangeName,String routeKey,Object object){
rabbitTemplate.convertAndSend(exchangeName,routeKey,object);
}