1、pom文件引用RabitMQ:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、添加AmqpConfig配置文件类,接收消息的方法在给类的最后 onMessage 方法:
package com.yixin.web.config;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
@Configuration
public class AmqpConfig {
@Value("${rabitmq.exchange}")
private String exchange;
@Value("${rabitmq.queueName}")
private String queueName;
@Value("${rabitmq.routingKey}")
private String routingKey;
@Value("${rabitmq.host}")
private String host;
@Value("${rabitmq.username}")
private String username;
@Value("${rabitmq.password}")
private String password;
@Value("${rabitmq.vhost}")
private String vhost;
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses(host);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(vhost);
connectionFactory.setPublisherConfirms(true); // 必须要设置
return connectionFactory;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
// 必须是prototype类型
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
}
//direct交换机
@Bean
public DirectExchange defaultExchange() {
return new DirectExchange(exchange);
}
//如果需要多个队列,再写一个方法
@Bean
public Queue queue() {
return new Queue(queueName, true); // 队列持久
}
@Bean
public Binding binding() {
return BindingBuilder.bind(queue()).to(defaultExchange()).with(routingKey);
}
@Bean
public SimpleMessageListenerContainer messageContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
//如果需要添加多个队列,setQueues(queue(),queue2())
container.setQueues(queue());
container.setExposeListenerChannel(true);
container.setMaxConcurrentConsumers(1);
container.setConcurrentConsumers(1);
container.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 设置确认模式手工确认
container.setMessageListener(new ChannelAwareMessageListener() {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
byte[] body = message.getBody();
System.out.println("receive msg : " + new String(body));
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); // 确认消息成功消费
}
});
return container;
}
}
3、参数配置:
rabitmq:
exchange: op_loanorder-exchange1
queueName: riskQueue
routingKey: riskQueue
host: 192.168.145.171:5672
username: guest
password: guest
vhost: yx_mq
4、调用
@Autowired
private AmqpTemplate rabbitTemplate;
@Value("${rabitmq.queueName}")
private String queueName;
public String send() {
rabbitTemplate.convertAndSend(queueName, "helloword");
return "helloword";
}