Wednesday, December 30, 2009

Event Notification with Apache Camel

I ever tried using Apache Camel to solve the event notification problem.

Given a Sensor (event source) and a Display (listener), here's how I would implement it:

public class Sensor {

    @EndpointInject(uri="seda:sensor.events")
    private ProducerTemplate sensorProducer;
    
    private void fireSensor(SensorEvent sensorEvent) {
        sensorProducer.sendBody(sensorEvent);
    }

}

public class Display {

    @Consume(uri="seda:sensor.events")
    public void sensorUpdated(SensorEvent sensorEvent) {
        // ... code to update ...  
    }

}

Advantages


As you can see, the code is pretty concise and elegant. Even Listener interfaces are not needed here. Simply agree on an event object, and the endpoint (channel) URI, and the event is wired up.

POJO is somewhat preserved, if not for ProducerTemplate.

It's possible to receive response from listeners (asynchronously), but I think it should be thought as a feature of the routing engine, not the event notification framework.

Issues


The above isn't a true publish-subscribe though, since Camel's seda component won't support multiple consumers (multicasting) until Camel 2.2.0 is released.

It also uses hard coded endpoint URIs, which is quick and convenient for singleton event sources but falls apart when there are several event sources of the same type.

A routing engine introduces one more concept besides event sources, listeners, and event objects: endpoints (channels a.k.a. topics). I hope it's possible to remove this, but all publish-subscribe event notification frameworks use it, including Dojo AJAX Toolkit's dojo.publish.

The event source object can be put inside the event object so the listeners can filter it, which addresses one side of the problem.

Making endpoint URIs dynamic is another alternative, i.e. referring to its own Spring bean name via BeanNameAware.

Yet another alternative is having a router-consumer that routes the messages (events) to actual listeners. This way, listeners attach to the router similar to old JavaBean event notification way. But I think this is getting too complex.

Dynamic Endpoints

Here's some code to illustrate how to implement event notification with Apache Camel dynamic endpoints.

public class Sensor { 

    // dynamically created or injected
    private Endpoint sensorEndpoint;
    @EndpointInject
    private ProducerTemplate producer;
    
    private void fireSensor(SensorEvent sensorEvent) {
        producer.sendBody(sensorEndpoint, sensorEvent);
    }

}

public class Display {

    @Autowired
    private Endpoint sensorEndpoint;
    private Consumer sensorConsumer;
    
    @PostConstruct
    public void initialize() {
        // BeanProcessor should automatically
        // match the method based on argument type
        sensorConsumer = sensorEndpoint.createConsumer(
            new BeanProcessor(this, sensorEndpoint.getCamelContext()));
        sensorConsumer.start();
    }
    
    @PreDestroy
    public void destroy() {
        sensorConsumer.stop();
    }

    public void sensorUpdated(SensorEvent sensorEvent) {
        // ... code to update ...  
    }

}

It's far from elegant. Especially the part where the Event Driven Consumer has to be created, configured, started, and stopped manually.

It's also somewhat decoupled, since the consumer needs to only know the endpoint object or the endpoint URI. And the producer doesn't have to iterate all consumers and call their callbacks one by one.

Update: I've written a more advanced proof-of-concept event notification with Apache Camel here.

8 comments:

Claus Ibsen said...

You can use ref in @EndpointInject to refer to an Endpoint which you can register in the Registry. This is more dynamic than the static url option offers.

Claus Ibsen said...

And you can use @Consumer on your POJO to easily let it consume from some endpoint. See more at: http://camel.apache.org/pojo-consuming.html

Thats more elegant.

Hendy Irawan said...

Thank you Claus.

I'll explore the @Consume on specific injected Endpoint object (not endpoint URI) as you suggested. There's no such example on http://camel.apache.org/pojo-consuming.html though. Adequate documentation on this technique would be very helpful.

Anyway, I've posted another article on using Camel as event notification here:

http://spring-java-ee.blogspot.com/2010/01/advanced-event-notification-framework.html

Different technique, using Camel proxy beans instead of annotating POJOs.

Purohit D said...

Java development services with dedicated Java developers. Java development outsourcing with proficient Java Developers.

Anonymous said...

I just wish that I could find time to visit the campus so that I could see the exhibition; I think that’s an exciting one! www.purestates.com |

www.computergeleen.com |

www.guru4travell.com |

www.highstreet-entertainment.com |

www.cheshirebusinesslink.com |

www.universities-rankings.com |

www.healthandwellnessbydesign.com |

www.homes-products.com |

www.realestateblues.com |

www.computervideoservices.com |

Anonymous said...

This post is one of the most important post that I've ever got to read in my life. The significance of this post on my life is huge.crystalapartment.co.uk |

neveschambers.co.uk |

aikosolutions.co.uk |

lewishickey.co.uk |

stadiumcreative.co.uk |

salsagrande.co.uk |

butlerbag.co.uk |

thewatchavenue.co.uk |

artofbrand.co.uk |

bowdennursery.co.uk |

polo outlet said...

http://www.superbagsmarket.com/
http://www.superbagsmarket.com/louis-vuitton-bags-c-1.html
http://www.superbagsmarket.com/hermes-bags-c-2.html
http://www.superbagsmarket.com/prada-bags-c-3.html
http://www.superbagsmarket.com/chanel-bags-c-4.html
http://www.superbagsmarket.com/gucci-bags-c-5.html
http://www.superbagsmarket.com/burberry-bags-c-6.html
http://www.superbagsmarket.com/celine-bags-c-7.html
http://www.superbagsmarket.com/balenciaga-bags-c-8.html
http://www.superbagsmarket.com/christian-dior-bags-c-9.html
http://www.superbagsmarket.com/chloe-bags-c-10.html
http://www.superbagsmarket.com/bvlgari-bags-c-11.html
http://www.superbagsmarket.com/bally-bags-c-12.html
http://www.superbagsmarket.com/coach-bags-c-13.html
http://www.superbagsmarket.com/michael-kors-bags-c-14.html
http://www.superbagsmarket.com/mcm-bags-c-15.html
http://www.superbagsmarket.com/fendi-bags-c-16.html
http://www.superbagsmarket.com/mulberry-bags-c-17.html
http://www.superbagsmarket.com/marc-jacobs-bags-c-18.html
http://www.superbagsmarket.com/miu-miu-bags-c-19.html
http://www.superbagsmarket.com/ysl-bags-c-20.html
http://www.superbagsmarket.com/tory-burch-bags-c-21.html
http://www.superbagsmarket.com/givenchy-bags-c-22.html
http://www.superbagsmarket.com/ferragamo-bags-c-23.html
http://www.superbagsmarket.com/lancel-bags-c-24.html
http://www.superbagsmarket.com/loewe-bags-c-25.html
http://www.superbagsmarket.com/tods-bags-c-26.html
http://www.superbagsmarket.com/paul-smith-bags-c-27.html
http://www.superbagsmarket.com/dg-bags-c-28.html
http://www.superbagsmarket.com/alexander-wang-bags-c-29.html
http://www.superbagsmarket.com/bottega-veneta-bags-c-30.html
http://www.superbagsmarket.com/specials.html
http://www.guccishoesuk-2014.com/
http://www.burbagssale2014.com/
http://www.polo-ralphlaurenonline.com/
http://www.thebeatsbydre.net/

Nithyanantham M said...

Thanks for sharing this post
Dailymotion video downloader

Try Dailymotion video
downloader
to download videos from dailymotion.

Post a Comment

 
Copyright 2009 Spring vs Java EE Web Dev. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan