Full code is available on GitHub
All the Java code for the project is available in Github - requires Maven to run. See the README for more details - link here.
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.
- Using CometD - the Browser can use some javascript to subscribe to a CometD channel, keeping a long running connection open - and the endpoint of that channel can push data down directly to all its subscribers. Handily, Apache Camel comes with a CometD plugin that can handle all this on the server side for us.
The CometD and Camel Demo
The demo shows HTML messages being sent to an ActiveMQ JMS Queue, from where they are picked up by Camel and then routed to a CometD channel endpoint.
A HTML page consumes messages from the CometD channel - and as they are received, adds them to a list on the web page. An example of the HTML page is shown below (I've added some JQuery to initially flash the images in red, and then fade them out to black over time - just to make it obvious as new messages arrive).
The messages are just simple, generated automatically from a Java client app, which pushes them to ActiveMQ using a Spring JMSTemplate.
This flow is shown in the diagram below. All three constituent parts can be run from the same project (but they must be started in the sequence shown - ActiveMQ/Camel server first, then web browser, then Java client)
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 - ActiveMQ\Camel 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 Javascript Consumer
The Java Client Message Dispatcher
The Java Client Message Creator
Future Extensions
- 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.)