javaRMI使用入门程序及配置

发表于:2007-07-04来源:作者:点击数: 标签:
本例子包含了共5个文件,分别是HelloClient,Hello,HelloServer,Configur,config.properties -------------------------------------------------------------------------------- /*Hello.java*/ /* * Created on 2004-11-10 * */ package test.rmi; impo
本例子包含了共5个文件,分别是HelloClient,Hello,HelloServer,Configur,config.properties




--------------------------------------------------------------------------------


/*Hello.java*/
/*
* Created on 2004-11-10
*
*/
package test.rmi;

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

/**
* @author dogcome
*
* <p>远程方法调用接口,定义远程调用方法</p>
*/
public interface Hello extends Remote {

String sayHello() throws RemoteException;

}




--------------------------------------------------------------------------------


/*HelloServer*/
/*
* Created on 2004-11-10
*
*/
package test.rmi;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
* @author dogcome
*
*<p>服务器端,实现了Hello接口中的方法,用于实现远程调用方法的具体业务逻辑</p>
*/
public class HelloServer extends UnicastRemoteObject implements Hello {
String name;
public HelloServer(String s) throws RemoteException {
super();
name=s;
}

public String sayHello() throws RemoteException {
// TODO Auto-generated method stub
return "Hello world!";
}

public static void main(String[] args) {
/**下面这句话若要加上,则需要进行权限的认证,即增加.policy文件
* 并且在命令行中使用如下格式
* java -Djava.security.policy=java.policy test.rmi.HelloServer
*/
/*System.setSecurityManager(new RMISecurityManager());*/
Registry registry = null;
try {
/**启动注册服务器,使用了这个语句就不再需要在命令行环境中
*启动registry服务了
*/
registry = LocateRegistry.getRegistry();
/* 若没有获得连接,则此句会抛出异常,后面在捕获后进行相关处理 */
registry.list();
System.out.println("Register the exist server!"); //$NON-NLS-1$
} catch (RemoteException re) {
try {
int port = Integer.parseInt(Configur
.getString("HelloServer.RegistryServerPort")); //$NON-NLS-1$
registry = LocateRegistry.createRegistry(port);
System.out.println("Create Registry Server!"); //$NON-NLS-1$
} catch (Exception e) {
e.printStackTrace();
}
}
try {
HelloServer helloServer = new HelloServer("Hello");
registry
.rebind(
Configur.getString("HelloServer.HelloServerName"), helloServer); //$NON-NLS-1$
System.out.println("HelloServer server start!"); //$NON-NLS-1$
} catch (Exception e) {
e.printStackTrace();
}
}
}




--------------------------------------------------------------------------------


/*HelloClient*/
/*
* Created on 2004-11-10
*
*/
package test.rmi;

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
* @author dogcome
*
* <p>客户端,需要注册服务器并使用jndi</p>
*/
public class HelloClient {

public static void main(String[] args) {
/**下面这句话若要加上,则需要进行权限的认证,即增加.policy文件
* 并且在命令行中使用如下格式
* java -Djava.security.policy=java.policy test.rmi.HelloServer
*/
/*System.setSecurityManager(new RMISecurityManager());*/
try {
/*注册服务器*/
String hostName=Configur.getString("HelloServer.RegistryServerName");
int port=Integer.parseInt(Configur.getString("HelloServer.RegistryServerPort"));
Registry registry=LocateRegistry.getRegistry(hostName,port);

Hello hello=(Hello)registry.lookup(Configur.getString("HelloServer.HelloServerName"));
String message=hello.sayHello();
System.out.println(message);

}catch(Exception e) {
e.printStackTrace();
}
}
}




--------------------------------------------------------------------------------


/*
* Created on 2004-11-12
*
*/
package test.rmi;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
* @author dogcome
*
* <p>获取系统运行所需要的配置信息,对应的配置文件名称为config.properties</p>
*/
public class Configur {
private static final String BUNDLE_NAME = "test.rmi.config";

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);

private Configur() {
}

/**
* <p>通过key名称获得配置文件的相关信息</p>
* @param key key名称
* @return String 配置文件信息
*/
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return ''!'' + key + ''!'';
}
}
}



--------------------------------------------------------------------------------
#config.properties
HelloServer.RegistryServerPort=1111
HelloServer.HelloServerName=HelloServer
HelloServer.RegistryServerName=127.0.0.1



--------------------------------------------------------------------------------


首先使用rmic test.rmi.HelloServer命令生成需要的stub及skel两个类文件
然后运行HelloServer服务器 java test.rmi.HelloServer
最后运行HelloClient客户端 java test.rmi.HelloClient
屏幕输出HelloWorld,这样,一个最简单的rmi远程调用成功了



原文转自:http://www.ltesting.net