Apache Camel : Execute Apache camel routes Sync and Async

Apache camel route can be executed Asynchronous and Synchronous way.

Synchronous
- Sends an asynchronous body to the given endpoint.
private static org.springframework.beans.factory.BeanFactory BEAN_FACTORY;

@Autowired(required = false)
private org.apache.camel.ProducerTemplate producerTemplate;
/**
 * Sends an asynchronous body to the given endpoint.
 * Execute Apache Camel Route Asyncrousnly 
 */
private void sendEmailToReceipients(ReceipientExchange receipientExchange) {
    try {
        if (this.producerTemplate == null) {
            this.producerTemplate = BEAN_FACTORY.getBean("producerTemplate", 
                  ProducerTemplate.class);
        }
        this.producerTemplate.asyncSendBody(RouteConstants.SEND_EMAIL_ROUTE,
                receipientExchange);
    } catch (CamelExecutionException expObj) {

    }
}

Asynchronous
- Send the body to an endpoint returning any result output body
private static org.springframework.beans.factory.BeanFactory BEAN_FACTORY;

/*
 * Send the body to an endpoint returning any result output body.
 */
public void executeTaxComputation(TaxComutePayloadExchange taxPayloadExchange) {
    Map<String, Object> headers = new HashMap<>();
    try {
        if (this.producerTemplate == null) {
            this.producerTemplate = BEAN_FACTORY.getBean("producerTemplate", ProducerTemplate.class);
        }
        TaxComutePayload taxComputedPayload = 
                this.producerTemplate.requestBodyAndHeaders(RouteConstants.TAX_COMUTE_ROUTE,
                taxPayloadExchange, headers, CreateReturnRAPExchange.class);


    } catch (CamelExecutionException expObj) {

    }
}

1 Comments

Previous Post Next Post