Raghu
Raghu

Reputation: 1393

Send MessageProperties [priority=anyInteger] while publishing message in RabbitMQ

we are using rabbit MQ and Spring Integration in our project. Every Message has a deliver mode, header, properties, and payload part. We want to add properties i.e) priority with value 2(any integer) , payload with "test message 3" and publish the message to the queue named OES. please see screen shot.

Adding properties[priority=3] in rabbitmq message

How to add the messageproperties i.e) priority =2(or any value) in the below outbound-channel-adapter(Spring Integration). I know we can add "headers" by adding into "mapped-request-headers" but i would like to add the properties. There are no properties defined for the MessageProperties in "outbound-channel-adapter". Is there a way to overcome this issue.

We have no issues with payload, it is going already. we want to add only the MessageProperties with priority=2(any value). how to add that in the outbound-channel-adapter(no need of hardcoding, should be generic)?

<!-- 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"
/>

Other configuration:

<!-- 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 */

}

Other Code:

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;


    }

If we are not using spring integration, then we can use channel.basicPublish below way and send the 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);

Please let me know if you need more details.

Upvotes: 2

Views: 1390

Answers (1)

Gary Russell
Gary Russell

Reputation: 174769

Properties are already mapped automatically - see the header mapper.

Simply use a <header-enricher/> to set the appropriate header and it will be mapped to the correct property. In the case of priority, the constant is here for the amqp-specific header constants, see here.

Upvotes: 2

Related Questions