在RabbitMQ中发布消息时发送MessageProperties [priority = anyInteger]

时间:2015-04-21 13:20:57

标签: rabbitmq spring-integration spring-amqp

我们在项目中使用了Rabbit MQ和Spring Integration。每个消息都有一个传递模式,标题,属性和有效负载部分。 我们想要添加属性,即优先级值为2(任何整数),有效负载为"测试消息3"并将消息发布到名为OES的队列中。请看屏幕截图。

Adding properties[priority=3] in rabbitmq message

如何在下面的出站通道适配器(Spring Integration)中添加messageproperties,即priority = 2(或任何值)。我知道我们可以添加"标题"通过添加到" mapped-request-headers"但我想添加属性。在"出站通道适配器"中没有为MessageProperties定义属性。有没有办法克服这个问题。

我们对有效载荷没有任何问题,它已经存在了。我们只想添加优先级= 2(任何值)的MessageProperties。如何在出站通道适配器中添加(不需要硬编码,应该是通用的)?

<!-- the mapped-request-headers should be symmetric with 
     the list on the consumer side defined in consumerbeans.consumerHeaderMapper() -->
<int-amqp:outbound-channel-adapter id="publishingAmqpAdapter" 
    channel="producer-processed-event-channel" 
    amqp-template="amqpPublishingTemplate"
    exchange-name="events_forwarding_exchange"
    routing-key-expression="headers['routing-path']"
    mapped-request-headers="X-CallerIdentity,routing-path,content-type,route_to*,event-type,compression-state,STANDARD_REQUEST_HEADERS"
/>

其他配置:

<!-- chain routes and transforms the ApplicationEvent into a json string -->
<int:chain id="routingAndTransforming"
    input-channel="producer-inbound-event-channel"
    output-channel="producer-routed-event-channel">
    <int:transformer ref="outboundMessageTracker"/>
    <int:transformer ref="messagePropertiesTransformer"/>
    <int:transformer ref="eventRouter"/>
    <int:transformer ref="eventToJsonTransformer"/>
</int:chain>

<int:transformer id="messagePayloadCompressor" 
   input-channel="compress-message-payload" 
   output-channel="producer-processed-event-channel"
   ref="payloadCompressor"/>

@Configuration("amqpProducerBeans")
@ImportResource(value = "classpath:com/apple/store/platform/events/si/event-producer-flow.xml")
public class AmqpProducerBeans {

    @Bean(name = { "amqpPublishingTemplate" })
        public AmqpTemplate amqpTemplate() {
            logger.debug("creating amqp publishing template");
            RabbitTemplate rabbitTemplate = new RabbitTemplate(producerConnectionFactory());
            SimpleMessageConverter converter = new SimpleMessageConverter();
            // following needed for retry logic
            converter.setCreateMessageIds(true);
            rabbitTemplate.setMessageConverter(converter);
            return rabbitTemplate;
        }

/*Other code commented */

}

其他代码:

import org.springframework.integration.Message;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.message.GenericMessage;

public class PayloadCompressor {

    @Transformer
    public Message<byte[]> compress(Message<String> message){
        /* some code commented */

        Map<String, Object> headers = new HashMap<String, Object>();
        headers.putAll(message.getHeaders());
        headers.remove("compression-state");
        headers.put("compression-state", CompressionState.COMPRESSED);
        Message<byte[]> compressedMessage = new GenericMessage<byte[]>(compressedPayload, headers);
      return compressedMessage;


    }

如果我们不使用spring集成,那么我们可以在下面使用channel.basicPublish并发送MessageProperties。

ConnectionFactory factory = new ConnectionFactory();
factory.setVirtualHost("/");
factory.setHost("10.102.175.30");
factory.setUsername("rahul");
factory.setPassword("rahul");
factory.setPort(5672);
Connection connection = factory.newConnection();
System.out.println("got connection "+connection);
Channel channel = connection.createChannel();
MessageProperties msgproperties= new MessageProperties() ;
MessageProperties.BASIC.setPriority(3);
// set Messageproperties with priority
    String exchangeName = "HeaderExchange";
      String routingKey = "testkey";
      //routingkey
      byte[] messageBodyBytes = "Message having priority value 3".getBytes();
      channel.basicPublish(exchangeName,
                           routingKey,
                           true,
                           msgproperties.BASIC,
                           messageBodyBytes);

如果您需要更多详细信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

属性已自动映射 - 请参阅header mapper

只需使用<header-enricher/>设置适当的标头,它就会映射到正确的属性。在优先级的情况下,对于特定于amqp的标头常量,常量为here,请参阅here