Skip to content

Apache Camel, Spring Remting and ActiveMQ

May 6, 2008
This is to show how to use JMS/ActiveMQ backed Spring Remoting using Apache Camel. This is based on Camel’s simple example you may find here.

First of all we have this interface, just as like the base example.

package org.apache.camel.spring.remoting;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ISay extends Remote {
String say() throws RemoteException;
}

You should put this interface somewhere in common between server and client API, e.g. common module.

Then we have the server-side impl.:

package org.apache.camel.spring.remoting;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SayService implements ISay {
private static final Log LOG = LogFactory.getLog(SayService.class);

private final String message = “Hello”;

public String say() {
LOG.info(“Invoking say() method with message: ” + message);
return message;
}
}

And server main class:

package org.apache.camel.spring.remoting;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RemotingServer {
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(“spring-server.xml”);
applicationContext.start();
}
}

As show above, this uses spring-server.xml descriptor file:

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd”&gt;

<bean id=”sayService” class=”org.apache.camel.spring.remoting.SayService”/>

<camelContext id=”camel” xmlns=”http://activemq.apache.org/camel/schema/spring”&gt;
<export id=”say” uri=”activemq:topic:org.apache.camel.spring.remoting.ISay”
serviceRef=”sayService” serviceInterface=”org.apache.camel.spring.remoting.ISay”/>
<route>
<from uri=”direct:say”/>
<to uri=”direct:sayImpl”/>
</route>
</camelContext>

<bean id=”activemq” class=”org.apache.camel.component.jms.JmsComponent”>
<property name=”connectionFactory”>
<bean class=”org.apache.activemq.ActiveMQConnectionFactory”>
<property name=”brokerURL” value=”tcp://localhost:61616″/>
</bean>
</property>
</bean>
</beans>

At the end we have client main class:

package org.apache.camel.spring.remoting;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.rmi.RemoteException;

public class RemotingClient {
public static void main(String[] args) throws RemoteException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(“spring-client.xml”);
ctx.start();

ISay say = (ISay) ctx.getBean(“sayProxy”);

System.out.println(“Calling service..”);
System.out.println(“Server said: ” + say.say());
System.out.println(“Service call done.”);

ctx.stop();
}
}

And client’s spring descriptor:

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd”&gt;

<camelContext id=”camel”
xmlns=”http://activemq.apache.org/camel/schema/spring”&gt;
<proxy id=”sayProxy”
serviceUrl=”activemq:topic:org.apache.camel.spring.remoting.ISay”
serviceInterface=”org.apache.camel.spring.remoting.ISay”/>
</camelContext>

<bean id=”activemq” class=”org.apache.camel.component.jms.JmsComponent”>
<property name=”connectionFactory”>
<bean class=”org.apache.activemq.ActiveMQConnectionFactory”>
<property name=”brokerURL” value=”tcp://localhost:61616″/>
</bean>
</property>
</bean>

</beans>

Now all you have to do is to start ActiveMQ and enjoy.

cd ~/apache-activemq-5.0.0/bin
./activemq
About these ads

From → java

3 Comments
  1. katz permalink

    what about asynchronism? can Camel do the same stuff Lingo does with asynchronous invocations of void methods?

  2. Tanveer Ahmed permalink

    Amin,

    Thanks for the step by step example, it helped me alot to understand things. Could you please write a simple example for usage of camel and RMI.

    Thanks,
    Tanveer

  3. James permalink

    Very useful, the camel docs are a bit sparse in this area.
    Thanks alot.

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

Follow

Get every new post delivered to your Inbox.

Join 105 other followers

%d bloggers like this: