1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.rabbitmq.demo.demo2;
import com.rabbitmq.client.Channel; import org.springframework.amqp.rabbit.annotation.*; import org.springframework.amqp.support.AmqpHeaders; import org.springframework.messaging.Message; import org.springframework.messaging.handler.annotation.Headers; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.stereotype.Component;
import java.util.Map;
@Component public class RabbitReceiver {
@RabbitListener(bindings = @QueueBinding( value = @Queue(value = "springboot-queue-1", durable = "true"), exchange = @Exchange(value = "springboot-exchange-1", durable = "true", type = "topic"), key = "springboot.*", ignoreDeclarationExceptions = "true" )) @RabbitHandler public void onMessage(Message message, Channel channel) throws Exception { System.out.println("消息:" + message.getPayload()); Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG); channel.basicAck(deliveryTag, false); }
@RabbitListener(bindings = @QueueBinding( value = @Queue(value = "${spring.rabbitmq.listener.order.queue.name}", durable = "${spring.rabbitmq.listener.order.queue.durable}"), exchange = @Exchange(value = "${spring.rabbitmq.listener.order.exchange.name}", durable = "${spring.rabbitmq.listener.order.exchange.durable}", type = "${spring.rabbitmq.listener.order.exchange.type}"), key = "${spring.rabbitmq.listener.order.exchange.key}", ignoreDeclarationExceptions = "${spring.rabbitmq.listener.order.exchange.ignoreDeclarationExceptions}" )) @RabbitHandler public void onMessage2(@Payload String message, @Headers Map<String, Object> headers, Channel channel) throws Exception { System.out.println("消息:" + message); Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG); channel.basicAck(deliveryTag, false); } }
|