Camel AsyncProcessor and asyncprocessor

Source: Internet
Author: User

Camel AsyncProcessor and asyncprocessor
Camel supports a more complex asynchronous processing model. The asynchronous Processor implements an AsyncProcessor interface inherited from the Processor interface. The advantages of asynchronous Processor are as follows:
A. asynchronous Processor does not run out of threads because it is waiting for blocking calls. In this way, the system's scalability can be increased by reducing the number of threads while processing the same workload.
B. asynchronous Processor can be used to process routes in stages, and different thread pools can process their corresponding routing stages. This means that routes can be processed in parallel.
Disadvantage: asynchronous Processor implementation is much more complex than synchronous Processor.

Differences between asynchronous Processor and synchronous Processor:
A. An AsyncCallback object must be provided, which is notified after the exchange processing is complete.
B. The Exception cannot be thrown when the asynchronous Processor processes exchange, but the Exception should be stored in the Exception attribute of exchange.
C. asynchronous Processor must know the method in which it will complete processing, asynchronous or synchronous. If the process method returns true, the synchronization is complete. If the process method returns false, the asynchronous process is complete.
D. When the processor finishes processing exchange, it must call the callback. done (boolean sync) method. The sync parameter must be consistent with the return value of the process method.

For a route, full use of the asynchronous mode can reduce the usage of threads, which requires that the asynchronous processing API (that is, calling the asynchronous
Process Method). If the Consumer calls the synchronous process () method, the Consumer thread will be forcibly blocked when processing Exchange.
One thing to note is that when you call an asynchronous API, this does not mean that the processing process is asynchronous. This only provides the possibility of not bundling in the caller thread.

Whether asynchronous processing depends on the configuration of the Camel route.

The above is the official explanation of asynchronous Processor by Camel. Below is an example of my testing:

public static void main(String[] args) throws Exception {RouteBuilder builder = new RouteBuilder() {@Overridepublic void configure() throws Exception {RouteDefinition definition1 = this.from("file:H:/temp/in");RouteDefinition definition2 = definition1.process(new Processor() {@Overridepublic void process(Exchange exchange) throws Exception {System.out.println(Thread.currentThread().getName());System.out.println("process1");}}).process(new AsyncProcessor() {@Overridepublic void process(Exchange exchange) throws Exception {System.out.println("process");}@Overridepublic boolean process(Exchange exchange, AsyncCallback callback) {System.out.println(Thread.currentThread().getName());System.out.println("async process");try {Thread.sleep(10 * 1000);} catch (InterruptedException e) {e.printStackTrace();}callback.done(false);return false;}}).process(new Processor() {@Overridepublic void process(Exchange exchange) throws Exception {System.out.println(Thread.currentThread().getName());System.out.println("process2");}});definition2.to("file:H:/temp/out");}};DefaultCamelContext camelContext = new DefaultCamelContext();camelContext.addRoutes(builder);camelContext.start();Object object = new Object();synchronized (object) {object.wait();}}

When I see two asynchronous words, the intuition is that a new thread is enabled for processing when asynchronous Processor is used, but in the preceding example, the names of the three threads are the same,
In addition, process2 is printed after being blocked for 10 seconds. This indicates that the above three processors are executed in the same thread, which is also the reason for blocking for 10 seconds.

I personally think that there is a deviation in understanding the "Asynchronous" word of Camel asynchronous Processor. The Asynchronous Method here is only the processor method of processor.
Callback Function, rather than starting another thread. In addition, our self-writing Processor has limited asynchronous usage, because the Processor we write is called and AsyncCallback is provided by the upper layer, we can only call the done method to notify the upper-layer to complete the processing. We need to register the callback function and control the callback time of the callback function, at present, we cannot provide registration of callback functions. So we can't help wondering, who provided this AsyncCallback object? The source of the AsyncCallback object is provided by the consumer class. For the above example, In the FileConsumer class, the following is a snippet of the processExchange method of GenericFileConsumer (FileConsumer inherited from GenericFileConsumer)

getAsyncProcessor().process(exchange, new AsyncCallback() {public void done(boolean doneSync) {// noopif (log.isTraceEnabled()) {log.trace("Done processing file: {} {}", target, doneSync ? "synchronously" : "asynchronously");}}});

At this time, the created AsyncCallback object is the source callback object. Of course, in the subsequent process of Route execution, this callback object can be encapsulated. Among them, the process (Exchange exchange, AsyncCallback callback) of CamelInternalProcessor) the method is an example:
Callback = new InternalCallback (states, exchange, callback );
Here, we can't help but ask, since CamelInternalProcessor can package the source AsyncCallback object and add its own callback logic, why can't we do it ourselves? The Processor we wrote was originally called, is the wrapper. For details about the process, refer to the Camel route startup process.

If you have to add your own callback logic, you can only write the consumer by yourself, and write the consumer to control the source AsyncCallback object.
A packaging process of the source AsyncCallback object. As long as the outermost AsyncCallback object is called, the source AsyncCallback object will be called. Therefore, in the above example, if callback. done (false) is not executed in the second Processor, the routing process will never end, because the upper layer always thinks that the lower layer processing is not over. Of course, if we do not write an exception Processor, the routing process will still end normally, and Camel will handle it internally. However, if we write an asynchronous Processor, we must call the callback. done method.

So after such a call, I did not feel the non-blocking call, price reduction thread usage, and phased route processing mentioned by the official team. My personal feeling is that there is an additional callback method, in addition, this callback function is still very limited. Of course, this may be something wrong with your own understanding. If so, please correct it ......


What is the difference between American camels and camel?

Baidu searches for American camels or German camels. I have bought them in a store and I have some text descriptions on them. This is clear.
Camels now have 20 or 30 brands in China. However, most of the products we buy now are American camels and German camel active. One is registered in the United States, and then back for processing and production (only domestic brands), the other is a real international brand.
Reference: zhenwg.taobao.com

What brand of cigarettes does camel use?

Camels

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.