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.
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.
- Calculation.java
- CalculationImpl.java
- web.xml
- httpInvoker-servlet.xml
- client-beans.xml
- Client.java
1) Calculation.java
It is the simple interface containing one method cube.
- package com.javatpoint;
- public interface Calculation {
- int cube(int number);
- }
2) 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;
- }
- }
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.
- <?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>
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.
- <?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>
5) 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>
6) 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
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:
- 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
reference from:http://www.javatpoint.com/spring-remoting-by-http-invoker-example
Spring Remoting by HTTP Invoker Example--reference的更多相关文章
- Spring Remoting: Remote Method Invocation (RMI)--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...
- Spring Remoting: HTTP Invoker--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp Concept Overview ...
- Lingo (Spring Remoting) : Passing client credentials to the server
http://www.jroller.com/sjivan/entry/lingo_spring_remoting_passing_client Lingo (Spring Remoting) : P ...
- Spring Remoting: Burlap--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...
- Spring Remoting: Hessian--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The p ...
- Asynchronous calls and remote callbacks using Lingo Spring Remoting
http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...
- Spring Framework 4.3.22.RELEASE Reference文档目录
<Spring Framework Reference Documentation 4.3.22.RELEASE> https://docs.spring.io/spring/docs/4 ...
- spring remoting源码分析--Hessian分析
1. Caucho 1.1 概况 spring-remoting代码的情况如下: 本节近分析caucho模块. 1.2 分类 其中以hession为例,Hessian远程服务调用过程: Hessian ...
- Spring Remoting: Hessian
随机推荐
- 跟我学android-Activity介绍
Activity是android四大组件之一,activity 可以看成是一个屏幕,用户可以在这里做一些操作.activity通常都是满屏的,但也可以小于屏幕而浮于其它窗口之上,比如把activity ...
- linux的du和df命令
今天也有同学问我Linux下查看目录大小的命令,现在也将前阵子学习到du/df两个命令总结一下吧.前阵子测试工作中有遇到过由于磁盘空间满导致程序无法执行到情况,所以使用了df和du两个命令. du查看 ...
- sql编程 1
declare emp_count number;begin select count(*) into emp_count from emp where HIOREDATE >= TO_DATE ...
- js JSONP实例
<script type="text/javascript"> $(function(){ checkuserstatus(); $('#loginbutton').c ...
- matplotlib入门--1(条形图, 直方图, 盒须图, 饼图)
作图首先要进行数据的输入,matplotlib包只提供作图相关功能,本身并没有数据读入.输出函数,针对各种试验或统计文本数据输入可以使用numpy提供的数据输入函数. # -*- coding: gb ...
- C语言 位操作
c语言位操作中需要注意有: 位操作只针对整型和字符型数据 在右移操作中:对无符号数和有符号中的正数补 0:符号数中的负数,取决于所使用的系统:补 0 的称为“逻辑右移”,补 1 的称为“算术右移”. ...
- 学c语言做练习之统计文件中字符的个数
统计文件中字符的个数(采用命令行参数) #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[] ...
- 你不知道的JavaScript(作用域和闭包)
作用域和闭包 ・作用域 引擎:从头到尾负责整个JavaScript的编译及执行过程. 编译器:负责语法分析及代码生成等. 作用域:负责收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套非 ...
- 一个关于多线程和DbHelper的问题
我的初衷是这样的:在多线程环境下,每个数据库编号对应一个DbHelper对象. 下面是代码,不知道这样写有什么问题. namespace TestDAL { public class DB { pri ...
- MyEclipse10 中增加svn插件
http://www.cnblogs.com/bluesky4485/archive/2012/04/23/2467177.html 确实这种方法可行!向牛人学习!