`
gcgmh
  • 浏览: 355062 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java RMI program

阅读更多
应用的是朋友的代码:
package com.cmsz.common.rmiServer;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;

/**
 * ********************************************************* 
 * Copyright cmsz
 * 2007. All rights reserved.
 * 
 * @file: RmiServerImpl
 * @date: 2007-2-8
 * @brief:  远程调用server端接口
 * @author: tangqy
 * @version: 0.0.1
 * @since 1.0
 *  Change Log:
 *      <author>      <time>       <version>     <description>
 *      tangqy       2007-2-8       0.0.1          create
 * **********************************************************
 */
public interface RmiServerImpl extends Remote{
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   执行后台binary程序接口
	 * @param pm_sProcNm:程序名
	 * @param pm_sProcParams:程序参数
	 * @return 后台binary程序返回字符串
	 * @throws RemoteException
	 */
	public abstract String callStringProc(String pm_sProcNm, String pm_sProcParam)  throws RemoteException;
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   执行后台binary程序接口
	 * @param pm_sProcNm 程序名
	 * @param pm_sProcParams 程序参数
	 * @return List 第一个值为console输出,第二个值为process返回值
	 * @throws RemoteException
	 */
	public abstract ArrayList callProc(String pm_sProcNm,String pm_sProcParam) throws RemoteException;
	
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   执行后台binary程序接口
	 * @param pm_sProcNm 程序名
	 * @param pm_sProcParams 程序参数数组
	 * @return 后台binary程序返回字符串
	 * @throws RemoteException
	 */
	public abstract String callStringProc(String pm_sProcNm, String[] pm_sProcParams)  throws RemoteException;
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   执行后台binary程序接口
	 * @param pm_sProcNm 程序名
	 * @param pm_sProcParams 程序参数数组
	 * @return ArrayList 第一个值为console输出,第二个值为程序returnCode
	 * @throws RemoteException
	 */
	public abstract ArrayList callProc(String pm_sProcNm,String[] pm_sProcParams) throws RemoteException;
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   执行后台Deamon程序接口
	 * @param pm_sProcNm 程序名
	 * @param pm_sProcParam 程序参数
	 * @throws RemoteException
	 */
	public abstract void callProcDeamon(String pm_sProcNm,String pm_sProcParam) throws RemoteException;
}



package com.cmsz.common.rmiServer;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

/**
 * ********************************************************* 
 * Copyright cmsz
 * 2007. All rights reserved.
 * 
 * @file: RmiServerObject
 * @date: 2007-2-8
 * @brief:  远程服务端实现类
 * @author: tangqy
 * @version: 0.0.1
 * @since 1.0
 *  Change Log:
 *      <author>      <time>       <version>     <description>
 *      tangqy       2007-2-8       0.0.1          create
 * **********************************************************
 */
public class RmiServerObject extends UnicastRemoteObject implements RmiServerImpl
{
	public RmiServerObject() throws RemoteException
	{
		
	}
	/* (non-Javadoc)
	 * @see com.cmsz.common.util.RmiServerImpl#callStringProc(java.lang.String, java.lang.String)
	 */
	public String callStringProc(String pm_sProcNm, String pm_sProcParams) throws RemoteException {
		String returnValue = "";
		try 
		{
			Process process = Runtime.getRuntime().exec(pm_sProcNm + " " + pm_sProcParams);
			InputStream ins = process.getInputStream();
			StringBuffer strBuf = new StringBuffer("");
	        byte[] temp = new byte[1024];
	        while(ins.read(temp)!=-1)
	        {
	        	strBuf.append(new String(temp));
	        }
	        return strBuf.toString().trim();
		} 
		catch (IOException e) 
		{
			returnValue = e.getMessage();
			return returnValue;
		}
		
	}

	/* (non-Javadoc)
	 * @see com.cmsz.common.util.RmiServerImpl#callBoolProc(java.lang.String, java.lang.String)
	 */
	public ArrayList callProc(String pm_sProcNm, String pm_sProcParams) throws RemoteException {
		try
		{
			Process process = Runtime.getRuntime().exec(pm_sProcNm+" "+pm_sProcParams);
			int returnVal = process.waitFor();
			
			InputStream ins = process.getInputStream();
			StringBuffer buf = new StringBuffer("");
			ArrayList lsRet = new ArrayList();
			byte[] temp = new byte[1024];
	        while(ins.read(temp)!=-1)
	        {
	        	buf.append(new String(temp));
	        }	        
	        lsRet.add(buf.toString());
	        lsRet.add(new Integer(returnVal));
			return lsRet;
		}
		catch(Exception es)
		{
			System.out.println("Runtime异常!"+es.getMessage());
			return null;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cmsz.common.util.RmiServerImpl#callStringProc(java.lang.String, java.lang.String[])
	 */
	public String callStringProc(String pm_sProcNm, String[] pm_sProcParams) throws RemoteException {
		String returnValue = "";
		try 
		{
			Process process = Runtime.getRuntime().exec(pm_sProcNm,pm_sProcParams);
			InputStream ins = process.getInputStream();
			StringBuffer strBuf = new StringBuffer("");
	        byte[] temp = new byte[1024];
	        while(ins.read(temp)!=-1)
	        {
	        	strBuf.append(temp);
	        }
	        return strBuf.toString().trim();
		} 
		catch (IOException e) 
		{
			returnValue = e.getMessage();
			return returnValue;
		}
		
	}
	/* (non-Javadoc)
	 * @see com.cmsz.common.util.RmiServerImpl#callBoolProc(java.lang.String, java.lang.String[])
	 */
	public ArrayList callProc(String pm_sProcNm, String[] pm_sProcParams) throws RemoteException {
		try
		{
			Process process = Runtime.getRuntime().exec(pm_sProcNm,pm_sProcParams);
			int returnVal = process.waitFor();
			
			InputStream ins = process.getInputStream();
			StringBuffer buf = new StringBuffer("");
			ArrayList lsRet = new ArrayList();
			byte[] temp = new byte[1024];
	        while(ins.read(temp)!=-1)
	        {
	        	buf.append(new String(temp));
	        }
	        lsRet.add(buf.toString());
	        lsRet.add(new Integer(returnVal));
			return lsRet;
		}
		catch(Exception es)
		{
			System.out.println("Runtime异常!"+es.getMessage());
			return null;
		}
	}
	
	
	/* (non-Javadoc)
	 * @see com.cmsz.common.rmiServer.RmiServerImpl#callProcDeamon(java.lang.String, java.lang.String)
	 */
	public void callProcDeamon(String pm_sProcNm, String pm_sProcParam) throws RemoteException 
	{		
		try 
		{
			Process process = Runtime.getRuntime().exec(pm_sProcNm + " " + pm_sProcParam);
		} 
		catch (IOException e) 
		{
			System.out.println(e.getMessage());
		}
	} 
	
	

}



package com.cmsz.common.rmiServer;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.util.ArrayList;

import com.cmsz.common.rmiServer.RmiServerImpl;

/**
 * ********************************************************* 
 * Copyright cmsz
 * 2007. All rights reserved.
 * 
 * @file: RmiClient
 * @date: 2007-2-8
 * @brief:  远程调用客户端
 * @author: tangqy
 * @version: 0.0.1
 * @since 1.0
 *  Change Log:
 *      <author>      <time>       <version>     <description>
 *      tangqy       2007-2-8       0.0.1          create
 * **********************************************************
 */
public class RmiClient {
	
	public RmiClient()
	{
		if(System.getSecurityManager() == null)
		{
			System.setSecurityManager(new RMISecurityManager());
		}	
	}
	
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   远程调用
	 * @param pm_sRmiHost RMIserver端主机名
	 * @param pm_iRmiPort RMIserver端端口号
	 * @param pm_sProcNm 远程程序名
	 * @param pm_sParams 远程调用程序参数
	 * @return
	 */
	public String callRemoteProcStr(String pm_sRmiHost,int pm_iRmiPort,String pm_sProcNm,String pm_sParams)
	{			
		try
		{
			RmiServerImpl rmiObject = (RmiServerImpl)Naming.lookup(pm_sRmiHost+":"+pm_iRmiPort+"/RemoteRMI");
			return rmiObject.callStringProc(pm_sProcNm,pm_sParams);
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			return e.getMessage();
		} 
		catch (RemoteException e)
		{
			e.printStackTrace();
			return e.getMessage();
		} 
		catch (NotBoundException e) 
		{
			e.printStackTrace();
			return e.getMessage();
		}
		
	}
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   远程调用
	 * @param pm_sRmiHost RMIserver端主机名
	 * @param pm_iRmiPort RMIserver端端口号
	 * @param pm_sProcNm 远程程序名
	 * @param pm_sParams 远程调用程序参数
	 * @return
	 */
	public ArrayList callRemoteProcInt(String pm_sRmiHost,int pm_iRmiPort,String pm_sProcNm,String pm_sParams)
	{
		try
		{
			RmiServerImpl rmiObject = (RmiServerImpl)Naming.lookup(pm_sRmiHost+":"+pm_iRmiPort+"/"+"RemoteRMI");
			return rmiObject.callProc(pm_sProcNm,pm_sParams);
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			return null;
		} 
		catch (RemoteException e)
		{
			e.printStackTrace();
			return null;
		} 
		catch (NotBoundException e) 
		{
			e.printStackTrace();
			return null;
		}
		
	}

	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   远程调用
	 * @param pm_sRmiHost RMIserver端主机名
	 * @param pm_iRmiPort RMIserver端端口号
	 * @param pm_sProcNm 远程程序名
	 * @param pm_sParams 远程调用程序参数
	 * @return
	 */
	public ArrayList callRemoteProcInt(String pm_sRmiHost,int pm_iRmiPort,String pm_sProcNm,String[] pm_sParams,ArrayList pm_lsRet)
	{
		try
		{
			RmiServerImpl rmiObject = (RmiServerImpl)Naming.lookup(pm_sRmiHost+":"+pm_iRmiPort+"/RemoteRMI");
			return rmiObject.callProc(pm_sProcNm,pm_sParams);
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			return null;
		} 
		catch (RemoteException e)
		{
			e.printStackTrace();
			return null;
		} 
		catch (NotBoundException e) 
		{
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	/**
	 * author tangqy
	 * 2007-2-8
	 * TODO   远程调用
	 * @param pm_sRmiHost RMIserver端主机名
	 * @param pm_iRmiPort RMIserver端端口号
	 * @param pm_sProcNm 远程程序名
	 * @param pm_sParams 远程调用程序参数
	 * @return
	 */
	public String callRemoteProcStr(String pm_sRmiHost,int pm_iRmiPort,String pm_sProcNm,String[] pm_sParams)
	{
		try
		{
			RmiServerImpl rmiObject = (RmiServerImpl)Naming.lookup(pm_sRmiHost+":"+pm_iRmiPort+"/RemoteRMI");
			return rmiObject.callStringProc(pm_sProcNm,pm_sParams);
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			return e.getMessage();
		} 
		catch (RemoteException e)
		{
			e.printStackTrace();
			return e.getMessage();
		} 
		catch (NotBoundException e) 
		{
			e.printStackTrace();
			return e.getMessage();
		}
		
	}
	
	public void callRemoteProcDeamon(String pm_sRmiHost,int pm_iRmiPort,String pm_sProcNm,String pm_sParams)
	{
		try
		{
			RmiServerImpl rmiObject = (RmiServerImpl)Naming.lookup(pm_sRmiHost+":"+pm_iRmiPort+"/RemoteRMI");
			rmiObject.callProcDeamon(pm_sProcNm,pm_sParams);
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();			
		} 
		catch (RemoteException e)
		{
			e.printStackTrace();			
		} 
		catch (NotBoundException e) 
		{
			e.printStackTrace();			
		}
	}
	
	public static void main(String[] args)
	{
		RmiClient client = new RmiClient();
		client.callRemoteProcDeamon("//132.32.20.133",1099,"/opt/mcb/tts/bin/PreMainMon.sh -start"," ");
	}

}



package com.cmsz.common.rmiServer;

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

/**
 * ********************************************************* 
 * Copyright cmsz
 * 2007. All rights reserved.
 * 
 * @file: RmiServerMain
 * @date: 2007-2-8
 * @brief:  启动远程调用服务端
 * @author: tangqy
 * @version: 0.0.1
 * @since 1.0
 *  Change Log:
 *      <author>      <time>       <version>     <description>
 *      tangqy       2007-2-8       0.0.1          create
 * **********************************************************
 */
public class RmiServerMain {
	
	public static void main(String[] args)
	{
		try
		{
			if(System.getSecurityManager() == null)
			{
				System.setSecurityManager(new RMISecurityManager());
			}
			int rmiPort = 1099;
			if(args.length!=0)
			{
				rmiPort = Integer.parseInt(args[0]);
			}
			LocateRegistry.createRegistry(rmiPort);			
			RmiServerObject rmiObject = new RmiServerObject();
			Naming.rebind("//localhost:"+rmiPort+"/RemoteRMI", rmiObject);						
			System.out.println("Remote Object is Ready");
		}
		catch(Exception exception)
		{
			exception.printStackTrace();
		}
	}
	
}

分享到:
评论

相关推荐

    rmi.rar_The Program

    【标题】:“rmi.rar_The ...总的来说,“rmi.rar_The Program”不仅提供了阿拉伯数字和罗马数字转换的实用功能,还展现了如何在Java环境中利用RMI实现跨JVM的分布式服务调用,对于学习和理解RMI机制有着重要的价值。

    java 远程方法调用

    Java 远程方法调用(Remote Method Invocation,RMI)是一种在分布式环境中执行对象方法的技术。它允许一个Java应用程序调用运行在不同JVM(Java虚拟机)上的另一个对象的方法,就像是在本地调用一样。在Java RMI中...

    实验2 RMI.doc

    - 打开 `C:\Program Files\Java\jre\lib\security\java.policy` 文件,在末尾添加以下代码: ```plaintext grant { permission java.net.SocketPermission "*:1024-65535", "connect,accept"; permission java...

    JAVA_RMI_Win.zip_The Bat_rmi

    This application was written using RMI Java. It resolve "access to critical section " example problem. To use this program you should change in *.bat / *.sh files the java path according in your ...

    RMI.zip_remote

    标题"RMI.zip_remote"表明这是一个与RMI相关的代码示例压缩包,而描述中的"Remote Method Invocation Program code"进一步确认了这一点。接下来,我们将深入探讨RMI的关键概念和工作原理,并基于提供的文件名称...

    RMI Project

    使用RMI远程调用java程序,在MyEclipse2014测试成功。其中服务器端添加该Project中的RemotingService和Service,在客户端端添加RemotingClient和Service。先运行RemotingService中的Program再运行客户端中...

    Java高级教程PDF

    JavaServer Pages (JSP), servlets, Remote Method Invocation (RMI) and Enterprise JavaBeans (EJB) into a production-quality system that allows developers to benefit from the leverage and platform ...

    习题解答-Java面向对象程序设计-邢国波-清华大学出版社.pdf

    - **分布性**:Java支持RMI(远程方法调用)技术,便于开发分布式应用。 - **简单性**:与C/C++相比,Java简化了许多编程细节,如不使用指针。 2. **Java程序的生命周期**: - **编译**:使用`javac`命令将.java...

    Java名词解释

    JSA(Java Security Application Program Interface)是Java安全应用编程接口,用于实现安全策略和管理。 以上仅是Java及相关技术的一部分核心概念,它们共同构成了Java丰富的生态系统,为开发者提供了强大且灵活的...

    RMI使用学习 小结

    远程方法调用(Remote Method Invocation,简称RMI)是一种Java技术,允许开发者在不同的JVM(Java虚拟机)之间通过网络进行对象间的通信。它使得在一台机器上的Java对象能够调用另一台机器上Java对象的方法,就像是...

    <Head First Java> 中文版

    此外,书中还涵盖了数值与静态变量、异常处理、图形用户界面(GUI)、文件输入/输出、网络编程、线程、集合与泛型、包、JAR文件部署以及远程方法调用(RMI)等高级主题,这些都是Java开发中不可或缺的知识点。...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    A Simple Java Program 36 Comments 39 Data Types 40 Variables 44 Operators 46 Strings 53 Input and Output 63 Control Flow 71 Big Numbers 88 Arrays 90 Chapter 4: Objects and Classes 105 ...

    Java Network Programming 3rd Edition By Elliotte Rusty Harold 2004

    The java.rmi.registry Package Section 18.6. The java.rmi.server Package Chapter 19. The JavaMail API Section 19.1. What Is the JavaMail API? Section 19.2. Sending Email Section 19.3. ...

    阿里内部资料--Java开发杂项

    -vm C:\Program Files\Java\jdk1.6.0_18\bin\javaw.exe --launcher.XXMaxPermSize 256m -vmargs -Xms40m -Xmx1024m ``` 这些参数分别设置了Eclipse使用的JVM路径、最大永久代大小以及最小和最大堆大小。 #### 六、...

    Java_Corba编程技术.pdf

    java -Djava.security.policy=security.policy -Djava.rmi.server.hostname=127.0.0.1 -Djava.security.manager -jar CorbaDemoServer.jar ``` 此批处理文件用于启动服务器端程序。通过设置`java.security.policy`...

    DistributedVideoConversor:该项目使您能够在您配置的服务器上转换视频。 目标是在具有更高硬件要求的计算机中转换视频,从而在更少的时间内转换视频。 在此项目中,使用了Java RMI技术

    * The syntax to call this program is presented in next description: * * 1º param: command to be executed in the server. The program have 3: * * send * * receive * * convert * * 2º parametro: server'...

    [清华大学]JAVA教程

    课程主要包括下列内容:  ■ Java语言的发展及相关技术的介绍,Java技术和平台在网络计算及电子商务中的应用介绍;  ■ Java语言的基础知识:Java语言的主要特点,设计思想,Java虚拟机,... ■ JavaBeans和RMI。

    Advanced JAVA Networking

    The same application (for a simple appointment-scheduling program) is used throughout the text. You learn how to use sockets, RMI, IDL/CORBA, and even servlets and JSPs to build different versions of...

    C院Java常用单词

    11. **RMI (Remote Method Invocation)**:RMI允许Java对象在不同的Java虚拟机之间进行通信,调用远程对象的方法。 12. **CVS (Concurrent Versions System)**:CVS是一种版本控制系统,用于跟踪和管理源代码的更改...

Global site tag (gtag.js) - Google Analytics