Results 1 to 3 of 3

Thread: RabbitTemplate.convertAndSend overrides MessageProperties

  1. #1

    Default RabbitTemplate.convertAndSend overrides MessageProperties

    I'm having an issue where headers that I explicitly set on a Message object get overwritten. It seems you're forced to use a MessagePostProcessor in order to add headers to a message. As an example, the following method creates a new instance of RabbitMessageProperties in RabbitTemplate class:

    Code:
    public void convertAndSend(String exchange, String routingKey, final Object object) throws AmqpException {
    		send(exchange, routingKey, new MessageCreator() {
    			public Message createMessage() {
    				return getRequiredMessageConverter().toMessage(object, new RabbitMessageProperties());
    			}
    		});
    	}
    This method is delegated to from the following method:

    Code:
    public void convertAndSend(String routingKey, final Object object) throws AmqpException {
    		convertAndSend(this.exchange, routingKey, object);
    	}
    Since I passed in a Message object with the headers I needed, I expected message and headers to be present but the headers were lost because of the new instance of RabbitMessageProperties created on the delegate method. Here is my code that creates a message and the desired headers:

    Code:
    MessageProperties mProperties = new RabbitMessageProperties();
        mProperties.setHeader("bondId", request.getBondId());
        mProperties.setHeader("jobName", request.getJobName());
        mProperties.setHeader("filename", outputFile.getName());
        mProperties.setHeader("startTime", startTime);
        mProperties.setHeader("stopTime", stopTime);
    
        Message msg = new Message(outputFileBArray, mProperties);
    
        getRabbitTemplate().convertAndSend(request.getCallbackQueue(), msg);
    Is this the desired behavior? If so, what's the best way to add user-defined headers to a message before sending it?

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,854

    Default

    We've changed this quite a bit on the trunk version (for the upcoming M2 release). In fact, we now have methods that take a Message object directly, e.g.:
    Code:
    void send(Message message) throws AmqpException;
    If you don't want to update to a snapshot dependency, then for M1 you could avoid the "convert" methods. Instead you can do something like this:
    Code:
    final Message someMessage = ...
    template.send(new MessageCreator() {
        public Message createMessage() {
            return someMessage;
        }
    });

  3. #3

    Default

    Thanks again, Mark. Responsive and helpful as always.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •