Implementation of remote service programming based on spring

Source: Internet
Author: User
Tags http post object serialization serialization web services java se

I. INTRODUCTION

Fundamentally, remoting is actually a component of enterprise distributed computing. A service (or class) that is invoked within the same server (the Java Virtual machine) does not need to expose itself to a remote service; However, if you need to communicate with an external program (on a different server or in a different organization), you must implement it as a remote service. The Spring Framework provides a unique and flexible way to expose business classes to remote services.

At the heart of the Spring remoting architecture are service objects, which are actually some pojo, also known as spring beans. The spring framework can isolate these service objects from infrastructure details, such as how they are exposed to remote services, so that developers can focus on implementing the business interface of the service object rather than the details.

This remoting model provides a remote abstraction of the business services. It is responsible for orchestration and deserialization of method parameters and also for handling any exceptions thrown in service methods-wrapping them with unchecked remoteaccessexception exceptions. To implement a variety of services, Spring uses several design patterns. For example, it uses proxy design mode to translate your call to an HTTP POST request to a URL that points to the output service.

This article will focus on how to implement a remote service using spring. That is, to show that a normal Java object (POJO) is transformed into a remote service using the Spring Remoting API, so that the external program can invoke the service from its business implementation. The demo uses a sample loan processing application to implement a business service as a remote HTTP service and invoke the business methods in that class from a test client.

Two. Spring Remoting working mechanism

In this section, let's analyze the remoting work mechanism of spring in more detail. To implement a common Java class as a remote service, you need to provide some of the following:

1. Remote service output (exporter)-These classes are used to create remote service endpoints that are invoked by the client program. The service exporter also manages any registry used to query remote services.

2. Agent Factory bean-They are the factory classes that are used to create proxies that clients can use to connect to remote services.

3.HTTP invoker-As mentioned earlier, the Spring HTTP Invoker uses a remoting model that you can use to implement remote calls across HTTP while using Java serialization technology to pass Java objects. This makes it much easier to implement a remote service from a common Java class and allows you to focus on the business interface of a remote service without having to consider the implementation details of the remote infrastructure yourself.

The technology relies on the infrastructure of the RMI Invoker, but uses HTTP as the transport protocol.

On the client side, the Spring HTTP Invoker provides two types of clients: the standard API provided by the Java SE and the Commons httpclient API. By default, it uses HttpClient.

Next, let's look at the remote (Remoting) technology supported by the spring framework.

Remote technology enumeration supported by the spring framework

The spring framework supports a variety of remoting technologies. Below, let's take a brief description of them.

① Remote method call (RMI)

RMI is a distributed Java technology that can be invoked on a different Java virtual machine by means of a remote Java object. It is essentially a Java version of a remote procedure call (RPC), but it also provides the ability to pass multiple objects along with the corresponding request. RMI uses real object serialization to orchestrate and deserialize the parameters of a method without truncating its corresponding type.

Spring supports RMI in two ways: traditional RMI and remote technology using RMI Invoker.

②hessian

Hessian is a lightweight binary RPC protocol developed by the Caucho technology. It uses a custom serialization technology to deliver Java objects across the network. In addition to Java support, Hessian provides implementation support for other languages, such as Php,python,c++ and C #.

③burlap

Burlap is a lightweight XML-RPC protocol for implementing Web services. Like Hessian, it also uses a proprietary serialization mechanism to serialize Java objects. For more information on Hessian/burlap, we do not have much to do here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.