领略分布式编程乐趣--[4]

 


                     领略分布式编程乐趣

RedStar81 9/9/200313/9/2003
                                                                      
81_RedStar81@163.com
                                                                  TomHornson.student@www.sina.com.cn

首次修改时间:13/9/2003
            
个人文集:
http://www.csdn.net/develop/author/netauthor/RedStar81/
             WebLog: http://www.advogato.org/person/TomHornson/

 

   

2)访问远程对象

我们知道在RMI或是Remoting,使用分布式对象技术,你需要建立服务器端的远程对象,这个对象需要实现Remote接口并且继承于UnicastRemoteObject以获得分布式特性支持。而这一切在Voyager下变得简单了。Voyager在运行时生成一个类型兼容的代理,并且提供了几种方法做到这一点: a 创建一个本地对象,然后调用Proxy的静态方法Proxy.of(Object obj)来创建它的代理.  b 使用名字服务。Voyager提供名字服务功能。每个对象都可以通过将它们的名字服务登记到名字服务中,然后其它程序就可以通过Namespace.lookup(String Name)来得到这个对象的一个代理。 c 这个方法很有特色。在客户端你可以通过Factory.create(String classname,String url)在远程机器上建立对象,并且返回远程对象的代理。

下面我们就通过这几种方法实践一下Voyager环境下远程对象计算:

 

方法A示例:

程序片断1:

//

//Project : RemoteObject

//Filename : IBall.java

//Creator And Date : RedStar81 2003-8-22 22:51

//

//Statement :

//

 

public interface IBall{

  public void hit();

}

  

   //

//Project : RemoteObject

//Filename : Ball.java

//Creator And Date : RedStar81 2003-8-22 22:51

//

//Statement :

//

 

public class Ball implements IBall{

 

public void hit(){

    System.out.println("Ball has been hit");

}

}

 

//

//Project : RemoteObject

//FileName : Bat.java

//Creator And Date : RedStar81 2003-8-22 22:50

//

//Statement :

//

 

import com.objectspace.voyager.*;

 

public class Bat{

 

public void play(IBall iball){

    iball.hit();

}

 

public static void main(String[] args){

 

try{

    Voyager.startup();

   

    Bat bat = new Bat();

    IBall iball = (IBall)Namespace.lookup("8000/Ball");   

    bat.play(iball);   

}catch(Exception e){

       System.err.println(e);

}

    Voyager.shutdown();

}

 

}

 

//

//Project : RemoteObject

//Filename : BallMachine.java

//Creator And Date : RedStar81 2003-8-22 22:50

//

//Statement :

//

 

 

import com.objectspace.voyager.*;

 

public class BallMachine{

 

public static void main(String[] args){

 

try{

    Voyager.startup("8000");

    Ball ball = new Ball();

    IBall iball = (IBall)Proxy.of(ball);

    Namespace.bind("8000/Ball",iball);

}catch(Exception e){

       System.err.println(e);

}

 

}

}

上面的程序很简单,略知RMI或时Remoting,看起来一目了然,这里就不再讲解了.

 

 

程序片断二:下面是方法一复杂一点点的程序,这里客户端在调用远程对象的方法时,传递计算对象:

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : IComputing.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

public interface IComputing{

public Double computing(ComputingObject co);

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : Computing.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

 

public class Computing implements IComputing{

 

public Double computing(ComputingObject co){

   

    System.out.println("Computing is done on this machine !");   

    return co.workCode();

}

 

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : ComputingServer.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

import java.io.*;

 

public class ComputingObject implements Serializable {

//

//Serializable : need !

//

private double sum = 0;

private int dtStart,dtEnd;

 

public ComputingObject(int dataStart,int dataEnd){

    dtStart = dataStart;

    dtEnd = dataEnd;

}

   

public Double workCode()

{

       for(int control = dtStart ; control < dtEnd ; control++)

              sum += Math.sqrt(control);

       return new Double(sum);

}

 

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : ComputingServer.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

import com.objectspace.voyager.*;

 

public class ComputingServer{

 

public static void main(String[] args)

{

      

try{

    Voyager.startup("8000");

    Computing computingObject = new Computing();

    IComputing iComputing = (IComputing)Proxy.of(computingObject);

    Namespace.bind("8000/Computing",iComputing);

}catch(Exception e)

{

       System.err.println(e);

}   

 

}

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : Caller.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

import com.objectspace.voyager.*;

 

public class Caller{

 

public static void main(String[] args){

      

try{

    Voyager.startup();

    Computing computingObject = new Computing();

   

    IComputing iC = (IComputing)Namespace.lookup("//localhost:8000/Computing");

   

    double returnValue = (iC.computing(new ComputingObject(1,100000))).doubleValue();

    System.out.println(returnValue);   

 

}catch(Exception e)

{

       System.err.println(e);

}

    Voyager.shutdown();  

}

}

上面需要注意的就是传递给远程对象方法的参数需要实Serializable接口.

 

 

待续:

 

2.    DCOM

3.    CORBA

4.       WebServices

5.         Sun RMI  VS. dotNET Remoting

6.         LindaSun JavaSpace ,IBM Tspace

7.         JINI

8.         Emerald VS. Dejay

9.         Pjama

10.     IBM Mobile Computing Interface : Aglets

.综合应用分析

1.       利用多线程和分布对象计算技术模拟高性能并行计算

2.       利用Vdejay计算几何分形图

 

 

  声明:

1。本人不是专门研究分布式的,完全出于兴趣写下这篇文章.希对分布式计算应用爱好者有所帮助。
2
。水平有限,欢迎指正。
3
。由于本文讲述内容较多,很多讲述不可能太细致。而且考虑到读者群的问题,理论性内容尽量的省略。
  
如果读者有兴趣,可参考列出的参考书籍和网络资源。

4。可任意的转载、不过请注明出处;不可用于商业用途。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值