Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring - Remoting By Hessian

Hessian is a binary web service protocol from Caucho Technology, which provides a simple way to create web services. Spring provides built-in support for Hessian-based remoting. Here's a guide on how to set up Hessian with Spring:

  • Add Dependencies:

Make sure you have the necessary libraries for Spring and Hessian:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>YOUR_SPRING_VERSION</version>
</dependency>
<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>YOUR_HESSIAN_VERSION</version>
</dependency>
  • Define Service Interface:
public interface MyService {
    String sayHello(String name);
}
  • Implement Service:
public class MyServiceImpl implements MyService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}
  • Expose Service with HessianServiceExporter:

In your Spring configuration:

<bean name="/MyService" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="myServiceBean"/>
    <property name="serviceInterface" value="path.to.MyService"/>
</bean>

<bean id="myServiceBean" class="path.to.MyServiceImpl"/>
  • Client Side:

On the client side, you can use HessianProxyFactoryBean to consume the remote service:

<bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://host:port/app/MyService"/>
    <property name="serviceInterface" value="path.to.MyService"/>
</bean>

In Java:

ApplicationContext context = new ClassPathXmlApplicationContext("client-context.xml");
MyService service = (MyService) context.getBean("myServiceClient");
System.out.println(service.sayHello("Alice"));
  • Server Configuration:

Make sure you have a servlet configuration to handle the Hessian service on the server side, typically in web.xml.

Spring's built-in support for Hessian and Burlap has been deprecated, but it's still available for older versions. For new projects or for adding remoting to existing ones, more modern and widely adopted technologies such as RESTful services (with JSON or XML payloads) or gRPC are worth considering.

  1. Using Hessian for remoting in Spring framework:

    • Description: Hessian is a binary web service protocol that can be used for remoting in Spring, allowing communication between distributed components.

    • Code Example:

      <!-- Spring configuration for Hessian remoting -->
      <bean id="myService" class="com.example.MyServiceImpl"/>
      <bean id="hessianExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">
          <property name="service" ref="myService"/>
          <property name="serviceInterface" value="com.example.MyService"/>
      </bean>
      
  2. Configuring Hessian remoting with Spring:

    • Description: Hessian remoting is configured in Spring by defining a service bean and exporting it using the HessianServiceExporter.

    • Code Example:

      <!-- Spring configuration for Hessian remoting -->
      <bean id="myService" class="com.example.MyServiceImpl"/>
      <bean id="hessianExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">
          <property name="service" ref="myService"/>
          <property name="serviceInterface" value="com.example.MyService"/>
      </bean>
      
  3. Hessian remoting vs other Spring remoting options:

    • Description: Hessian remoting, compared to other Spring remoting options like HTTP invoker or RMI, provides a lightweight binary protocol suitable for web services with efficient serialization.

    • Code Example (HTTP Invoker):

      <!-- Spring configuration for HTTP Invoker remoting -->
      <bean id="myService" class="com.example.MyServiceImpl"/>
      <bean id="httpInvokerExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
          <property name="service" ref="myService"/>
          <property name="serviceInterface" value="com.example.MyService"/>
      </bean>
      
  4. Setting up Hessian remoting with Spring Boot:

    • Description: In Spring Boot, setting up Hessian remoting involves configuring the service bean and using the HessianServiceExporter in the application context.

    • Code Example (Spring Boot):

      @SpringBootApplication
      public class MyApplication {
      
          @Bean
          public MyService myService() {
              return new MyServiceImpl();
          }
      
          @Bean
          public HessianServiceExporter hessianExporter(MyService myService) {
              HessianServiceExporter exporter = new HessianServiceExporter();
              exporter.setService(myService);
              exporter.setServiceInterface(MyService.class);
              return exporter;
          }
      
          public static void main(String[] args) {
              SpringApplication.run(MyApplication.class, args);
          }
      }
      
  5. Handling remote invocations with Hessian in Spring:

    • Description: Remote invocations with Hessian in Spring involve exporting a service using HessianServiceExporter and creating a Hessian proxy for remote clients.

    • Code Example (Client Side):

      MyService myService = (MyService) new HessianProxyFactory().create(MyService.class, "http://example.com/myService");
      myService.someMethod();
      
  6. Hessian serialization and deserialization in Spring remoting:

    • Description: Hessian uses its own efficient binary serialization and deserialization mechanism, making it suitable for network communication in distributed systems.

    • Code Example:

      <!-- Spring configuration for Hessian remoting -->
      <bean id="myService" class="com.example.MyServiceImpl"/>
      <bean id="hessianExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">
          <property name="service" ref="myService"/>
          <property name="serviceInterface" value="com.example.MyService"/>
      </bean>
      
  7. Security considerations for Hessian remoting in Spring:

    • Description: When using Hessian remoting, it's essential to consider security aspects. Using HTTPS for communication and securing sensitive methods with proper authentication and authorization are recommended.

    • Code Example (Secure Hessian Exporter):

      HessianServiceExporter exporter = new HessianServiceExporter();
      exporter.setService(myService);
      exporter.setServiceInterface(MyService.class);
      exporter.setSecured(true);
      
  8. Examples of Hessian remoting usage in Spring applications:

    • Description: Examples of Hessian remoting in Spring applications can be found in scenarios where lightweight binary communication is preferred, such as in web services or microservices.

    • Code Example (Client Side):

      MyService myService = (MyService) new HessianProxyFactory().create(MyService.class, "http://example.com/myService");
      myService.someMethod();