一.简单使用
1.消费者
1.1配置文件
@Configuration
public class RabbitConfig {
@Value("directQueue")
private String directQueue;
@Value("directExchange")
private String directExchange;
// 创建队列
@Bean(""directQueue")
public Queue getQueue(){
return new Queue(directQueue);
}
// 创建交换机
@Bean("directExchange")
public DirectExchange getDirectExchange(){
return new DirectExchange(directExchange);
}
// 绑定
@Bean
public Binding bindFirst(@Qualifier("directQueue") Queue queue, @Qualifier("directExchange") DirectExchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("direct.messgae");
}
/**
* 在消费端转换JSON消息
* 监听类都要加上containerFactory属性
* @param connectionFactory
* @return
*/
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
factory.setAutoStartup(true);
return factory;
}
}
1.2消费
@Component
@RabbitListener(queues = "firstqueue", containerFactory="rabbitListenerContainerFactory")
public class FirstConsumer {
@RabbitHandler
public void process(@Payload Merchant merchant){
System.out.println("First Queue received msg : " + merchant.getName());
}
}
2.生产者
2.1配置文件
@Configuration
public class RabbitConfig {
/**
* 所有的消息发送都会转换成JSON格式发到交换机
* @param connectionFactory
* @return
*/
@Bean
public RabbitTemplate gupaoTemplate(final ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
}
2.2生产
@Autowired
AmqpTemplate gupaoTemplate;
public void send() throws JsonProcessingException {
Merchant merchant = new Merchant(1001,"a direct msg : 测试","测试消息");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(merchant);
gupaoTemplate.convertAndSend("directExchange","", json);
}
二.SpringBoot下死信队列
1.1配置
@Configuration
public class TtlConfig {
@Bean
public ConnectionFactory connectionFactory() throws Exception {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setUri(ResourceUtil.getKey("rabbitmq.uri"));
return cachingConnectionFactory;
}
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
}
@Bean("ttlQueue")
public Queue queue() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x-message-ttl", 11000); // 队列中的消息未被消费11秒后过期
// map.put("x-expire", 30000); // 队列30秒没有使用以后会被删除
return new Queue("TTL_QUEUE", true, false, false, map);
}
@Bean("expireQueue")
public Queue expireQueue() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x-expire", 30000); // 队列30秒没有使用以后会被删除
return new Queue("EXPIRE_QUEUE", true, false, false, map);
}
@Bean("ttlExchange")
public DirectExchange exchange() {
return new DirectExchange("TTL_EXCHANGE", true, false, new HashMap<>());
}
@Bean
public Binding binding(@Qualifier("ttlQueue") Queue queue,@Qualifier("ttlExchange") DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("test.ttl");
}
}
1.2使用
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TtlSender.class);
RabbitAdmin rabbitAdmin = context.getBean(RabbitAdmin.class);
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
MessageProperties messageProperties = new MessageProperties();
messageProperties.setExpiration("4000"); // 消息的过期属性,单位ms
messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
Message message = new Message("这条消息4秒后过期".getBytes(), messageProperties);
rabbitTemplate.send("TTL_EXCHANGE", "test.ttl", message);
// 随队列的过期属性过期,单位ms
rabbitTemplate.convertAndSend("TTL_EXCHANGE", "test.ttl", "这条消息");
}