天天看点

Spring Remoting by HTTP Invoker Example--reference

Spring provides its own implementation of remoting service known as HttpInvoker. It can be used for http request than RMI and works well across the firewall.

By the help of HttpInvokerServiceExporter and HttpInvokerProxyFactoryBean classes, we can implement the remoting service provided by Spring's Http Invokers.

Example of Spring HTTP Invoker

To create a simple spring's HTTP invoker application, you need to create following files.

Calculation.java
    CalculationImpl.java
    web.xml
    httpInvoker-servlet.xml
    client-beans.xml
    Client.java
           

Calculation.java

It is the simple interface containing one method cube.

package com.javatpoint;  
public interface Calculation {  
int cube(int number);  
} 
           

CalculationImpl.java

This class provides the implementation of Calculation interface.

package com.javatpoint;  
public class CalculationImpl implements Calculation{  
    public int cube(int number) {  
        return number*number*number;  
    }  
}  
           

web.xml

In this xml file, we are defining DispatcherServlet as the front controller. If any request is followed by .http extension, it will be forwarded to DispatcherServlet.

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    
    <servlet>  
    <servlet-name>httpInvoker</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
  
<servlet-mapping>  
    <servlet-name>httpInvoker</servlet-name>  
    <url-pattern>*.http</url-pattern>  
</servlet-mapping>  
  
</web-app>  
           

httpInvoker-servlet.xml

It must be created inside the WEB-INF folder. Its name must be servletname-servlet.xml. It defines bean forCalculationImpl and HttpInvokerServiceExporter.

<?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="calculationBean" class="com.javatpoint.CalculationImpl"></bean>  
<bean name="/Calculation.http"   
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
    <property name="service" ref="calculationBean"></property>  
    <property name="serviceInterface" value="com.javatpoint.Calculation"></property>  
</bean>  
  
</beans> 
           

client-beans.xml

In this xml file, we are defining bean for HttpInvokerProxyFactoryBean. You need to define two properties of this class.

serviceUrl
    serviceInterface

    <?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="calculationBean"   
    class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">  
        <property name="serviceUrl"   
             value="http://localhost:8888/httpinvoker/Calculation.http"></property>  
        <property name="serviceInterface" value="com.javatpoint.Calculation"></property>  
    </bean>  
    </beans>  
           

Client.java

This class gets the instance of Calculation and calls the method.

package com.javatpoint;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class Client {  
 public static void main(String[] args){  
  ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");  
  Calculation calculation = (Calculation)context.getBean("calculationBean");  
  System.out.println(calculation.cube(5));  
 }  
}  
           

Output

Output: 125  
<h3 id="h3">How to run this example</h3><p>Start and deploy the project, here we are assuming that server is running on 8888 port number. If the port number is different, change the serviceURL in client-beans.xml.</p><p>Then, Compile and Run the Client.java file.</p>
           

Web-based Client

In the example given above, we used console based client. We can also use web based client. You need to create 3 additional files. Here, we are using following files:

ClientInvoker.java
    index.jsp
    process.jsp
           

ClientInvoker.java

It defines only one method getCube() that returns cube of the given number

package com.javatpoint;  
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
      
    public class ClientInvoker {  
        public static int getCube(int number){  
            ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");  
            Calculation calculation = (Calculation)context.getBean("calculationBean");  
            return calculation.cube(number);  
        }  
    }  
           

index.jsp

It creates a form to get number.

<form action="process.jsp">  
Enter Number:<input type="text" name="number"/>  
<input type="submit" value="cube" />  
</form> 
           

process.jsp

It creates a form to get number.

<jsp:include page="index.jsp"></jsp:include>  
    <hr/>  
    <%@page import="com.javatpoint.ClientInvoker"%>  
      
    <%  
    int number=Integer.parseInt(request.getParameter("number"));  
    out.print("cube of "+number+" is: "+ClientInvoker.getCube(number));  
    %>  
           

Output

Spring Remoting by HTTP Invoker Example--reference
Spring Remoting by HTTP Invoker Example--reference

继续阅读