Spring RMI Example, springrmiexample

Source: Internet
Author: User

Spring RMI Example, springrmiexample

I. Remote End of the service

1-1. applicationContext. xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="      http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <bean id="userRmiServiceImpl" class="com.goodfan.rmi.service.impl.UserRmiServiceImpl" />    <bean id="userRmi" class="org.springframework.remoting.rmi.RmiServiceExporter">        <property name="service" ref="userRmiServiceImpl" />        <property name="serviceName" value="userRmi" />        <property name="serviceInterface" value="com.goodfan.rmi.service.UserRmiService" />        <property name="registryPort" value="9999" />    </bean></beans>  

1-2. Interface

package com.goodfan.rmi.service;public interface UserRmiService {     public String sayHello(User user);}

1-3. javabean

package com.goodfan.rmi.service;import java.io.Serializable;  public class User implements Serializable{        private static final long serialVersionUID = 8550373205815267923L;      private String userName;        public String getUserName() {          return userName;      }        public void setUserName(String userName) {          this.userName = userName;      }    } 

1-4. Implementation class

package com.goodfan.rmi.service.impl;import com.goodfan.rmi.service.User;import com.goodfan.rmi.service.UserRmiService;public class UserRmiServiceImpl implements UserRmiService {    @Override    public String sayHello(User user) {         return "Hello, " + user.getUserName();    }}

1-5. ServerTest class

package com.goodfan.rmi.service;import org.springframework.context.support.ClassPathXmlApplicationContext;  public class ServerTest {        public static void main(String[] args) {          System.setProperty("java.rmi.hostname", "10.7.3.12");           new ClassPathXmlApplicationContext("applicationContext.xml");          System.out.println("server start......");      }  }

 

Ii. Local call end

2-1. applicationContext-client

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="  http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">        <bean id="rmiProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">          <property name="serviceUrl" value="rmi://10.7.3.12:9999/userRmi"/>          <property name="ServiceInterface" value="com.goodfan.rmi.service.UserRmiService" />      </bean>  </beans> c

2-2. ClientTest class

package com.goodfan.rmi.service;import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;    public class ClientTest {        public static void main(String[] args) {          ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");          UserRmiService ums = (UserRmiService) ctx.getBean("rmiProxy");          User user = new User();          user.setUserName("RMI");          System.out.println(ums.sayHello(user));      }  } 

 

Related Article

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.