手动应答模式
- 第1步,编写application.properties
spring.rabbitmq.host = 127.0.0.1
spring.rabbitmq.port = 5672
spring.rabbitmq.password = test
spring.rabbitmq.username = test
spring.rabbitmq.virtual-host = /test
# 手动应答模式下,开启消息回调功能
spring.rabbitmq.publisher-confirms = true
spring.rabbitmq.publisher-returns = true
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
public class SpringBootRabbitMQConfiguration2 {
@Autowired
private RabbitTemplate rabbitTemplate;
@Bean
public AmqpTemplate amqpTemplate() {
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
System.out.println("------------------------------------------------");
System.out.println("ConfirmCallBackListener:correlationData=" + correlationData + ",ack=" + ack + ",cause=" + cause);
System.out.println("------------------------------------------------");
});
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
System.out.println("------------------------------------------------");
System.out.println("ReturnCallBackListener:message=" + new String(message.getBody()) + ",replyCode=" + replyCode + ",replyText=" + replyText + ",exchange=" + exchange + ",routingKey=" + routingKey);
System.out.println("------------------------------------------------");
});
return rabbitTemplate;
}
@Bean
public TopicExchange myExchange() {
return new TopicExchange("myExchange");
}
@Bean
public Queue myQueue() {
return new Queue("myQueue");
}
@Bean
public Binding bindingMyQueueToTestTopicExchange(@Qualifier("myQueue") Queue myQueue, @Qualifier("myExchange") TopicExchange myExchange) {
return BindingBuilder.bind(myQueue).to(myExchange).with("foo.*");
}
}
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;
@Component("springBoot2Consumer")
public class Consumer {
@RabbitListener(queues = "myQueue")
public void onMessage(Message message, Channel channel) throws Exception {
try {
System.out.println("消费者: " + new String(message.getBody()));
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("springBoot2Producer")
public class Producer {
public static final String EXCHANGE = "myExchange";
public static final String ROUTING_KEY = "foo.bar";
@Resource
private AmqpTemplate amqpTemplate;
public void test1() throws InterruptedException {
amqpTemplate.convertAndSend(EXCHANGE, ROUTING_KEY, "Hello, world!!!");
}
public void test2() throws InterruptedException {
amqpTemplate.convertAndSend("aaa", ROUTING_KEY, "Hello, world!!!");
}
public void test3() throws InterruptedException {
amqpTemplate.convertAndSend(EXCHANGE, "bbb", "Hello, world!!!");
}
public void test4() throws InterruptedException {
amqpTemplate.convertAndSend("aaa", "bbb", "Hello, world!!!");
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRabbitMQTest2 {
@Autowired
@Qualifier("springBoot2Producer")
private Producer producer;
@Test
public void test1() throws InterruptedException {
producer.test1();
}
@Test
public void test2() throws InterruptedException {
producer.test2();
}
@Test
public void test3() throws InterruptedException {
producer.test3();
}
@Test
public void test4() throws InterruptedException {
producer.test4();
}
}