camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject [DISCUSS - Camel 2.0]- Synchronization (UnitOfWork callbacks) and needed syntax DSL sugar
Date Tue, 12 May 2009 08:20:47 GMT
Hi

Now that the we had an internal API cleanup and the new Async API in place.
I thought I will take a look at the UnitOfWork and its synchronization hooks.

About synchronization
=================
org.apache.camel.spi.Synchronization is an interface defining 2
methods for callbacks when an Exchange is done.
- 1 callback for complete
- 1 callback for failure

This is really great and nice. What end users and Camel internally
actually need.

What you can do with this is for instance as some end users have
requirements for is to be able to send an email in case something goes
wrong.
Or log when a processing of the message is complete / finished etc.

Today you can do this with error handling and eg route to an additonal
endpoint when the route is at its end.
But what we would like is nice DSL syntax for this and the feature
implemented in a single logical execution wihtin Camel itself.

Some of the basis idea for the UnitOfWork constitutes this. That is a
context that abstracts the work an exchange undertakes during routing.
UnitOfWork is already integrated with  the Synchronization API and
thus it invokes the callback if any is registered.

But to use it today you need to access and register the callbacks on
the Exchange and thus you usually need a Processor and do the wiring
in Java code.
The code is not clean and nice so what is needed is some syntax sugar in the DSL

Syntax Sugar in DSL
===============
I have started experimenting with new DSL for registering callbacks.
But I am in doubt of naming and such.

For example I have:

        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                Processor complete = new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        assertEquals("Bye World",
exchange.getIn().getBody(String.class));
                        SynchronizeTest.this.complete = true;
                    }
                };

                from("direct:start")
                    .synchronize(complete)
                    .transform(constant("Bye World"))
                    .to("mock:result");
            }
        };

This was just a quick prototype to allow to register a Processor with
the completion callback. Notice that we use the .synchronize DSL
keyword.

I am wondering what good names would be
- synchronize
- onComplete
- onFailure
- onDone (eg for a single DSL keyword for both complete/failure)

For instance with onComplete:
                from("direct:start")
                    .onComplete(complete)
                    .transform(constant("Bye World"))
                    .to("mock:result");

Not just a processor
===============
The processor thingy was just an experiment the idea is to be able to
route, eg to a bean for POJO, log for logging, smtp for email etc.

For instance something like this:
                from("direct:start")
                    .onFailure()

.to("log:damn?level=WARN").to("smtp://noone@somemailserver?subject=Damn&to=damn@damn.org")
                    // end to indicate end of onFailure route
                    .end()
                    // this is the original route that we route to
after from direct:start
                    .transform(constant("Bye World"))
                    .to("mock:result");

And it should be a route by itself just like the intercept() so you
can route it to multiple destinations and do other work as you like.

And we could consider having scope as well. So you can define
synchronization hooks at global level, so they apply for all.


Any thoughts?


-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration
Interview with me:
http://architects.dzone.com/articles/interview-claus-ibsen-about?mz=7893-progress

Mime
View raw message