首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jini/JavaSpaces发现错误

Jini/JavaSpaces发现错误
EN

Stack Overflow用户
提问于 2011-07-28 06:24:18
回答 3查看 2.7K关注 0票数 0

本文简介:http://java.sun.com/developer/technicalArticles/tools/JavaSpaces/是一个如何运行JavaSpaces客户端的教程。我用Eclipse编写了这些类,启动了Launch-All脚本和Run example。它起作用了。之后,我将这些类导出到可执行jar ( JavaSpaceClient.jar )中,并使用以下命令尝试该jar : java -jar JavaSpaceClient.jar它运行良好,给我的结果是:搜索JavaSpace...已发现JavaSpace。正在向空间中写入邮件...正在从太空中读取消息...这条消息是:ЗдравоJavaSpaceсвете!

我的问题是,当我在另一台局域网计算机上移动这个jar文件时,当我键入相同的命令时,它会显示错误。下面是错误:

代码语言:javascript
复制
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)

我只写了“搜索JavaSpace...”并在一段时间后打印这些错误消息。有人能帮我纠正这个错误吗?

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

代码语言:javascript
复制
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写入其中,然后检索消息并将其打印出来。以下是客户端程序:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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;
   }
}

EDIT2:我在两台Windows机上做了发现。在那之后,我尝试了Windows - Ubuntu组合,但它不起作用。也许有一些网络问题?当我互相ping的时候,一切都很好。也许在Ubuntu上有一些DNS问题..

EDIT3:如果JavaSpaces服务在Windows上启动,客户端程序在Ubuntu上,则Windows - Ubuntu组合有效。当我尝试反向操作时,在Ubuntu上运行JavaSpaces服务和在Windows上运行客户端时出现错误。显然,Ubuntu存在一些问题。Ubuntu默认安装了OpenJDK。我安装了Oracle JDK,设置了JAVA_HOME,并将JAVA_HOME/bin放入PATH变量。我想知道也许不同版本的Java有一些问题,也许我用的不对。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-22 22:44:39

我找到解决方案了!这是dns问题。在Ubuntu上,我的/etc/hosts文件是:

代码语言:javascript
复制
192.168.1.3 biske-Inspiron-1525 # Added by NetworkManager
127.0.0.1   localhost.localdomain   localhost
::1 biske-Inspiron-1525 localhost6.localdomain6 localhost6
127.0.1.1   biske-Inspiron-1525

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

我刚刚删除了行127.0.1.1 biske-Inspiron-1525,现在它工作得很好。一件小事毁了我上百万的神经:)

票数 0
EN

Stack Overflow用户

发布于 2011-08-24 18:11:39

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

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

代码语言:javascript
复制
-Dcom.sun.jini.reggie.unicastDiscoveryHost="biske-Inspiron-1525.and.its.domain"
票数 2
EN

Stack Overflow用户

发布于 2011-08-24 09:06:27

您似乎正在对特定主机和端口执行单播发现,并且无法查找该主机。

假设您可以使用DNS解析名称biske-Inspiron-1525,尝试删除":4160“部分,然后查看单播查找是否成功。

下面是我用来查找服务的代码示例。这有点复杂,因为我实现了ServiceDiscoveryListener并以这种方式处理服务发现。我实际上保留了一个服务列表,并在其中一个服务失败时动态切换,但我在示例中去掉了这一部分。我还使用了Jini的配置部分,稍后我将对其进行解释。我在这里使用的服务接口称为"TheService":

代码语言:javascript
复制
public class JiniClient implements ServiceDiscoveryListener {

private TheService service = null;

private Class[] serviceClasses;
private ServiceTemplate serviceTemplate;

public JiniClient(String[] configFiles) throws ConfigurationException {

    Configuration config = ConfigurationProvider.getInstance(configFiles,
            getClass().getClassLoader());


    // Set the security manager
    System.setSecurityManager(new RMISecurityManager());        

    // Define the service we are interested in.
    serviceClasses = new Class[] {TheService.class};
    serviceTemplate = new ServiceTemplate(null, serviceClasses, null);

    // Build a cache of all discovered services and monitor changes
    ServiceDiscoveryManager serviceMgr = null;

    DiscoveryManagement mgr = null;
    try {
        mgr = (DiscoveryManagement)config.getEntry(
                getClass().getName(), // component
                "discoveryManager",                 // name
                DiscoveryManagement.class);          // type

        if (null == mgr) {
            throw new ConfigurationException("entry for component " +
                    getClass().getName() + " name " +
                    "discoveryManager must be non-null");
        }
    } catch (Exception e) {
        /* This will catch both NoSuchEntryException and 
         * ConfigurationException. Putting them both
         * below just to make that clear.
         */
        if( (e instanceof NoSuchEntryException) || 
                (e instanceof ConfigurationException)) {
            // default value
            try {
                System.err.println("Warning, using default multicast discover.");
                mgr = new LookupDiscoveryManager(LookupDiscovery.ALL_GROUPS,
                        null,  // unicast locators
                        null); // DiscoveryListener
            } catch(IOException ioe) {
                e.printStackTrace();
        throw new RuntimeException("Unable to create lookup discovery manager: " + e.toString());
            }
        } 
    }

    try {
        serviceMgr = new ServiceDiscoveryManager(mgr, new LeaseRenewalManager());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unable to create service discovery manager: " + e.toString());
    }

    try {
        serviceMgr.createLookupCache(serviceTemplate, 
                                                null,  // no filter
                                                this); // listener
    } catch(Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Unable to create serviceCache: " + e.getMessage());
    }
}

public void serviceAdded(ServiceDiscoveryEvent evt) {
       /* Called when a service is discovered */
    ServiceItem postItem = evt.getPostEventServiceItem();
    //System.out.println("Service appeared: " +
    //         postItem.service.getClass().toString());

    if(postItem.service instanceof TheService) {
        /* You may be looking for multiple services. 
                     * The serviceAdded method will be called for each
                     * so you can use instanceof to figure out if 
                     * this is the one you want.
                     */
        service = (TheService)postItem.service;

    }       
}

public void serviceRemoved(ServiceDiscoveryEvent evt) {
/* This notifies you of when a service goes away. 
     * You could keep a list of services and then remove this 
     * service from the list. 
 */ 
}

public void serviceChanged(ServiceDiscoveryEvent evt) {
/* Likewise, this is called when a service changes in some way. */  

}

配置系统允许您动态配置发现方法,以便您可以切换到发现特定单播系统或组播,而无需更改应用程序。下面是一个单播发现配置文件的示例,您可以将其传递给上述对象构造函数:

代码语言:javascript
复制
import net.jini.core.discovery.LookupLocator;
import net.jini.discovery.LookupDiscoveryManager;
import net.jini.discovery.LookupDiscovery;

com.company.JiniClient {
    discoveryManager = new LookupDiscoveryManager(
        LookupDiscovery.ALL_GROUPS,
        new LookupLocator[] { new LookupLocator("jini://biske-Inspiron-1525.mycompany.com")},
        null,
        this); // the current config
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6851983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档