ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
เขียนโปรแกรมบน iPhone ด้วย MonoTouch
News
Php Tips
Ubuntu
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
C# Design Pattern
Linux Quick Tips
C# Tips & Technique
C# using Linq น่าใช้จริงๆ
Java & JavaScript Tips
MAVEN
Database & SQL
ZengCode Framework Guide
Mac OSx
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     Camel : [Using Synchronization callbacks]  (2011-09-09)

Use Synchronization callbacks to execute any after-processing you want
done when the Exchange is complete. Don’t worry about throwing exceptions
from your custom Synchronization—Camel will catch those and log them at
WARN level, and will then continue to invoke the next callback. This ensures
that all callbacks are invoked even if one happens to fail.

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.spi.Synchronization;

public class TestTestTest {

    public static void main(String[] args) throws Exception {

        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {

            public void configure() {
                from("direct:start")
                        .process(new Processor() {
                            public void process(Exchange exchange) throws Exception {
                                exchange.getUnitOfWork()
                                        .addSynchronization(new FileRollback());
                            }
                        })
                        .bean(TestInteger.class).log("Finished.........................");
            }
        });

        context.start();

        ProducerTemplate producerTemplate = context.createProducerTemplate();
        Exchange exchange = new DefaultExchange(context);
        exchange.getIn().setHeader("MyInteger", "1");


        producerTemplate.asyncSend("direct:start", exchange);


        Thread.sleep(3000);
        context.stop();
    }

  static  class FileRollback implements Synchronization {
        public void onComplete(Exchange exchange) {
            System.out.println("onComplete!!!!!!!!!!!");
            //do something when complete
        }
        public void onFailure(Exchange exchange) {
            System.out.println("onFailure!!!!!!!!!!!");
            //do somethidn when on failure
        }
}
}

 

Output :

2011-09-09 12:20:01,088 [INFO ] [org.apache.camel.impl.DefaultCamelContext][start][1318] - Apache Camel 2.8.0 (CamelContext: camel-1) is starting
2011-09-09 12:20:01,091 [INFO ] [org.apache.camel.impl.DefaultCamelContext][createManagementStrategy][2301] - JMX enabled. Using ManagedManagementStrategy.
2011-09-09 12:20:01,316 [INFO ] [org.apache.camel.impl.converter.AnnotationTypeConverterLoader][load][118] - Found 3 packages with 14 @Converter classes to load
2011-09-09 12:20:01,344 [INFO ] [org.apache.camel.impl.converter.DefaultTypeConverter][loadCoreTypeConverters][394] - Loaded 153 core type converters (total 153 type converters)
2011-09-09 12:20:01,355 [INFO ] [org.apache.camel.impl.converter.AnnotationTypeConverterLoader][load][118] - Found 1 packages with 2 @Converter classes to load
2011-09-09 12:20:01,377 [INFO ] [org.apache.camel.impl.converter.DefaultTypeConverter][loadTypeConverters][420] - Loaded additional 3 type converters (total 156 type converters) in 0.030 seconds
2011-09-09 12:20:01,436 [INFO ] [org.mortbay.log][info][67] - Logging to org.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
2011-09-09 12:20:01,755 [INFO ] [org.apache.camel.impl.DefaultCamelContext][doStartOrResumeRouteConsumers][1906] - Route: route1 started and consuming from: Endpoint[direct://start]
2011-09-09 12:20:01,755 [INFO ] [org.apache.camel.impl.DefaultCamelContext][start][1335] - Total 1 routes, of which 1 is started.
2011-09-09 12:20:01,755 [INFO ] [org.apache.camel.impl.DefaultCamelContext][start][1336] - Apache Camel 2.8.0 (CamelContext: camel-1) started in 0.669 seconds
2011-09-09 12:20:01,792 [INFO ] [route1][log][196] - Finished.........................
onComplete!!!!!!!!!!!
2011-09-09 12:20:04,777 [INFO ] [org.apache.camel.impl.DefaultCamelContext][doStop][1463] - Apache Camel 2.8.0 (CamelContext:camel-1) is shutting down
2011-09-09 12:20:04,778 [INFO ] [org.apache.camel.impl.DefaultShutdownStrategy][doShutdown][120] - Starting to graceful shutdown 1 routes (timeout 300 seconds)
2011-09-09 12:20:04,783 [INFO ] [org.apache.camel.impl.DefaultShutdownStrategy][run][460] - Route: route1 shutdown complete, was consuming from: Endpoint[direct://start]
2011-09-09 12:20:04,783 [INFO ] [org.apache.camel.impl.DefaultShutdownStrategy][doShutdown][158] - Graceful shutdown of 1 routes completed in 0 seconds
2011-09-09 12:20:04,786 [INFO ] [org.apache.camel.impl.DefaultInflightRepository][doStop][89] - Shutting down with no inflight exchanges.
2011-09-09 12:20:04,790 [INFO ] [org.apache.camel.impl.DefaultCamelContext][doStop][1519] - Uptime: 3.703 seconds
2011-09-09 12:20:04,790 [INFO ] [org.apache.camel.impl.DefaultCamelContext][doStop][1520] - Apache Camel 2.8.0 (CamelContext: camel-1) is shutdown in 0.012 seconds
 

 หากแก้ไข Code ส่ง Header ผิดไป ดังข้างล่าง

.....

exchange.getIn().setHeader("MyInteger", "xxx");

.....

 

จะได้ผลลัพธ์ดังข้างล่างนี้ครับ

/usr/lib/jvm/java-6-sun/bin/java -Didea.launcher.port=7538 -Didea.launcher.bin.path=/home/chiwa

    ........
  
onFailure!!!!!!!!!!!
2011-09-09 12:28:44,135 [INFO ] [org.apache.camel.impl.DefaultCamelContext][doStop][1463] - Apache Camel 2.8.0 (CamelContext:camel-1) is shutting down
2011-09-09 12:28:44,136 [INFO ] [org.apache.camel.impl.DefaultShutdownStrategy][doShutdown][120] - Starting to graceful shutdown 1 routes (timeout 300 seconds)
2011-09-09 12:28:44,141 [INFO ] [org.apache.camel.impl.DefaultShutdownStrategy][run][460] - Route: route1 shutdown complete, was consuming from: Endpoint[direct://start]
2011-09-09 12:28:44,141 [INFO ] [org.apache.camel.impl.DefaultShutdownStrategy][doShutdown][158] - Graceful shutdown of 1 routes completed in 0 seconds
2011-09-09 12:28:44,144 [INFO ] [org.apache.camel.impl.DefaultInflightRepository][doStop][89] - Shutting down with no inflight exchanges.
2011-09-09 12:28:44,147 [INFO ] [org.apache.camel.impl.DefaultCamelContext][doStop][1519] - Uptime: 3.687 seconds
2011-09-09 12:28:44,148 [INFO ] [org.apache.camel.impl.DefaultCamelContext][doStop][1520] - Apache Camel 2.8.0 (CamelContext: camel-1) is shutdown in 0.012 seconds

 


Comment
Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.050958 seconds to load.