Code I/O

A topnotch WordPress.com site

5 Minutes on Java: Creating a JMS producer with Camel and FUSE frameworks

Leave a comment

In the previous post I demonstrated how to quickly create a server push application.  However, the previous post deliberately avoided the producer component.  In this post we’ll create a producer totally de-coupled from the consumer. I like this approach because, the clients need not be developed within the producer context.

What you’ll need?

  • Spring: for injecting beans (avoid writing boiler plate code)
  • Apache Camel: enable routing and manage messaging infrastructure
  • FUSE: message broker

Create a Java project.

You’ll require the following jar files.

        org.springframework.asm-3.0.3.RELEASE.jar
        org.springframework.beans-3.0.3.RELEASE.jar
        org.springframework.context-3.0.3.RELEASE.jar
        org.springframework.context.support-3.0.3.RELEASE.jar
        org.springframework.core-3.0.3.RELEASE.jar
        org.springframework.jdbc-3.0.3.RELEASE.jar
        org.springframework.transaction-3.0.3.RELEASE.jar
        log4j-1.2.15.jar
        commons-lang-2.4.jar
        commons-codec-1.4.jar
        commons-logging-1.1.1.jar
        org.springframework.expression-3.0.3.RELEASE.jar
        commons-dbcp-1.4.jar
        commons-pool-1.5.4.jar
        slf4j-log4j12-1.5.10.jar
        slf4j-api-1.5.10.jar
        camel-core-2.4.0.jar
        camel-jetty-2.4.0.jar
        camel-jms-2.2.0.jar
        camel-spring-2.4.0.jar
        commons-discovery-0.2.jar
        commons-io.jar
        commons-management-1.0.jar
        org.springframework.aop-3.0.3.RELEASE.jar
        org.springframework.jms-3.0.3.RELEASE.jar
        activemq-all-5.4.0-fuse-00-00.jar

Create a applicationContext.xml and add the following beans

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:s="http://www.springframework.org/s"
	xmlns:context="http://www.springframework.org/schema/context"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

	<!-- ActiveMQ JMS -->
	<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
		p:brokerURL="tcp://localhost:61616" />

	<jms:listener-container container-type="default"
		destination-type="topic" connection-factory="connectionFactory"
		acknowledge="auto">
	</jms:listener-container>

	<camelContext xmlns="http://camel.apache.org/schema/spring">
	</camelContext>

	<!-- Producer -->
	<bean id="messageProducer" class="org.onesun.camel.test.MessageProducer" init-method="init">
		<property name="topic" value="jms:topic:flex-client-broadcast-topic" />
	</bean>

	<bean id="clientProxy" class="org.onesun.camel.test.CamelTestApp">
		<property name="messageProducer" ref="messageProducer" />
	</bean>
</beans>

The connectionFactory bean references the FUSE ActiveMQ broker endpoint to which the application must bind to.  the listener container uses the connectionFactory to create a default route to topics and finally, the camelContext activates the beans and the endpoints.  These three definitions are essential for interconnecting FUSE, JMS and Camel, with this we’re now ready to write a simple producer.

package org.onesun.camel.test;

import org.apache.camel.EndpointInject;
import org.apache.camel.InOnly;
import org.apache.camel.ProducerTemplate;
import org.apache.log4j.Logger;

@InOnly
public class MessageProducer {
	private static Logger logger = Logger.getLogger(MessageProducer.class);
	protected String topic = null;

	@EndpointInject
	protected ProducerTemplate producer;

	public void init(){
		logger.info(MessageProducer.class + " initialized");
	}

	public void send(String message) {
		producer.sendBody(topic, message);
	}
	public String getTopic() {
		return topic;
	}
	public void setTopic(String topic) {
		this.topic = topic;
	}
}

The MessageProducer bean provides interface to send message to a topic. Now we can create the main program that will publish messages onto the topic.

package org.onesun.camel.test;

import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CamelTestApp {
	private static ApplicationContext context = null;

	private static MessageProducer messageProducer = null;

	public static MessageProducer getMessageProducer() {
		return messageProducer;
	}

	public void setMessageProducer(MessageProducer messageProducer) {
		ClientProxy.messageProducer = messageProducer;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Load Spring
		if (context == null) {
			context = new ClassPathXmlApplicationContext("applicationContext.xml");
		}

		for(int index = 0; index < 10; index++){
			messageProducer.send(
				"<item><label>" +
				new Date() +
				"</label></item>"
			);
		}
	}
}

With this application running you should see the dates appear on the flex sample or any other consumer application.  Good luck fusing together frameworks and riding the Camel.

This example has been tested and validated on Linux and Windows.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s