Message carrier exchange of the Apache camel route Node

Source: Internet
Author: User
Tags apache camel

In the route of camel, messages are transmitted in the form of exchange in each node of route. Therefore, understanding the exchange structure is very important for using camel.
If the exchange ID is not specified, camel sets one by default, which can be used to identify an execution of a route.
MEP message exchange pattern, which can be inonly or inout.
When exception occurs but a route exception occurs, the thrown exception is assigned to this variable (but does not seem like this in the example ?).
In message, the content passed in by a node is mandatory.
Out message is used only when MEP is inout, not mandatory.
Headers key-value pairs <String object> can be retrieved from the next node.
Attachments include attachments when calling Web service or sending emails.
Body message content, Java object.
Shows the structure of exchange:

Exchange can be directly used as a parameter in the method used by route. If the method in route is not exchange, camel converts the body in exchange to the parameter type of the method according to a set of rules.

The content of each part in this structure can be accessed in the following code example:

from("file:d:/temp/inbox?delay=3000").bean(new ExchangeManipulateBean(),"implicitConvert").bean(new ExchangeManipulateBean(),"traverseExchange").to("file:d:/temp/outbox");public class ExchangeManipulateBean {    private static Logger log = Logger.getLogger(ExchangeManipulateBean.class);    public void implicitConvert(String msg){                    log.debug("implicitConvert: " + msg);            }    public void traverseExchange(Exchange exchange){        log.debug("exchange.getExchangeId() " + exchange.getExchangeId());        log.debug("exchange.getPattern() " + exchange.getPattern());        log.debug("exchange.getException() " + exchange.getException());                Map<String, Object> props = exchange.getProperties();        for(String key:props.keySet()){            log.debug("props: " + key + " " + props.get(key));        }                Message in = exchange.getIn();//in always null,but can be accessed without exception                log.debug("exchange.getIn() " + in);        log.debug("in.getMessageId() " + in.getMessageId() );        log.debug("in.getClass().getName() " + in.getClass().getName());        Map<String, Object> headers = in.getHeaders();        for(String key:headers.keySet()){            log.debug("headers: " + key + " " + headers.get(key));        }                Object body = in.getBody();        log.debug("body " + body  + " " + body.getClass().getName());        log.debug("in.getAttachmentNames() " + in.getAttachmentNames());        //Message out = exchange.getOut();//whenever out is touched,information in headers are lost

1 * The implicitconvert method receives a string parameter. Camel converts the body of "in message" to a string according to certain rules during running, here, the object content is converted to a string, which is completed by the typeconverter in camel.
2 * Exchange. getexception () cannot get exception. You must use the following method: exception = (exception) Exchange. getproperty (exchange. exception_caught );
3 * Exchange. after getout () is called, you also need to copy the content in the header of "in message" to "out message. generally, values are passed to the next node by bypassing out. You can use this method: Exchange. getin (). setbody ("$ contents "),
The content can still be obtained at the next node, and the content of the node set in the header also exists.
4 * I am using camel 2.7.5. I don't know if these "pitfall" will be filled in later versions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.