• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

javaRMI使用入门程序及配置

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 10次 | 进入软件测试论坛讨论

领测软件测试网 本例子包含了共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远程调用成功了



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网