java rmi两种实现

原生java rmi

IHello接口

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IHello extends Remote {
	public String hellWorld() throws RemoteException;
	
	public String sayHelloToSomeBody(String someBodyName)throws RemoteException;
}


HelloImpl实现类
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements IHello {

	private static final long serialVersionUID = 1L;
	
	protected HelloImpl() throws RemoteException {
		super();
	}

	public String hellWorld() throws RemoteException {
		return "Hello World!";
	}

	public String sayHelloToSomeBody(String someBodyName) throws RemoteException {
		
		return "你好," + someBodyName + "!";
	}
}

服务端

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class HelloServer {

	public static void main(String[] args) {
		
		try {
			IHello rhello = new HelloImpl();
			LocateRegistry.createRegistry(8888);
			Naming.bind("rmi://localhost:8888/RHello", rhello);
			System.out.println(">>>>>INFO:远程IHello对象绑定成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}

客户端

import java.rmi.Naming;

public class HelloClient {

	public static void main(String[] args) {
		try{
			IHello rhello = (IHello) Naming.lookup("rmi://localhost:8888/RHello");
			System.out.println(rhello.hellWorld());
			System.out.println(rhello.sayHelloToSomeBody("朱志云"));
		}catch(Exception e){
			e.printStackTrace();
		}

	}

}

spring rmi

IHello接口

public interface IHello  {
	public String hellWorld();
	
	public String sayHelloToSomeBody(String someBodyName);
}

HeloImpl实现类

public class HelloImpl implements IHello {

	protected HelloImpl(){
	}

	public String hellWorld(){
		
		return "Hello World!";
	}

	public String sayHelloToSomeBody(String someBodyName){
		
		return "你好," + someBodyName + "!";
	}

}

服务端

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloHost {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi_server_context.xml");
		System.out.println("RMI服务伴随Spring的启动而启动了.....");
	}
}


客户端
import java.rmi.RemoteException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloClient {

	public static void main(String[] args) throws RemoteException {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi_client_context.xml");
		IHello hs = (IHello) ctx.getBean("hello");
		System.out.println(hs.hellWorld());
		System.out.println(hs.sayHelloToSomeBody("美女"));
	}
}


服务端spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

	<bean id="hello" class="com.zzy.test.test_spring_rmi.HelloImpl" />

	<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
		<property name="service" ref="hello" />
		<!-- 定义服务名 -->
		<property name="serviceName" value="hello" />
		<property name="serviceInterface" value="com.zzy.test.test_spring_rmi.IHello" />
		<property name="registryPort" value="8088" />
	</bean>

</beans>


客户端spring配置文件

<?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-2.0.xsd">
	<bean id="hello" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceUrl" value="rmi://localhost:8088/hello" />
		<property name="serviceInterface" value="com.zzy.test.test_spring_rmi.IHello" />
	</bean>
</beans>

项目POM配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zzy.test</groupId>
  <artifactId>test-spring-rmi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>test-spring-rmi</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <springframework.version>4.2.1.RELEASE</springframework.version>
  </properties>

  <dependencies>
	<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${springframework.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
		<version>${springframework.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${springframework.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
		<version>${springframework.version}</version>
	</dependency>
  </dependencies>
</project>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值