Full code is available on GitHub
The Key Tech Players
The demo shows these various pieces of kit working (hopefully) harmoniously together:
- CometD Bayeux Ajax Push – push technology in play between the Camel server and the web browser(s) [http://cometd.org/]
- Apache Camel – enterprise integration framework – provides the Enterprise routing and CometD producer channel [http://camel.apache.org/]
- Apache ActiveMQ – JMS messaging broker [http://activemq.apache.org/]
- Apache Camel CometD plugin – CometD plugin for Camel [http://camel.apache.org/cometd.html]
- And the usual suspects – Java, Spring, Maven, Git
What’s the Scenario?
- Imagine a complex enterprise infrastructure, running a bunch of different systems, servers and technologies, with data flowing between them all.
- Commonly there will be some sort of Messaging system in play – handling routing and transformation of the various message flows. In our example we have ApacheMQ as the Messaging Server.
- The endpoints of some of these flows may be to send data to multiple clients. One way to do this is to route messages to a Topic, and have many clients subscribed to that topic – this is easy of the clients are Java, but not so easy if the clients are Browsers.
- Typically browser clients will have to use a hand rolled polling approach to check for new data being available.
The CometD and Camel Demo
The messages are just simple, generated automatically from a Java client app, which pushes them to ActiveMQ using a Spring JMSTemplate.
Running the Demo
- See the README file in Github for full details on how to run the demo (just remember they need to be started in the sequence above – ActiveMQCamel server needs to be running first for the browser to consume the cometd channel and the client to send JMS message)
The Camel Route
- Properties are set in application.properties
- The Camel Route – listens on a Queue and outputs to a CometD channel
The Java Client Message Dispatcher
The Java Client Message Creator
- Obviously this is a pretty simple demo – in a real world situation you may not want to be sending raw HTML down the channel. A better solution would be JSON, allowing the client to extract and format how it wanted to.
- (And if you’re using Camel – then you’re probably doing a bunch of other conversions and processing in the space between receiving and publishing the data.)