赞
踩
mq 用的很少,简单记录一下。
需求:数据提供方采用mq的方式推送数据,我方接收数据后,入库。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
spring.rabbitmq.host=
spring.rabbitmq.port=
spring.rabbitmq.username=
spring.rabbitmq.password=
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.virtual-host=virtual
spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.simple.concurrency=1
spring.rabbitmq.listener.simple.max-concurrency=1
spring.rabbitmq.listener.simple.retry.enabled=true
@Component
@Slf4j
@RabbitListener(queues = "test")
public class RabbitMQListener {
@RabbitHandler
public void onMessage(HashMap<String, Object> baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
try {
String messageType = (String) baseMap.get("messageType");
log.info(messageType);
// 业务同步
// 确认已经成功处理的消息,确保 RabbitMQ 从队列中将其移除,以避免重复消费
channel.basicAck(deliveryTag, false);
} catch (Exception e) {
log.info("接收消息失败,重新放回队列");
try {
// 否认消息并重新入队
channel.basicNack(deliveryTag, false, true);
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
}
telnet ip port
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。