Flat Preloader Icon

Spring Remoting by Hessian

  • Hessian is a binary-based web service protocol for remote method invocation. In the context of the Spring Framework, you can use Hessian for remoting to invoke remote services in a distributed system.
  • Hessian provides a lightweight and efficient way to serialize and deserialize data, making it suitable for high-performance communication between Java components. Here’s how to set up Spring remoting with Hessian:

Server Configuration:

Define the Service Interface:

  • Create a Java interface that defines the methods you want to expose as remote services.
				
					public interface MyService {
    String sayHello(String name);
}

				
			

Implement the Service:

  • Create a class that implements the service interface. This class contains the actual implementation of the service methods.
				
					public class MyServiceImpl 
implements MyService {
    @Override
    public String sayHello
    (String name) {
     return "Hello, " + name + "!";
    }
}

				
			

Configure Spring for Hessian Service Exporter

  • In your Spring configuration file (e.g., applicationContext.xml), configure the Hessian service exporter.
				
					<bean name="/myService" 
class="org.springframework.remoting
.caucho.HessianServiceExporter">
    <property name="service"
    ref="myService" />
    <property 
    name="serviceInterface" 
    value="com.example.MyService" />
</bean>

				
			

Client Configuration:

Configure Hessian Proxy:

  • In the client’s Spring configuration, set up a Hessian proxy to access the remote service.
				
					<bean id="myServiceProxy" 
class="org.springframework.remoting
.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" 
    value="http://localhost:8080
    /my-application/myService" />
    <property name="serviceInterface" 
    value="com.example.MyService" />
</bean>

				
			
  • In this configuration:
    • myServiceProxy is the proxy to the remote service.
    • serviceUrl specifies the URL where the remote service is hosted.
    • serviceInterface specifies the interface that defines the service contract.

Use the Proxy:

  • Inject the proxy bean into your client code and use it to invoke remote service methods.
				
					@Autowired
private MyService myServiceProxy;

public void invokeRemoteMethod() {
    String result =myServiceProxy.sayHello
    ("Alice");
    System.out.println(result);
}

				
			
  • With this setup, you’ve configured a Hessian server to expose a remote service and a client to access it over HTTP using the Hessian protocol. This allows you to call remote methods as if they were local, making it easier to build distributed Java applications with Spring and Hessian.