java - Jini/JavaSpaces 发现错误

标签 java distributed-computing javaspaces jini

关于这篇文章:http://java.sun.com/developer/technicalArticles/tools/JavaSpaces/是一个如何运行 JavaSpaces 客户端的教程。我在 Eclipse 中编写了这些类,启动了 Launch-All 脚本并运行示例。有用。 之后,我将这些类导出到可执行 jar (JavaSpaceClient.jar) 中,并使用以下命令尝试该 jar: java -jar JavaSpaceClient.jar 它工作正常,给了我结果: 正在寻找 JavaSpace... JavaSpace 已被发现。 将消息写入空间... 正在读取来自空间的消息... 读取的消息是:Здраво JavaSpace свете!

我的问题是,当我将此 jar 文件移动到另一台 LAN 计算机上时,当我键入相同的命令时,它会显示错误。这是错误:

cica@cica-System-Name:~/Desktop$ java -jar JavaSpaceClient.jar 
Searching for a JavaSpace...
Jul 27, 2011 11:20:54 PM net.jini.discovery.LookupDiscovery$UnicastDiscoveryTask run
INFO: exception occurred during unicast discovery to biske-Inspiron-1525:4160 with constraints InvocationConstraints[reqs: {}, prefs: {}]
java.net.UnknownHostException: biske-Inspiron-1525
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:175)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at com.sun.jini.discovery.internal.MultiIPDiscovery.getSingleResponse(MultiIPDiscovery.java:134)
at com.sun.jini.discovery.internal.MultiIPDiscovery.getResponse(MultiIPDiscovery.java:75)
at net.jini.discovery.LookupDiscovery$UnicastDiscoveryTask.run(LookupDiscovery.java:1756)
at net.jini.discovery.LookupDiscovery$DecodeAnnouncementTask.run(LookupDiscovery.java:1599)
at com.sun.jini.thread.TaskManager$TaskThread.run(TaskManager.java:331)

我只是写了“Searching for JavaSpace...”,过了一会儿就打印了这些错误消息。 有人可以帮我解决这个错误吗?

编辑: 对于发现,我使用在互联网上找到的 LookupDiscovery 类:

import java.io.IOException;

import java.rmi.RemoteException;

import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;

import net.jini.discovery.LookupDiscovery;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryEvent;

/**
   A class which supports a simple JINI multicast lookup.  It doesn't register
   with any ServiceRegistrars it simply interrogates each one that's
   discovered for a ServiceItem associated with the passed interface class.
   i.e. The service needs to already have registered because we won't notice
   new arrivals. [ServiceRegistrar is the interface implemented by JINI
   lookup services].

   @todo Be more dynamic in our lookups - see above

   @author  Dan Creswell (dan@dancres.org)
   @version 1.00, 7/9/2003
 */
public class Lookup implements DiscoveryListener {
    private ServiceTemplate theTemplate;
    private LookupDiscovery theDiscoverer;

    private Object theProxy;

    /**
       @param aServiceInterface the class of the type of service you are
       looking for.  Class is usually an interface class.
     */
    public Lookup(Class aServiceInterface) {
        Class[] myServiceTypes = new Class[] {aServiceInterface};
        theTemplate = new ServiceTemplate(null, myServiceTypes, null);
    }

    /**
       Having created a Lookup (which means it now knows what type of service
       you require), invoke this method to attempt to locate a service
       of that type.  The result should be cast to the interface of the
       service you originally specified to the constructor.

       @return proxy for the service type you requested - could be an rmi
       stub or an intelligent proxy.
     */
    Object getService() {
        synchronized(this) {
            if (theDiscoverer == null) {

                try {
                    theDiscoverer =
                        new LookupDiscovery(LookupDiscovery.ALL_GROUPS);
                    theDiscoverer.addDiscoveryListener(this);
                } catch (IOException anIOE) {
                    System.err.println("Failed to init lookup");
                    anIOE.printStackTrace(System.err);
                }
            }
        }

        return waitForProxy();
    }

    /**
       Location of a service causes the creation of some threads.  Call this
       method to shut those threads down either before exiting or after a
       proxy has been returned from getService().
     */
    void terminate() {
        synchronized(this) {
            if (theDiscoverer != null)
                theDiscoverer.terminate();
        }
    }

    /**
       Caller of getService ends up here, blocked until we find a proxy.

       @return the newly downloaded proxy
     */
    private Object waitForProxy() {
        synchronized(this) {
            while (theProxy == null) {

                try {
                    wait();
                } catch (InterruptedException anIE) {
                }
            }

            return theProxy;
        }
    }

    /**
       Invoked to inform a blocked client waiting in waitForProxy that
       one is now available.

       @param aProxy the newly downloaded proxy
     */
    private void signalGotProxy(Object aProxy) {
        synchronized(this) {
            if (theProxy == null) {
                theProxy = aProxy;
                notify();
            }
        }
    }

    /**
       Everytime a new ServiceRegistrar is found, we will be called back on
       this interface with a reference to it.  We then ask it for a service
       instance of the type specified in our constructor.
     */
    public void discovered(DiscoveryEvent anEvent) {
        synchronized(this) {
            if (theProxy != null)
                return;
        }

        ServiceRegistrar[] myRegs = anEvent.getRegistrars();

        for (int i = 0; i < myRegs.length; i++) {
            ServiceRegistrar myReg = myRegs[i];

            Object myProxy = null;

            try {
                myProxy = myReg.lookup(theTemplate);

                if (myProxy != null) {
                    signalGotProxy(myProxy);
                    break;
                }
            } catch (RemoteException anRE) {
                System.err.println("ServiceRegistrar barfed");
                anRE.printStackTrace(System.err);
            }
        }
    }

    /**
       When a ServiceRegistrar "disappears" due to network partition etc.
       we will be advised via a call to this method - as we only care about
       new ServiceRegistrars, we do nothing here.
     */
    public void discarded(DiscoveryEvent anEvent) {
    }
}

我的客户端程序尝试简单地搜索 JavaSpaces 服务并将 MessageEntry 写入其中,然后检索消息并将其打印出来。这是客户端程序:

import net.jini.space.JavaSpace;

public class SpaceClient {
   public static void main(String argv[]) {
      try {
         MessageEntry msg = new MessageEntry();
         msg.content = "Hello JavaSpaces wordls!";
         System.out.println("Searching for JavaSpaces...");
         Lookup finder = new Lookup(JavaSpace.class);
         JavaSpace space = (JavaSpace) finder.getService();
         System.out.println("JavaSpaces discovered.");
         System.out.println("Writing into JavaSpaces...");
         space.write(msg, null, 60*60*1000);
         MessageEntry template = new MessageEntry();
         System.out.println("Reading message from JavaSpaces...");
         MessageEntry result = (MessageEntry) space.read(template, null, Long.MAX_VALUE);
         System.out.println("Message: "+result.content);
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

当然这是 MessageEntry 类:

import net.jini.core.entry.*;

public class MessageEntry implements Entry {
   public String content;

   public MessageEntry() {
   }

   public MessageEntry(String content) {
     this.content = content;
   }

   public String toString() {
     return "MessageContent: " + content;
   }
}

编辑2: 我在两台 Windows 计算机上进行了发现。 之后我尝试了 Windows - Ubuntu 组合,但它不起作用。也许存在一些网络问题?当我互相 ping 通时一切正常。也许 Ubuntu 上存在一些 DNS 问题..

编辑3: Windows - 如果 JavaSpaces 服务在 Windows 上启动并且客户端程序在 Ubuntu 上,则 Ubuntu 组合可以工作。当我尝试执行相反操作时,在 Ubuntu 上运行 JavaSpaces 服务并在 Windows 上运行客户端时会发生错误。 显然Ubuntu有一些问题。 Ubuntu已经默认安装了OpenJDK。我安装了Oracle JDK,并设置了JAVA_HOME并将JAVA_HOME/bin放入PATH变量中。我想知道也许不同版本的Java有一些问题,也许我没有使用正确的版本。

最佳答案

您正在运行的服务注册器(在端口 4160 的主机 biske-Inspiron-1525 上)可能发现其主机名不正确(没有域名),因此使用短主机名发送公告。因此,在发现服务注册器后,客户端随后尝试与服务注册器建立连接时,如果该服务注册器位于不同的域中,则可能无法解析主机名。

要确保服务注册器使用正确的主机名运行,请尝试使用以下命令行属性启动它:

-Dcom.sun.jini.reggie.unicastDiscoveryHost="biske-Inspiron-1525.and.its.domain"

关于java - Jini/JavaSpaces 发现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6851983/

相关文章:

c# - 一种使用 .Net 框架解决分布式计算问题的方法

java - 通过 Java Space 连接进行查询?

java - JAVA、JavaSpaces 中的进程通信

java - 如何使用 Google Dictionary 作为 API?

java - Android Studio 中带参数的查询 - Room

java - 来自 Postgresql 的类型和来自 Play 的类型 (anorm)

java - 如何独立于 Spring 上下文使用 @Autowired 和 @Value 注释?

java - Zookeeper cfg 文件 - 为什么有多个端口?

hadoop - YARN UNHEALTHY 节点

java - GWT 支持 Javaspaces 吗?