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
随机推荐
- ReactNative for Android入坑(一)
最近找工作发现有些公司要求会ReactNative,决定入坑. 搭建环境:官网详细的教程附链接. 坑一:FQ,建议整个搭建过程中FQ.(FQ链接,注册有200M试用流量,环境搭建够了)第一步:安装Ch ...
- idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found?
idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found? 很久没用idea搭新工程,最近自己想做个东西,冲心搭个web工程,jar包都 ...
- 一款好看+极简到不行的HTML5音乐播放器-skPlayer
Demo: github skPlayer在线预览 预览: 单曲循环模式预览: 使用方法: 方式1:NPM npm install skplayer 方式2:引入文件 引入css文件: <lin ...
- 自动化构建工具gulp
1.优点 1.1 易于使用 通过代码优于配置的策略,gulp让简单的任务简单,复杂的任务可管理 1.2 构建快速 利用node.js流的威力,你可以快速构建项目并减少频繁的IO操作 1.3 插件高质 ...
- 初涉JavaScript模式 (11) : 模块模式
引子 这篇算是对第9篇中内容的发散和补充,当时我只是把模块模式中的一些内容简单的归为函数篇中去,在北川的提醒下,我才发觉这是非常不严谨的,于是我把这些内容拎出来,这就是这篇的由来. 什么是模块模式 在 ...
- 如何让IIS 8.0支持无后缀图片的访问
进入“MIME类型”模块后,我们点击右侧的“添加”,然后填好文件扩展名和类型值.对于无后缀的图片文件,扩展名只需填写“点”符号即可,类型值根据图片文件实际的扩展名填写.如果是jpeg格式的,那么就填写 ...
- Js仿弹框
收藏一个简单实用的JS弹框,通过隐藏和显示div来实现,代码来自脚本之家! <html> <head> <title> LIGHTBOX EXAMPLE </ ...
- Oracle开启归档
#查看数据库是否为归档模式select name ,log_mode from v$database; #改变归档模式到非归档模式shutdown normal/immediate; #关闭数据库st ...
- 线程通信机制:共享内存 VS 消息传递
在并发编程中,我们必须考虑的问题时如何在两个线程间进行通讯.这里的通讯指的是不同的线程之间如何交换信息. 目前有两种方式: 1.共享内存 2.消息传递(actor 模型) 共享内存: 共享内存这种方式 ...
- LD1-K(求差值最小的生成树)
题目链接 /* *题目大意: *一个简单图,n个点,m条边; *要求一颗生成树,使得其最大边与最小边的差值是所有生成树中最小的,输出最小的那个差值; *算法分析: *枚举最小边,用kruskal求生成 ...