Spring Remote Access

Source: Internet
Author: User
Tags http authentication

Preface

For Java EE applications that do not apply to ejbs, RMI (Remote Method invocation, Java remote method call) is a common method for building distributed applications ), the Java object running on one JVM can be accessed by another JVM.
RMI is quite complex, and the details of the underlying RMI are hidden from the EJB.
Spring provides a comprehensive set of features for spring remoting to simplify the creation of distributed applications.

Spring remote access support:
RMI: RMI is part of j2se and used to create distributed applications. Spring's support for RMI can reduce the amount of code that exposes and accesses the RMI service. It also helps us deal with most of the red lines in RMI, such as RemoteException processing. Spring also integrates RMI and JNDI, which is very useful for the publication and access of the CORBA service.
JAX-RPC: "Java API for XML-based Remote Procedure CILS", which provides standard Java APIs for accessing and exposing RPC-style soap Web Services. Spring provides support classes to simplify the creation of JAX-RPC client applications and servlet-based service endpoints. Like many Java XML APIs, JAX-RPC has many implementations, the most popular of which is Apache axis, a soap stack fully compatible with JAX-RPC. The JAX-RPC examples in this chapter are implemented using axis.
JAX-WS: JAX-WS 2.0 is the successor of JAX-RPC 1.1. It provides APIs for publishing and accessing the soap 1.2web service and creating client applications. The jax0ws example in this chapter will be implemented using xfire.
HTTP invoker: the HTTP invoker architecture is spring's local remote architecture. It uses standard Java serialization and HTTP to provide a simple solution for the construction of Remote components. HTTP invoker uses the server-side servlet container to store remote services. The advantage of doing so is that the HTTP authentication method can be used to enhance the security of remote services.
Hessian: Hessian is a binary Protocol created by Caucho to simplify the creation of web services. It is specific to any specific transmission method, but usually uses HTTP. Spring provides support classes to simplify the creation of Hessian services. It uses HTTP for transmission and provides proxy support so that users can access Hessian Services transparently.
Burlap: burlap is also created by Caucho. It is an XML-based protocol that supplements Hessian protocol. Apart from the protocol details, burlap is used in the same way as Hessian. Spring's support for burlap is similar to that for Hessian. In fact, when the two protocols need to be exchanged, the burlap class can easily replace the Hessian class.

Spring remoting Architecture

The core of spring remoting architecture is the concept of a service exporter and a proxy generator.

Once a remote service is published, you need to create a client to access the service. This is usually one of the most complex ways to build distributed applications, because you need to pay close attention to the selected remote architecture.
With Spring, you can create a remote resource proxy through a proxy generator, so that you can use a simple Java interface to access the remote service.
This method not only reduces the complexity of client code (Spring handles remote architecture issues), but also applies to the selected remote architecture for decoupling (Spring hides all implementation details ).

Four of the five remote architectures supported by spring can use these two components. While JAX-RPC/JAX-WS does not have a service outputer, because the method of public services depends on the JAX-RPC/JAX-WS you are using. However, Spring provides the servletendpointsupport class to simplify the creation of a service endpoint in a JAX-RPC and expose it as a servlet.

Remote Access Call

Since Version 1.1, RMI has become a part of Java and is in the center of many remote solutions. The support of CORBA in Java is implemented through RMI, and EJB uses Rmi as the underlying mechanism of bean communication. JAX-RPC is built on the concept of RMI to expose Java objects as Web Services.

You can also use the Interface Definition Language (IDL) to implement the support of Java-based CORBA.
IDL is a declarative language that allows you to create objects that interact with each other-these objects may be created by different programming languages.
However, RMI is widely used in Java because of its better security and garbage collection capabilities.

1. Open any service
To build an Rmi service, you usually need to define an interface for the service, which inherits from the java. RMI. remote interface. Then your RMI service needs to implement this interface. It is better to inherit the java. RMI. server. unicastremoteobject class.
Spring remoting simplifies the above operations through the rmiserviceexporter class.

Demo:
We declare two beans: helloworldservice Bean (bean to be made public) and serviceexporter Bean (RMI service that exposes helloworldservice bean ).

Package CN. partner4java. remoting; public interface helloworld {Public String getmessage ();} package CN. partner4java. remoting; public class simplehelloworld implements helloworld {Public String getmessage () {return "Hello World" ;}}<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: Tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframe Work.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id =" helloworldservice "class =" CN. partner4java. remoting. simplehelloworld "/> <bean id =" serviceexporter "class =" org. springframework. remoting. RMI. rmiserviceexporter "> <! -- Service name --> <property name = "servicename" value = "helloworld"/> <property name = "service" ref = "helloworldservice"/> <property name = "serviceinterface" value = "CN. partner4java. remoting. helloworld "/> <! -- RMI registry port number --> <property name = "registryport" value = "9000"/> <! -- User communication service port. The default value is 0, indicating that an anonymous port number is used --> <property name = "serviceport" value = "9001"/> </bean> </beans>

2. Access the RMI service through a proxy
With the proxy generator, spring will generate the remote service proxy that implements the remote interface, so that you can interact with the remote service through the service interface, just like it is a local component. Spring hides all RMI details.
Just like all the proxy generators, the RMI proxy generator also implements the factorybean interface, so that we can create and configure the proxy through declaration, then, inject the dependency into the component.
Demo:

Package CN. partner4java. remoting. RMI; import CN. partner4java. remoting. helloworld; public class helloworldclient {private helloworld helloworldservice; Public void sethelloworldservice (helloworld helloworldservice) {This. helloworldservice = helloworldservice;} public void run () {system. out. println (helloworldservice. getmessage () ;}}<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: util = "http://www.springframework.org/schema/util" xmlns: Tx = "http://www.springframework.org/schema/tx" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemalocation = "http://www.springframework.org/schema/beansh Ttp: // consumer http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <bean id =" helloworldservice "class =" org. springframework. remoting. RMI. rmiproxyfactorybean "> <! -- Points to the proxy on the RMI service --> <property name = "serviceurl" value = "RMI: // localhost: 9000/helloworld"/> <! -- Tell the proxy generator which interface should be implemented for the interface --> <property name = "serviceinterface" value = "CN. partner4java. remoting. helloworld "/> </bean> <bean id =" helloworldclient "class =" CN. partner4java. remoting. RMI. helloworldclient "> <property name =" helloworldservice "ref =" helloworldservice "/> </bean> </beans>

3. Open the CORBA service
A great feature of RMI is that it allows you to publish services through the Internet Inter-ORB Protocol (IIOP), so that you can access these services using the CORBA components written in other languages. For the interaction between components written in different languages, CORBA is a popular solution. At the same time, many programming languages have extended its support.
Using RMI and using IIOP instead of the default Java remote method protocol (jrmp) to publish a Java service, you only need to do the following: generate the correct code pile and publish the service through the CORBA Object Request Broker (ORB. When jrmp is used to publish services, spring decouples your components from the RMI infrastructure without creating a pile. Although spring is responsible for registering the service with Orb when publishing the CORBA service, it is not so useful.
In fact, spring does not use classes to publish the CORBA service for simplified purposes. Instead, it uses JNDI to publish and search for remote services. It is most effective to use JNDI for service lookup when processing the establishment of CORBA, because the application can interact with orb through JNDI.

For more information about other services, see spring advanced programming.

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.