Java Remote Method Invocation (Java RMI) enables the programmer to create distributed Java technology-based applications, in which the methods of remote Java objects can be invoked from other Java virtual machines, possibly on different hosts. RMI uses object serialization to marshal and unmarshal parameters and does not truncate types, supporting true object-oriented polymorphism.

The development of remote-enabled services has been eased by Spring remoting support. Currently, Spring supports the following remoting technologies: Remote Method Invocation (RMI), HTTP Invoker, Hessian, Burlap, JAX-RPC, JAX-WS and JMS.

Spring RMI Support : Spring supports RMI via RmiProxyFactoryBean and RmiServiceExporter. RmiServiceExporter exports any Spring-managed bean as an RMI service and registers. RmiProxyFactoryBean is a factory bean creating a proxy for an RMI service. This proxy object talks with remote RMI services on behalf of the client.

This article shows how to develop a Infinispan RMI Service and Client. In the following sample application, a new cluster called DATA4J is created and User objects are added to cache and read from cache by using Spring RMI Clients. Added User Objects are distributed between two members of the cluster so First member adds and second member reads them via Infinispan cache cluster. The following figure shows the Infinispan RMI Client Architecture.

Infinispan RMI Client Architecture

Also Distributed Spring Beans via Infinispan Article is also suggested to investigate how to integrate Spring with Infinispan. Let us take a look the implementation.

Used Technologies :

JDK 1.7.0_40
Spring 3.2.4
Infinispan 5.2.7
JGroups 3.2.7
Maven 3.0.4

STEP 1 : CREATE MAVEN PROJECT

A maven project is created as below. (It can be created by using Maven or IDE Plug-in).

Infinispan - Spring RMI Integration

STEP 2 : LIBRARIES

Firstly, dependencies are added to Maven’ s pom.xml.

STEP 3 : CREATE USER BEAN

A new User Bean is created. It will be distributed between two nodes in DATA4J cluster.

STEP 4 : CREATE UserCacheListener CLASS

A new User Cache Listener is created. It listens distributed user-cache events.

STEP 5 : CREATE IUserCacheService INTERFACE

A new IUserCacheService Interface is created for service layer and it exposes cache functionality.

STEP 6 : CREATE UserCacheService IMPL

UserCacheService is created by implementing IUserCacheService.

STEP 7 : CREATE IRMIUserService INTERFACE

RMI User Service Interface is created in order to process RMI Requests. It is used by RmiServiceExporter for server side and used by RmiProxyFactoryBean for client side.

STEP 8 : CREATE RMIUserService IMPL

RMI User Service Impl is created by implementing IRMIUserService Interface.

STEP 9 : CREATE RMIServerStarter

RMIServerStarter is created to start RMI Server.

STEP 10 : CREATE RMIServiceClient

RMIServiceClient is created to start RMI Client. It triggers RMI Server by adding ten users.

RMIServiceClient Class for the first member of the cluster :

RMIServiceClient Class for the second member of the cluster :

STEP 11 : CREATE jgroups.xml

JGroups is a reliable multicast system written in the Java language. It is used to create clusters whose nodes can send messages to each other. Infinispan uses jgroups and jgroups.xml is created with default values as the following :

jgroups.xml for the members of the cluster :

STEP 12 : CREATE infinispan-distribution.xml

Infinispan configuration file, infinispan-distribution.xml, is created as the following. It covers Global settings shared among all cache instances created by a single CacheManager, cache configuration…

STEP 13 : CREATE rmiServerAppContext.xml

Spring rmiServerAppContext.xml is created. RMI Server is started in the light of this configuration.

STEP 14 : CREATE rmiClientAppContext.xml

Spring rmiClientAppContext.xml is created. RMI Client is started in the light of this configuration.

STEP 15 : BUILD PROJECT

After DATA4J_Spring_Infinispan Project is build, data4j-spring-infinispan-rmi-0.0.1-SNAPSHOT.jar will be created. The members of the cluster have to be built separately for each member.

STEP 16 : RUN PROJECT ON FIRST MEMBER OF THE CLUSTER

After created data4j-spring-infinispan-rmi-0.0.1-SNAPSHOT.jar file is run at the members of the cluster, the following output logs will be shown on first member’ s console :

First RMI Server Console :

First RMI Client Console :

Second RMI Server Console :

Second RMI Client Console :

STEP 17 : SOURCE CODE

DATA4J_Spring_RMI_Infinispan

REFERENCES :

Java RMI
Spring RMI
Infinispan 5.2 User Guide
Infinispan 5.2 API Documentation
JGroups Documentation
JGroups API Documentation