Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added sample code for accessing message content as string in "Message processing example"

...

Section
Column

This is similar example as the one from above (also works only for InOut exchange) but it shows how you can extract message from an exchange in order to process it and send back.

Column
Code Block
langjava
titleMessage processing example
import org.apache.servicemix.MessageExchangeListener;
import org.apache.servicemix.jbi.util.MessageUtil;

import javax.annotation.Resource;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.transform.Source;

public class ListenerBean implements MessageExchangeListener {

    @Resource
    private DeliveryChannel channel;

    public void onMessageExchange(MessageExchange exchange) throws MessagingException {
        if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
                        NormalizedMessage message = exchange.getMessage("in");
		        Source content = message.getContent();
			//process content according to your logic
			//e.g. to access the message body as a String use
			String body = (new SourceTransformer()).toString(content);

			message.setContent(content);
			exchange.setMessage(message, "out");
			channel.send(exchange);
        }
    }

}

...

Note: Please make sure that the namespace specified at the top xmlns:test does match to the namespace used in the endpoint's service test:service. When calling the service by the service name then you need to add the namespace in order to find the service like {urn:test}service.

...