Simple demo of spring RMI

Source: Internet
Author: User
(1) define the interface: Java code
 
  1. Package com. logcd. Spring. RMI;
  2. Public interface helloservice {
  3. Public String dohello (string name );
  4. }
Package com. logcd. Spring. RMI; public interface helloservice {Public String dohello (string name );}

(2) interface implementation: Java code
 

  1. Package com. logcd. Spring. RMI;
  2. Public class helloserviceimpl implements helloservice {
  3. Public String dohello (string name ){
  4. Return "hello," + name;
  5. }
  6. }
Package com. logcd. Spring. RMI; public class helloserviceimpl implements helloservice {Public String dohello (string name) {return "hello," + name ;}}

(3) rmi-server.xml Java code
 

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype beans public "-// spring/DTD bean/EN"
  3. Http://www.springframework.org/dtd/spring-beans.dtd>
  4. <Beans>
  5. <Bean id = "helloservice" class = "com. logcd. Spring. RMI. helloserviceimpl"/>
  6. <! -- Rmiserviceexporter displays support for exposing any non-RMI service using the RMI caller -->
  7. <Bean id = "serviceexporter"
  8. Class = "org. springframework. remoting. RMI. rmiserviceexporter">
  9. <Property name = "service" ref = "helloservice"/>
  10. <Property name = "serviceinterface"
  11. Value = "com. logcd. Spring. RMI. helloservice"/>
  12. <! -- Defines that the name of the service to be exposed can be different from that of the output bean. The client uses this name to call the service. -->
  13. <Property name = "servicename" value = "helloservice"/>
  14. <! -- Overwrite the RMI registration port number (1099). Generally, the application server also maintains RMI registration, so it is best not to conflict. -->
  15. <Property name = "registryport" value = "1199" type = "regxph" text = "yourobjectname"/>
  16. </Bean>
  17. </Beans>
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype beans public "-// spring/DTD bean/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id = "helloservice" class = "com. logcd. spring. RMI. helloserviceimpl "/> <! -- Rmiserviceexporter displays support for exposing any non-RMI service using the RMI caller --> <bean id = "serviceexporter" class = "org. springframework. remoting. RMI. rmiserviceexporter "> <property name =" service "ref =" helloservice "/> <property name =" serviceinterface "value =" com. logcd. spring. RMI. helloservice "/> <! -- Defines the name of the service to be exposed and the name of the output bean. The client calls the service through this name --> <property name = "servicename" value = "helloservice"/> <! -- Override the RMI registration port number (1099). Generally, the application server also maintains RMI registration, it is best not to conflict --> <property name = "registryport" value = "1199"/> </bean> </beans>

(4) rmi-client.xml Java code
 

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype beans public "-// spring/DTD bean/EN"
  3. Http://www.springframework.org/dtd/spring-beans.dtd>
  4. <Beans>
  5. <! -- Use rmiproxyfactorybean to connect to the server -->
  6. <Bean id = "serviceproxy"
  7. Class = "org. springframework. remoting. RMI. rmiproxyfactorybean">
  8. <Property name = "serviceurl"
  9. Value = "RMI: // localhost: 1199/helloservice"/>
  10. <Property name = "serviceinterface"
  11. Value = "com. logcd. Spring. RMI. helloservice"/>
  12. </Bean>
  13. </Beans>
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype beans public "-// spring/DTD bean/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <! -- Use rmiproxyfactorybean to connect to the server --> <bean id = "serviceproxy" class = "org. springframework. remoting. RMI. rmiproxyfactorybean "> <property name =" serviceurl "value =" RMI: // localhost: 1199/helloservice "/> <property name =" serviceinterface "value =" com. logcd. spring. RMI. helloservice "/> </bean> </beans>

(5) test Java code
 

  1. Package com. logcd. Spring. RMI;
  2. Import org. springframework. Context. applicationcontext;
  3. Import org. springframework. Context. Support. classpathxmlapplicationcontext;
  4. Public class testspringrmi {
  5. Public static void main (string ARGs []) {
  6. Applicationcontext context = new classpathxmlapplicationcontext (
  7. New String [] {"rmi-server.xml", "rmi-client.xml "});
  8. Helloservice service = (helloservice) Context. getbean ("serviceproxy ");
  9. System. Out. println (service. dohello ("logcd "));
  10. }
  11. }
Package COM. logcd. spring. RMI; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; public class testspringrmi {public static void main (string ARGs []) {applicationcontext context = new classpathxmlapplicationcontext (New String [] {"rmi-server.xml", "rmi-client.xml "}); helloservice service = (helloservice) context. getbean ("serviceproxy"); system. out. println (service. dohello ("logcd "));}}
 
From: http://log-cd.iteye.com/blog/213908

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.