mq的使用推荐一篇文章
mq介绍和使用
- 项目结构
(2)添加maven或者gradle坐标(看自己的项目是什么工程)
maven
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
gradle
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp
compile group: 'org.springframework.boot', name: 'spring-boot-starter-amqp', version: '2.1.0.RELEASE'
(2)application.yml 配置开启ACK
server.port=8888
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
# 开启发送确认
spring.rabbitmq.publisher-confirms=true
# 开启发送失败退回
spring.rabbitmq.publisher-returns=true
# 开启ACK
spring.rabbitmq.listener.direct.acknowledge-mode=manual
spring.rabbitmq.listener.simple.acknowledge-mode=manual
(3)MQconfig 配置topicExchange
package com.example.demo.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
//EXCHANGE
public static final String TEST_EXCHANGE = "COM.TEST.EXCHANGE";
//queue
public static final String TEST_QUEUE = "COM.TEST.QUEUE";
/**
* 配置交换机.
*/
@Bean
public TopicExchange getExchange() {
return new TopicExchange(TEST_EXCHANGE);
}
/**
* 配置队列.
*/
@Bean
public Queue getQueue() {
return new Queue(TEST_QUEUE);
}
/**
* 绑定exchange queue routekey.
*
* @param queueMessage 队列
* @param exchange 交换机
* @param routekey 路由
* @return
*/
public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
}
/**
* 绑定队列与exchange.
*
* @return
*/
@Bean
public Binding agencyChangeBinding() {
return bindingExchange(
getQueue(),
getExchange(),
RabbitConfig.TEST_QUEUE);
}
}
(4)producer(消息发送者)
package com.example.demo.controller;
import com.example.demo.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/v1/api/company")
public class Producer implements RabbitTemplate.ReturnCallback {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("test")
public void testSendMq() {
Logger log = LoggerFactory.getLogger(RabbitTemplate.class);
rabbitTemplate.setReturnCallback(this);
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (!ack) {
System.out.println("HelloSender消息发送失败" + cause + correlationData.toString());
} else {
System.out.println("HelloSender 消息发送成功 ");
}
});
rabbitTemplate.convertAndSend(
RabbitConfig.TEST_EXCHANGE, RabbitConfig.TEST_QUEUE, "45678998767909878987");
}
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
System.out.println("sender return success" + message.toString() + "===" + replyCode + "===" + replyText + "===" + exchange);
}
}
(5)comsumer(消息消费)
package com.example.demo.controller;
import com.example.demo.config.RabbitConfig;
import java.io.IOException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
@Component
public class Comsumer {
@RabbitHandler
@RabbitListener(queues = RabbitConfig.TEST_QUEUE)
public void aa(String nnnn,Channel channel, Message message){
try {
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
} catch (IOException e) {
e.printStackTrace();
//channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
}
System.out.println(nnnn);
}
}