• 类对象RMI的简单实现


    发一下牢骚和主题无关:

        第一次用使RMI实现java分布式,利用一个单简的例子停止试测。

        首先要需一个实现了Remote的接口,这个接口供提近程对象的法方集

        这个接口如下:

    package com.hello;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.util.Date;
    
    public interface HelloServer extends Remote 
    {
    	public String echo(String msg)throws RemoteException;
    	public Date getTime()throws RemoteException;
    }

        在客户端和务服器上都应该有这样一个接口,只是可以用使的务服。

        然后再务服端要需一个实现了HelloServer的类,并使其成为够能供提近程务服的近程对象,这里通过继承UnicastRemoteObject使其出导近程对象:

    package com.hello;
    
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.Date;
    
    public class HelloServerImpl extends UnicastRemoteObject implements HelloServer
    {
    	private String name;
    	protected HelloServerImpl(String name) throws RemoteException 
    	{
    		this.name=name;
    	}
    	public String echo(String msg) throws RemoteException
    	{
    		System.out.println("用调了echo()法方");
    
    		return "echo "+msg+" from "+name;
    	}
    
    	public Date getTime() throws RemoteException 
    	{
    
    		System.out.println("用调了getTime()法方");
    		return new Date();
    	}
    
    }

        接上去编写务服端程序

    package com.hello;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    public class SimpleServer 
    {
    	public static void main(String[]args)
    	{
    		try
    		{
    			HelloServer service1=new HelloServerImpl("service1");
    			HelloServer service2=new HelloServerImpl("service2");
    			
    			Context namingContext=new InitialContext();
    			
    			namingContext.rebind("rmi://localhost:1099/HelloServer1",service1);
    			namingContext.rebind("rmi://localhost:1099/HelloServer2",service2);
    			System.out.println("注册了两个对象");
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }

        接上去编写客户端程序

        每日一道理
    “一年之计在于春”,十几岁的年纪,正是人生的春天,别辜负了岁月老人的厚爱与恩赐。行动起来,播种梦想吧!
    package com.hello;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NameClassPair;
    import javax.naming.NamingEnumeration;
    
    public class SimpleClient 
    {
    	public static void main(String[]args)
    	{
    		String url="rmi://localhost:1099/";
    		try
    		{
    			Context namingContext=new InitialContext();
    			//取得近程对象的存根对象
    			HelloServer service1=(HelloServer)namingContext.lookup(url+"HelloServer1");
    			HelloServer service2=(HelloServer)namingContext.lookup(url+"HelloServer2");
    			Class stubClass=service1.getClass();
    			//试测存根类所属的类
    			System.out.println("service1是 "+stubClass.getName());
    			//试测存根类实现的接口
    			Class[] interfaces=stubClass.getInterfaces();
    			for(int i=0;i<interfaces.length;i++)
    			{
    				System.out.println("存根类实现了"+interfaces[i].getName());
    			}
    			System.out.println(service1.echo("hello"));
    			System.out.println(service1.getTime());
    			System.out.println(service2.echo("hello"));
    			System.out.println(service2.getTime());
    			NamingEnumeration<NameClassPair> e=namingContext.list("rmi:");
    			while(e.hasMore())
    			{
    				System.out.println(e.next().getName());
    			}
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    }

        在行运程序之前要需进入到HelloServer的bin目录下开启rmiregistry

        命令:start rmiregistry

        如果没有错误,就会弹出新的黑框;

        接上去客户端还要需一个存根类,为了取得它要需在务服端的bin目录下用如下命令:

        rmic com.hello.SimpleServer

        这个命令注意:SimpleServer是完全的类名,并且没有.class后缀

        如果功成的话会生产一个HelloServerImpl_Stub.class,将其与客户端class文件放在一同。

        上去就够能行运程序了。

        首先行运务服端,行运结果如下:

        类和对象

        然后行运客户端,行运结果如下:

        类和对象

        而此时的务服端态状如下:

        类和对象

        

        说明客户端对近程对象的法方用调其执行进程确实是在务服端停止的。

        

        

    文章结束给大家分享下程序员的一些笑话语录: 人在天涯钻,哪儿能不挨砖?日啖板砖三百颗,不辞长做天涯人~

  • 相关阅读:
    操作系统学习(一)、80x86保护模式内存管理
    Linux命令(十三) 建立目录 mkdir 删除目录 rmdir
    Linux命令(十二) 分割文件 split 合并文件 join
    Linux命令(十一) 显示文件类型 file
    linux下环境变量PS1设置
    Jenkins email-ext邮件通知模板
    building system busy, pls wait !!
    SCP 命令
    NDK Build 用法(NDK Build)
    android下m、mm、mmm编译命令的使用
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3063385.html
Copyright © 2020-2023  润新知