Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Restlet,我在哪里可以预加载某个内容,使其不';每次访问资源时是否处理?_Java_Web Services_Restlet - Fatal编程技术网

Java 使用Restlet,我在哪里可以预加载某个内容,使其不';每次访问资源时是否处理?

Java 使用Restlet,我在哪里可以预加载某个内容,使其不';每次访问资源时是否处理?,java,web-services,restlet,Java,Web Services,Restlet,我想使用Restlet来处理一些信息的请求,但是从磁盘加载这些信息需要一些时间,所以我想在Restlet服务器启动时执行这一步骤,而不是在我的资源类中,它似乎在每个请求上都被实例化。换句话说,我想把它加载到内存中一次 我在看本教程:假设每次有人请求/用户时,router.attach(“/Users”,UserResource.class”)实例化一个新的UserResource()对象。假设我想将用户数据库加载到内存中,以便在UserResource.findUser()中快速查找 更新:也许

我想使用Restlet来处理一些信息的请求,但是从磁盘加载这些信息需要一些时间,所以我想在Restlet服务器启动时执行这一步骤,而不是在我的资源类中,它似乎在每个请求上都被实例化。换句话说,我想把它加载到内存中一次

我在看本教程:假设每次有人请求/用户时,
router.attach(“/Users”,UserResource.class”)实例化一个新的
UserResource()
对象。假设我想将用户数据库加载到内存中,以便在
UserResource.findUser()
中快速查找

更新:也许像这样的答案可以帮助我

更新2:我想我找到了一个解决方案,因此我将很快发回我在Restlet书中的发现,他们只需使用
资源
类中的
getApplication()
函数:

public class Application extends org.restlet.Application {

    public static void main(String... args) throws Exception {
        // Create a component with an HTTP server connector
        final Component comp = new Component();
        comp.getServers().add(Protocol.HTTP, 3000);

        // Attach the application to the default host and start it
        comp.getDefaultHost().attach("/v1", new Application());
        comp.start();
    }

    private final ObjectContainer container;

    /**
     * Constructor.
     */
    public Application() {
        /** Open and keep the db4o object container. */
        EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
        config.common().updateDepth(2);
        this.container = Db4oEmbedded.openFile(config, System
                .getProperty("user.home")
                + File.separator + "restbook.dbo");
    }

    @Override
    public Restlet createInboundRoot() {
        final Router router = new Router(getContext());

        // Add a route for user resources
        router.attach("/users/{username}", UserResource.class);

        // Add a route for user's bookmarks resources
        router.attach("/users/{username}/bookmarks", BookmarksResource.class);

        // Add a route for bookmark resources
        final TemplateRoute uriRoute = router.attach(
                "/users/{username}/bookmarks/{URI}", BookmarkResource.class);
        uriRoute.getTemplate().getVariables().put("URI",
                new Variable(Variable.TYPE_URI_ALL));

        return router;
    }

    /**
     * Returns the database container.
     * 
     * @return the database container.
     */
    public ObjectContainer getContainer() {
        return this.container;
    }
}


/** resource class (UserResource.java) has these functions
/**
 * Returns the parent application.
 * 
 * @return the parent application.
 */
@Override
public Application getApplication() {
    return (Application) super.getApplication();
}

/**
 * Returns the database container.
 * 
 * @return the database container.
 */
public ObjectContainer getContainer() {
    return getApplication().getContainer();
}