Java Restlet应用程序中用于处理变量的单例

Java Restlet应用程序中用于处理变量的单例,java,restlet,Java,Restlet,我需要你的帮助。我有一个用于索引文档的JavaRestlet服务器。我想有地图(同步),每个资源可以写入或读取。我发现每个资源都是不同的线程。主要问题是,在ServerRunner中,我将类作为参数而不是对象,所以我不知道如何使用此映射提供这些信息。我试着像这样使用单例: ServerRunner public class ServerRunner { public static void main(String[] args) throws Exception { //Server s

我需要你的帮助。我有一个用于索引文档的JavaRestlet服务器。我想有地图(同步),每个资源可以写入或读取。我发现每个资源都是不同的线程。主要问题是,在ServerRunner中,我将类作为参数而不是对象,所以我不知道如何使用此映射提供这些信息。我试着像这样使用单例:

ServerRunner

public class ServerRunner {
public static void main(String[] args) throws Exception {
    //Server server = new Server(Protocol.HTTP, 8888, ChickenResource.class);
    //server.start();

    Component component = new Component();
    component.getServers().add(Protocol.HTTP, 8888);

    DocIndexingResource test = DocIndexingResource.getInstance();
    List<IndexedFile> listIndexedFiles = Collections.synchronizedList(new ArrayList<IndexedFile>());
    test.setupMap(listIndexedFiles);
    component.getDefaultHost().attach("/chickens/{id}", test.getClass());


    //listIndexedFiles.add(new IndexedFile("asas", "aa", 2, null));



    //DocIndexingResource.getInstance().addIndexedFile();

    System.out.println("salajlalasjaksadsa");
    component.start();
}
}

非常感谢您的回答

RESTlet服务器资源不是单例。您将ServerResource类传递给应用程序/组件,它会根据需要创建它们。你在这里这样做:

component.getDefaultHost().attach("/chickens/{id}", test.getClass());

这里有很多问题。这可能并不理想,但您可以将
listIndexedFile
设置为静态成员(并对其进行初始化),以实现所需功能。

在ServerRunner中创建静态listIndexedFile?怎么做?我试过了,但它没有运行私有静态列表listIndexedFiles-您需要停止尝试将服务器资源用作单例。在两个类
DocIndexingResource
IndexedFile
中都有两个list
listinexedfile
?是的,这只是因为测试。。。现在我从indexedFile中删除了它,但没有成功。。。简单地说,我需要的是在某个地方创建一个同步HashMap(在代码中,由于测试,它是arraylist),并从每个资源中与此HashMap通信,但我在您发布的代码中没有发现任何问题。但是可以是这一行
indexedFile.listinexedfiles.add(indexedFile)创建问题,因为
索引文件
对象的
列表索引文件
为空。
Jan 27, 2014 12:42:33 PM org.restlet.resource.UniformResource doCatch
WARNING: Exception or error caught in resource
java.lang.NullPointerException
at docindexing.docindexing.DocIndexingResource.addIndexedFile(DocIndexingResource.java:47)
component.getDefaultHost().attach("/chickens/{id}", test.getClass());