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.


Http Invoker and Other Remoting techniques

You can use many Remoting techniques, let's see which one can be best for you.

Http Invoker Vs RMI

RMI uses JRMP protocol whereas Http Invokes uses HTTP protocol. Since enterprise applications mostly use http protocol, it is the better to use HTTP Invoker. RMI also has some security issues than HTTP Invoker. HTTP Invoker works well across firewalls.

Http Invoker Vs Hessian and Burlap

HTTP Invoker is the part of Spring framework but Hessian and burlap are proprietary. All works well across firewall. Hessian and Burlap are portable to integrate with other languages such as .Net and PHP but HTTP Invoker cannot be.


Example of Spring HTTP Invoker

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

  1. Calculation.java
  2. CalculationImpl.java
  3. web.xml
  4. httpInvoker-servlet.xml
  5. client-beans.xml
  6. Client.java

1) Calculation.java

It is the simple interface containing one method cube.

  1. package com.javatpoint;
  2. public interface Calculation {
  3. int cube(int number);
  4. }

2) CalculationImpl.java

This class provides the implementation of Calculation interface.

  1. package com.javatpoint;
  2. public class CalculationImpl implements Calculation{
  3. public int cube(int number) {
  4. return number*number*number;
  5. }
  6. }

3) 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.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <servlet>
  8. <servlet-name>httpInvoker</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>httpInvoker</servlet-name>
  14. <url-pattern>*.http</url-pattern>
  15. </servlet-mapping>
  16. </web-app>

4) 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.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>
  7. <bean name="/Calculation.http"
  8. class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
  9. <property name="service" ref="calculationBean"></property>
  10. <property name="serviceInterface" value="com.javatpoint.Calculation"></property>
  11. </bean>
  12. </beans>

5) client-beans.xml

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

  1. serviceUrl
  2. serviceInterface
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="calculationBean"
  7. class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
  8. <property name="serviceUrl"
  9. value="http://localhost:8888/httpinvoker/Calculation.http"></property>
  10. <property name="serviceInterface" value="com.javatpoint.Calculation"></property>
  11. </bean>
  12. </beans>

6) Client.java

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

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

Output

  1. Output: 125

How to run this example

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.

Then, Compile and Run the Client.java file.


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:

  1. ClientInvoker.java
  2. index.jsp
  3. process.jsp

ClientInvoker.java

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

  1. package com.javatpoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class ClientInvoker {
  5. public static int getCube(int number){
  6. ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
  7. Calculation calculation = (Calculation)context.getBean("calculationBean");
  8. return calculation.cube(number);
  9. }
  10. }

index.jsp

It creates a form to get number.

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

process.jsp

It creates a form to get number.

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

Output

reference from:http://www.javatpoint.com/spring-remoting-by-http-invoker-example

Spring Remoting by HTTP Invoker Example--reference的更多相关文章

  1. Spring Remoting: Remote Method Invocation (RMI)--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...

  2. Spring Remoting: HTTP Invoker--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp Concept Overview ...

  3. Lingo (Spring Remoting) : Passing client credentials to the server

    http://www.jroller.com/sjivan/entry/lingo_spring_remoting_passing_client Lingo (Spring Remoting) : P ...

  4. Spring Remoting: Burlap--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...

  5. Spring Remoting: Hessian--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The p ...

  6. Asynchronous calls and remote callbacks using Lingo Spring Remoting

    http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...

  7. Spring Framework 4.3.22.RELEASE Reference文档目录

    <Spring Framework Reference Documentation 4.3.22.RELEASE> https://docs.spring.io/spring/docs/4 ...

  8. spring remoting源码分析--Hessian分析

    1. Caucho 1.1 概况 spring-remoting代码的情况如下: 本节近分析caucho模块. 1.2 分类 其中以hession为例,Hessian远程服务调用过程: Hessian ...

  9. Spring Remoting: Hessian

随机推荐

  1. pod install后出现的错误

    [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use T ...

  2. 新修改了EMA的计算方法,合并线性回归率的计算。和通达信的结果一模一样

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  3. java对mysql数据库进行单表筛选备份、还原操作

    最近在做的一个项目需要对mysql数据库中的单个表格进行备份 其中,一部分表格需要进行筛选备份(例如对最近插入的1000条记录进行备份) 思路:java调用系统命令完成备份操作 假设现在有数据库tes ...

  4. 【BZOJ1901】Dynamic Rankings

    Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是 ...

  5. linux ulimit的使用,如何产生core文件,调试段错误

    ---恢复内容开始--- 下面先简单介绍下ulimit命令: 1. limit -a 可以查看系统各种资源的限制,如: core文件大小,数据段的大小等. $ ulimit -a core file ...

  6. golang io需要牢记的几个点

    对于Reader比较麻烦需要记住以下: When Read encounters an error or end-of-file condition after successfully readin ...

  7. Jquery学习笔记1-jquery总体代码框架

    第一次在博客中记录自己的笔记,希望能坚持下去吧,加油! 今天学习的是Jquery的源代码,官网上下载,然后使用DW(dream waver)编辑器打开Js(下载的是未压缩版),版本是2.0.3.第一次 ...

  8. getUrlParam,jQuery中的URL参数获取

    大家经常会需要在一段URL中截取到自己所需参数的值,下面的方法也许能帮到您: $.getUrlParam = function(name){ var reg = new RegExp("(^ ...

  9. PHP mongoDB 操作

    <?php /** * PHP操作MongoDB学习笔记 */ //************************* //** 连接MongoDB数据库 **// //************ ...

  10. python学习第二课要点记录

    字典使用时,使用for k,v in items():要将字典转换为元组,因此效率较低,如果数据量较大,就不建议使用这样的形式获取key和value的值,而要使用 for item in dict: ...