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.

  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.

download this example (developed using Myeclipse IDE)


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

时间: 2024-11-10 13:17:38

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

Spring Remoting: HTTP Invoker--转

原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp Concept Overview In the earlier articles we saw an introduction to spring remoting and its support for RMI, Hessian and Burlap. In this tutorial we look at one more su

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

原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring provides four ways to develop remote services. Remote services are services hosted on remote servers and accessed by clients over the network. For examp

Spring远程调用HTTP invoker

前言 在日常的工作中,会有客户端调用远程服务端接口的需求,这样的需求实现,在网上查到有像RMI.hessian.Burlap等,下文给出HTTP Invoker实现远程调用,他不用使用者考虑对象序列化的问题,对使用者,非常的友好. 下面介绍在Spring中配置HTTP Invoker实现远程调用.分为服务提供方和服务消费方两部分. 案例 服务提供方 1.定义一个接口和接口实现类,服务提供方提供. WelcomeService.java package com.aaron.service; imp

Spring Remoting: Hessian--转

原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The previous tutorial presents an overview of spring remoting and lists down various remoting protocols supported by Spring. In this tutorial we look at Sp

Spring Remoting: Burlap--转

原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the earlier tutorials we saw an introduction to Spring remoting and its support for RMI and Hessian. In this tutorial we look at another remoting protoco

Spring Framework 4.3.22.RELEASE Reference文档目录

<Spring Framework Reference Documentation 4.3.22.RELEASE> part I Spring Framework概述part II Spring Framework4.x的新特性part III 核心技术 7.IoC容器 8.Resources资源 9.校验.数据绑定.类型转换Validation, Data Binding, and Type Conversion 10.spring的EL表达式 11.spring AOP面向切面编程 12.

在Spring Boot中使用Http Invoker

https://blog.csdn.net/qq_34741165/article/details/88146067 在Spring 中使用Http Invoker在官方文档中已经描述的很清楚了,那么,在Spring Boot中怎么使用呢? 首先我们定义一个接口: public interface ITestService { String test(String hello); } 原文地址:https://www.cnblogs.com/leeego-123/p/12207237.html

[转]Spring——jar包详解

原文地址:http://my.oschina.net/huhaoren/blog/300856?p=1 spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了 spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试, 正式应用系统中是用不得这些类的. 除了spring.jar文件,Spring还包括有其它13个独立的jar包,各自包含着对应的Spring组件,用户可以根据自己的需要

spring mvc 介绍

Spring MVC Tutorial tag. * * If you do not want to deal with the intricities of the noscript * section, delete the tag (from ... to ). On * average, the noscript tag is called from less than 1% of internet * users. */--> <a href='http://delivery.ame