Java--spring and RMI integration for Remote access (episode)

Source: Internet
Author: User
Tags log4j

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45972095

Using spring's support for RMI makes it easy to build your distributed applications. On the server side, you can expose your service through spring's Org.springframework.remoting.rmi.RmiServiceExporter , in the client, It is very convenient to use the service exposed by Org.springframework.remoting.rmi.RmiProxyFactoryBean. This access to the C/S model can mask the complexity of the RMI itself, such as the processing details of the server skeleton and client stubs, which are transparent to the people who use the service development and service, without undue attention, and focus on developing your business logic.

Here's an example of how to integrate RMI through spring.

Service-Side Publishing Service

We define a service interface that implements the service interface to complete its complex logic, through which the client can invoke services exposed by the server, as follows:

Package org.shirdrn.spring.remote.rmi;/** * @author Liuyazhuang */public interface Accountservice {int querybalance ( String Mobileno); String Shoopingpayment (String Mobileno, byte protocol);
The service implementation, as shown below:

Package Org.shirdrn.spring.remote.rmi;import org.apache.log4j.logger;/** * @author Liuyazhuang */public class Mobileaccountserviceimpl implements Accountservice {private static final Logger LOG = Logger.getlogger ( Mobileaccountserviceimpl.class);p ublic int querybalance (String mobileno) {if (Mobileno! = null) return 100;return 0;} public string Shoopingpayment (string Mobileno, byte protocol) {StringBuffer SB = new StringBuffer (). Append ("Your Mobile Nu Mber is/""). Append (Mobileno). Append ("/", protocol type is/""). Append (Protocol). Append ("/". "); Log.info ("Message is:" + sb.tostring ()); return sb.tostring ();}}
The server-side Publishing service is for the client to make (remote method) calls, and the spring configuration Server.xml is as follows:

<?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:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd "><bean id=" Serviceexporter "class=" Org.springframework.remoting.rmi.RmiServiceExporter "><property name=" ServiceName "value=" Mobileaccountservice "/><property name=" service "ref=" Accountservice "/><property name=" Serviceinterface "value=" Org.shirdrn.spring.remote.rmi.AccountService "/><property name=" Registryport "value = "8080"/><property name= "Serviceport" value= "8088"/></bean><bean id= "AccountService" class= " Org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl "/></beans>

The above configuration specifies the name of the exposed service, injected into the rmiserviceexporter by the ServiceName attribute, and the service name is Mobileaccountservice, which the client can invoke through the service name.

Start the service side below and publish the service as follows:

Package Org.shirdrn.spring.remote.rmi;import Org.springframework.context.support.ClassPathXmlApplicationContext ;/** * @author Liuyazhuang */public class Rmiserver {public static void main (string[] args) throws Interruptedexception {n EW Classpathxmlapplicationcontext ("Org/shirdrn/spring/remote/rmi/server.xml"); object lock = new Object (); Synchronized (lock) {lock.wait ();}}}

Client Invoke Service

The client configuration Client.xml is as follows:

<?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:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd "><bean id=" Mobileaccountservice "class=" Org.springframework.remoting.rmi.RmiProxyFactoryBean "><property name=" serviceurl "value=" rmi:// 192.168.1.103:8080/mobileaccountservice "/><property name=" Serviceinterface "value=" Org.shirdrn.spring.remote.rmi.AccountService "/></bean></beans>
Configuration, a remote method call can be made by injecting a serviceurl and serviceinterface into the Rmiproxyfactorybean. The invocation example looks like this:

Package Org.shirdrn.spring.remote.rmi;import Org.apache.log4j.logger;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * @author Liuyazhuang */public class rmiclient {private static final Logger LOG = Logger.getlogger (rmiclient.class);p ublic static void Main (string[] args) {App Licationcontext CTX = new Classpathxmlapplicationcontext ("Org/shirdrn/spring/remote/rmi/client.xml"); Accountservice Accountservice = (accountservice) ctx.getbean ("Mobileaccountservice"); String result = Accountservice.shoopingpayment ("13800138000", (byte) 5); Log.info (result);}}
Visibility makes remote access easy



Java--spring and RMI integration for Remote access (episode)

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.