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
随机推荐
- CSS 布局Float 【2】
1.页面标准文档流.浮动层.float属性 1.1 文档流 HTML页面的标准文档流(默认布局)是:从上到下,从左到右,遇块(块级元素)换行. 1.2 浮动层 浮动层:给元素的float属性赋值后,就 ...
- AfxMessageBox("这里为提示框的内容");程序弹出一个提示窗口,可以做调试提示信息
AfxMessageBox("这里为提示框的内容"); 同时AfxMessageBox(AFX_IDP_PARSE_INT);里面也可以系统宏定义的一些参数
- 探讨CMake中关于RPATH的使用
最近研究CMake,发现CMake对于RPATH的管理也非常人性化.官方说法是当动态库的编译也和执行档在同级目录下的时候,CMake会自动给执行档加入适当的RPATH.具体可以通过readelf -d ...
- 零散的笔记:jquery中的事件
1.替代mouseover和mouseout的事件 jquery的mouseover和mouseout事件在移到子页面时也会触发,这在移到一级菜单弹出二级菜单,移到二级菜单时一级菜单也要显示时,这两个 ...
- HTML5验证及日期显示
以前忽略了HTML5的强大功能,谁知有了它数据大部分都不需要自己验证,显示日历也不需要插件啦,一些小功能分享给大家 1.Email输入框,自动验证Email有效性. <!DOCTYPE HTML ...
- Android 数据库读取数据显示 [5]
2016-12-1 课程内容 昨天学了Android数据库升级.降级.创建 今天把数据库表里面的数据读取出来显示到手机屏幕上 下面代码是MainActivity.java 的代码 package co ...
- Swift互用性:与 C的API交互(Swift 2.0版)-b
节包含内容: 基本数据类型(Primitive Types) 枚举(Enumerations) 指针(Pointer) 全局常量(Global Constants) 预处理指令(Preprocesso ...
- android 休眠唤醒机制分析(三) — suspend
本文转自:http://blog.csdn.net/g_salamander/article/details/7988340 前面我们分析了休眠的第一个阶段即浅度休眠,现在我们继续看休眠的第二个阶段 ...
- Common Subsequence
#include<cstdio> #include<iostream> #include<cmath> #include<string> #includ ...
- keil C51绝对地址访问
在利用keil进行8051单片机编程的时,常常需要进行绝对地址进行访问.特别是对硬件操作,如DA AD 采样 ,LCD 液晶操作,打印操作.等等.C51提供了三种访问绝对地址的方法: 1. 绝对宏 在 ...