Implementing Spring HTTP Invoker for Remote Service Communication
Server-Side Implementation
Service Enterface Definition
package com.example.service;
public interface MessageService {
String processMessage(String input);
}
Service Implementation
package com.example.service.impl;
public class MessageServiceImpl implements MessageService {
private NotificationService notificationService;
@Override
public String processMessage(String input) {
String response = "Remote invocation successful: " + input;
return response;
}
public void setNotificationService(NotificationService notificationService) {
this.notificationService = notificationService;
}
}
HTTP Invoker Configuration (message-servlet.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageServiceBean" class="com.example.service.impl.MessageServiceImpl">
<property name="notificationService" ref="notificationService"/>
</bean>
<bean name="/messageService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="messageServiceBean"/>
<property name="serviceInterface" value="com.example.service.MessageService"/>
</bean>
</beans>
Web Application Configuration (web.xml)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/message-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>message</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>message</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Cleint-Side Implementation
Client Service Interface
package com.client.service;
// Interface method signatures must match server-side exactly
public interface MessageService {
String processMessage(String input);
}
Client Proxy Configuration (client-config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageServiceProxy"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:8080/ServerApp/api/messageService"/>
<property name="serviceInterface" value="com.client.service.MessageService"/>
</bean>
</beans>
Action Class Integration
package com.client.actions;
import com.opensymphony.xwork2.ActionSupport;
import com.client.service.MessageService;
public class RemoteCallAction extends ActionSupport {
private MessageService messageService;
private String response;
public String executeRemoteCall() {
String requestData = "Sending data to server";
response = messageService.processMessage(requestData);
System.out.println("Response received: " + response);
return SUCCESS;
}
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}
Web Configuration for Client (web.xml)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/client-config.xml</param-value>
</context-param>
Spring Bean Wiring
<bean id="remoteCallAction" class="com.client.actions.RemoteCallAction"
scope="prototype">
<property name="messageService">
<ref bean="messageServiceProxy"/>
</property>
</bean>
Communication Flow
- Server exposes
MessageServiceviaHttpInvokerServiceExporterat/api/messageService - Client configures
HttpInvokerProxyFactoryBeanpointing to the server endpoint - Client invokes methods on the proxy as if calling a local service
- Spring serializes the request, transmits via HTTP, and deserializes the resposne
The HTTP Invoker mechanism provides a lightweight RMI alternative using standard Java serialization over HTTP, requiring both client and server to use compatible Spring versions.