Open In App

Spring – Remoting by HTTP Invoker

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

HTTP Invoker is a remote communication mechanism in the Spring framework that enables remote communication between Java objects over HTTP. It allows Java objects to invoke methods on remote Java objects, just as if they were local objects. In this article, we will learn how to implement Spring remoting by HTTP Invoker.

Step by Step Implementation

Step 1: Create a Java project in your favorite IDE and add the following dependencies to the pom.xml file:

XML




<!-- Adding spring-context and spring-web dependencies -->
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>5.3.0</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>5.3.0</version>
</dependency>


Step 2: Create an interface called CalculatorService.java which will define the methods that can be remotely invoked.

Java




// Define the methods that can be remotely invoked
public interface CalculatorService {
   int add(int a, int b);
   int subtract(int a, int b);
   int multiply(int a, int b);
   int divide(int a, int b);
}


Step 3: Create a class called CalculatorServiceImpl.java that implements the CalculatorService interface. This class will be the implementation of remote service.

Java




// Implementation of the remote service
public class CalculatorServiceImpl implements CalculatorService {
    
   @Override
   public int add(int a, int b) {
      return a + b;
   }
    
   @Override
   public int subtract(int a, int b) {
      return a - b;
   }
    
   @Override
   public int multiply(int a, int b) {
      return a * b;
   }
    
   @Override
   public int divide(int a, int b) {
      return a / b;
   }
}


Step 4: Create a class called HttpInvokerConfig.java that will be used to configure the remote service using the HTTP invoker.

Java




// Configuration of the remote service using HTTP Invoker
@Configuration
public class HttpInvokerConfig {
     
   public HttpInvokerServiceExporter httpInvokerServiceExporter(CalculatorService calculatorService) {
      HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
        
      // Setting the remote service to be exported
      exporter.setService(calculatorService);
        
      // Setting the interface of the remote service
      exporter.setServiceInterface(CalculatorService.class);
       
      return exporter;
   }
}


Step 5:Create a class called Client.java that will act as a client to the remote service.

Java




// Client to the remote service
public class Client {
    public static void main(String[] args)
    {
  
        // Get the ApplicationContext
        ApplicationContext context
            = new ClassPathXmlApplicationContext(
                "httpInvokerClientContext.xml");
  
        // Get the remote service bean
        CalculatorService calculatorService
            = (CalculatorService)context.getBean(
                "calculatorService");
  
        // Invoke the remote methods
        System.out.println("Add: "
                           + calculatorService.add(1, 2));
        System.out.println(
            "Subtract: "
            + calculatorService.subtract(1, 2));
        System.out.println(
            "Multiply: "
            + calculatorService.multiply(1, 2));
        System.out.println(
            "Divide: " + calculatorService.divide(1, 2));
    }
}


Step 6: Create an XML configuration file called applicationContext.xml that will contain the configuration for the client.

XML




<?xml version="1.0" encoding="UTF-8"?>
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
     
  <bean id="calculatorService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
      <property name="serviceUrl" value="http://localhost:8080/calculatorService" />
      <property name="serviceInterface" value="com.geeksforgeeks.CalculatorService" />
   </bean>
    
</beans>


Step 7: Run the Client.java class as a Java application and you should see the following output:

Output:

Add: 3
Subtract: -1
Multiply: 2
Divide: 0

Conclusion

In this article, we learned about the Spring HTTP Invoker and how to implement it step by step. We also covered some further subtopics to give you an idea of the various ways in which you can use the Spring HTTP Invoker in your projects.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads