RMI meets JMS

A simple lightweight library for calling remote services via JMS.

Library is using only JMS 1.1 API and it's Requestor and bytes messages.

As simple as it can be, ready to use!


How to use

Put the maven dependency into your POM file:

<dependency>
   <groupId>cz.net21.ttulka</groupId>
   <artifactId>rmi-meets-jms</artifactId>
   <version>1.0.1</version>
</dependency>

Alternatively you can download the library in the JAR file and put it directly into your project classpath.

Remote service

The remote service is just a simple interface implemented by a class:

public interface Service {
 
    Integer myMethod1(Integer i);
    void myMethod2(String str);
}

public class ServiceImpl implements Service {

    public Integer myMethod1(Integer i) {
        return i * 2;
    }

    public void myMethod2(String str) {
        System.out.println("myMethod2 says: " + str);
    }
}

Methods of the service can be overloaded.

Java code

To use the library directly from a java code you need to create a JMS factory and a destination queue:

final QueueConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
final Queue queue = new ActiveMQQueue("MyQueue1");

Then, on the server side, the remote service provider must be created with the service implementation as a parameter:

final Service serviceImpl = new ServiceImpl();

final RemoteServiceProvider provider = new RemoteServiceProvider(connectionFactory, queue, serviceImpl);

On the client side the remote service consumer must be created and then the remote service can be obtained:

final RemoteServiceConsumer consumer = new RemoteServiceConsumer(connectionFactory, queue, Service.class);

final Service service = (Service)consumer.getService();

and the service can be called:

Integer res1 = service.myMethod1(3);
System.out.println(res1);               // will print "6"

service.myMethod2("Hello, server!");    // will print "Hello, server!" on the server's console output

Spring approach

The same can be achieved with the Spring framework very easily (as everything is easy with the Spring):

<!-- Server side -->
<bean id="server" class="cz.net21.ttulka.rmimeetsjms.RemoteServiceProvider">
	<constructor-arg ref="jmsFactory" />
	<constructor-arg ref="queue" />
	<constructor-arg ref="serviceImpl" />
</bean>

<!-- Client side -->
<bean id="client" class="cz.net21.ttulka.rmimeetsjms.RemoteServiceConsumer">
	<constructor-arg ref="jmsFactory" />
	<constructor-arg ref="queue" />
	<constructor-arg value="mypack.Service" />
</bean>
<bean id="serviceProxy" factory-bean="client" factory-method="getService" />

<!-- Service implementation -->
<bean id="serviceImpl" class="mypack.ServiceImpl" />

<!-- JMS connection factory -->
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
	<property name="brokerURL" value="tcp://localhost:61616" />
</bean>

<!-- JMS destination -->
<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
	<constructor-arg index="0" value="MyQueue1" />
</bean>

All you need to do is to initialize the Spring context:

final ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");

and get the service proxy on the client side:

final Service service = (Service)context.getBean("serviceProxy");

Limitation

There is only one limitation regarding the parameters of the remote service: all the parameters must be serializable (must implement java.io.Serializable).


Have fun!