Spring Integration – Using RMI Channel Adapters

This article explains how to send and receive messages over RMI using Spring Integration RMI channel adapters. It is composed of the following sections:

You can get the source code at my Github repository.

1 Implementing the service

This first part is pretty simple. The service is defined through annotations, so it will be auto detected by component scanning. It has a repository injected. It gets the data from an embedded database, as it will be shown in this same section:

@Service("defaultEmployeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepository;
    
    @Override
    public Employee retrieveEmployee(int id) {
        return employeeRepository.getEmployee(id);
    }
}

The repository is as follows:

@Repository
public class EmployeeRepositoryImpl implements EmployeeRepository {
    private JdbcTemplate template;
    private RowMapper<Employee> rowMapper = new EmployeeRowMapper();
    private static final String SEARCH = "select * from employees where id = ?";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    
    @Autowired
    public EmployeeRepositoryImpl(DataSource dataSource) {
        this.template = new JdbcTemplate(dataSource);
    }
    
    public Employee getEmployee(int id) {
        return template.queryForObject(SEARCH, rowMapper, id);
    }
    
    private class EmployeeRowMapper implements RowMapper<Employee> {
        public Employee mapRow(ResultSet rs, int i) throws SQLException {
            Employee employee = new Employee();
            employee.setId(rs.getInt(COLUMN_ID));
            employee.setName(rs.getString(COLUMN_NAME));
            
            return employee;
        }
    }
}

 

2 Application configuration

The following configuration exposes the service over RMI:

<context:component-scan base-package="xpadro.spring.integration"/>

<int-rmi:inbound-gateway request-channel="requestEmployee"/>

<int:channel id="requestEmployee"/>

<int:service-activator method="retrieveEmployee" input-channel="requestEmployee" ref="defaultEmployeeService"/>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- in-memory database -->
<jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:db/schemas/schema.sql" />
    <jdbc:script location="classpath:db/schemas/data.sql" />
</jdbc:embedded-database>

 

Let’s focus on the lines with the ‘int’ namespace:

The gateway’s function is to separate the plumbing from the messaging system from the rest of the application. In this way, it gets hidden from the business logic. A gateway is bi directional, so you have:

In this example, we are using a RMI inbound gateway. It will receive a message over RMI and send it to the requestEmployee channel, which is also defined here.

Finally, the service activator allows you to connect a spring bean to a message channel. Here, it is connected to the requestEmployee channel. The message will arrive to the channel and the service activator will invoke the retrieveEmployee method. Take into account that the ‘method’ attribute is not necessary if the bean has only one public method or has a method annotated with @ServiceActivator.

The response will then be sent to the reply channel. Since we didn’t define this channel, it will create a temporary reply channel.

temporary channel diagram

3 Implementing the client

The client we are going to implement will invoke the service in order to retrieve an employee. To do this, it will use the MessagingTemplate class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:xpadro/spring/integration/test/config/client-config.xml"})
public class TestRmiClient {
    @Autowired
    MessageChannel localChannel;
    
    @Autowired
    MessagingTemplate template;
    
    @Test
    public void retrieveExistingEmployee() {
        Employee employee = (Employee) template.convertSendAndReceive(localChannel, 2);
        
        Assert.assertNotNull(employee);
        Assert.assertEquals(2, employee.getId());
        Assert.assertEquals("Bruce Springsteen", employee.getName());
    }
}

The client uses the messagingTemplate to convert the Integer object to a Message and send it to the local channel. As shown below, there’s an outbound gateway connected to the local channel. This outbound gateway will send the request message over RMI.

<int-rmi:outbound-gateway request-channel="localChannel" remote-channel="requestEmployee" host="localhost"/>

<int:channel id="localChannel"/>

<bean class="org.springframework.integration.core.MessagingTemplate" />

 

4 Abstracting SI logic

In the previous section, you may have noticed that the client class which accesses the service has Spring Integration specific logic mixed with its business code:

In this section, I will implement this same example abstracting the messaging logic, so the client will only care about its business logic.

First, let’s take a look at the new client:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:xpadro/spring/integration/test/config/client-gateway-config.xml"})
public class TestRmiGatewayClient {
    @Autowired
    private EmployeeService service;
    
    @Test
    public void retrieveExistingEmployee() {
        Employee employee = service.retrieveEmployee(2);
        
        Assert.assertNotNull(employee);
        Assert.assertEquals(2, employee.getId());
        Assert.assertEquals("Bruce Springsteen", employee.getName());
    }
}

We can see now that the client just implements its business logic, without using neither message channels nor messaging template. It will just call the service interface. All the messaging definitions are in the configuration file.

client-gateway-config.xml

<int-rmi:outbound-gateway request-channel="localChannel" remote-channel="requestEmployee" host="localhost"/>

<int:channel id="localChannel"/>

<int:gateway default-request-channel="localChannel" 
    service-interface="xpadro.spring.integration.service.EmployeeService"/>

What we did here is add a gateway that will intercept calls to the service interface EmployeeService. Spring Integration will use the GatewayProxyFactoryBean class to create a proxy around the service interface. This proxy will use a messaging template to send the invocation to the request channel and wait for the response.

Application flow using RMI Channel Adapters

 

5 Conclusion

We have seen how to use Spring Integration to access a service over RMI.  We have also seen that we can not only explicitly send messages using the MessagingTemplate but also do it transparently with GatewayProxyFactoryBean.