1,导包
org.springframework.boot
spring-boot-starter-amqp
2,发消息
前置工作,创建配置类加上@Configuration注解
2.1,配置rabbitmq的工厂
//使用value注解读取配置文件中的rabbit的账号密码端口号等信息
@Bean(“rboxConnectionFactory”)
public ConnectionFactory connectionFactory(
@Value("
s
p
r
i
n
g
.
r
a
b
b
i
t
m
q
.
h
o
s
t
"
)
S
t
r
i
n
g
h
o
s
t
,
@
V
a
l
u
e
(
"
{spring.rabbitmq.host}") String host, @Value("
spring.rabbitmq.host")Stringhost,@Value("{spring.rabbitmq.port}") int port,
@Value("
s
p
r
i
n
g
.
r
a
b
b
i
t
m
q
.
v
i
r
t
u
a
l
−
h
o
s
t
"
)
S
t
r
i
n
g
v
i
r
t
u
a
l
H
o
s
t
,
@
V
a
l
u
e
(
"
{spring.rabbitmq.virtual-host}") String virtualHost, @Value("
spring.rabbitmq.virtual−host")StringvirtualHost,@Value("{spring.rabbitmq.username}") String username,
@Value("${spring.rabbitmq.password}") String password) {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualHost);
//设置回调确认类型 connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
return connectionFactory;
}
2.2设置rabbitTemplate,失败回调
//配置rabbitTemplate
@Bean(“rabbitTemplate”)
public RabbitTemplate rabbitmqTemplate(
@Autowired @Qualifier(“rboxConnectionFactory”) ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
// 消息发送交换机失败回调
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (!ack) {
System.out.println(“消息”+ JSON.toJSONString(correlationData) +“发送交换机失败,” + cause);
}
});
// 消息发送队列失败回调
rabbitTemplate.setMandatory(true);
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
String correlationId = message.getMessageProperties().getHeader(PublisherCallbackChannel.RETURNED_MESSAGE_CORRELATION_KEY);
log.error(“消息: {}, 发送队列失败, 交换机: {}, 路由键: {}, 应答码: {}, 原因: {}”, correlationId, exchange, routingKey, replyCode, replyText);
});
return rabbitTemplate;
}
2.3发送消息
将配置好的rabbitTemplate注入
使用方法
rabbitTemplate.convertAndSend(“rabbit-demo”, “key-demo”, “哈哈哈heiha”, new CorrelationData(“72”));
3监听消息
3.1监听前的准备
在配置类中进行设置需要监听的rabbit的地址,端口,账号,密码,虚拟机,
@Bean(“rboxRabbitListenerContainerFactory”)//监听工厂的名字
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Value("
s
p
r
i
n
g
.
r
a
b
b
i
t
m
q
.
p
a
s
s
w
o
r
d
"
)
S
t
r
i
n
g
p
a
s
s
w
o
r
d
,
@
V
a
l
u
e
(
"
{spring.rabbitmq.password}")String password, @Value("
spring.rabbitmq.password")Stringpassword,@Value("{spring.rabbitmq.username}") String username,
@Value("
s
p
r
i
n
g
.
r
a
b
b
i
t
m
q
.
p
o
r
t
"
)
i
n
t
p
o
r
t
,
@
V
a
l
u
e
(
"
{spring.rabbitmq.port}") int port, @Value("
spring.rabbitmq.port")intport,@Value("{spring.rabbitmq.host}") String host,
@Value("${spring.rabbitmq.virtual-host}") String virtualHost) {
//设置需要监听的rabbit信息
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setPassword(password);
connectionFactory.setUsername(username);
connectionFactory.setPort(port);
connectionFactory.setHost(host);
connectionFactory.setVirtualHost(virtualHost);
//创建监听工厂
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
factory.setConcurrentConsumers(1);
factory.setMaxConcurrentConsumers(20);
configurer.configure(factory, connectionFactory);
return factory;
}
3.2监听类的编写
记得将类交给spring管理
@RabbitHandler
@RabbitListener(containerFactory = “testRabbitListenerContainerFactory”, queues = “wcl-queues-demo”)//设置监听工厂为刚才配置好的工厂,填写要监听的队列
public void process(Message testMessage) throws UnsupportedEncodingException {
String s = new String(testMessage.getBody(), “UTF-8”);
System.out.println("DirectReceiver消费者收到消息 : " + s);
}