在启动和泽西岛上加载 [英] load on startup and Jersey

查看:86
本文介绍了在启动和泽西岛上加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复的 Servlet在启动时未加载,但是我暂时不允许发表评论,所以我必须为此提出一个新问题...

Possibly duplicate of Servlet not loading on startup, however Im not allowed yet to comment, so I have to start a new question for this...

相同的设置,使用泽西岛(Jersey)和Tomcat使用servlet,并使用启动时加载来加载容器. 但是,由于上面提到的线程,我知道这只会加载Jersey容器,而不会加载我为servlet设置的类.

Same setting, having a servlet using Jersey and Tomcat, using load-on-startup for loading the container. However, due to the thread mentioned above, I understand that this only load the Jersey container but not the classes I setup for the servlet.

因此,与上述线程的答案中所隐含的内容有关,如何不仅在启动时加载了包含的内容,而且还添加了用@Path注释的我的类(例如,将从DB中加载数据)在内存中.)

So, related to what is implied in the answer of the thread above, how is it done that not only the contained is loaded at startup but also my classes which are annotated with @Path (which will e.g. load data from a DB in memory).

@Singleton
@Path( "156846986" )
public class SearchEngine {

    @Inject
    private DatabaseService dbService;

    @Inject
    private PatriciaTrieEngine trieEngine;
}

,例如:

@Singleton
@Path( "3455470640" )
public class PatriciaTrieEngine {

@Inject
DatabaseService dbService;
private PatriciaTrie< Object > patriciaTrie;

@PostConstruct
public void init( ) throws SQLException {

    ...some code initializing the trie by loading data from a database u using dbService
}

}

最后,某些类,例如SearchService具有请求的端点:

finally some classes like SearchService have the endpoints for requests:

@Path( "/search" )
@Produces( "application/json" )
public class SearchService {

    @Inject
    private DatabaseService dbService;
    @Inject
    private SearchEngine    engine;

    @GET
    @Path( "/candidates" )
    public Response getCandidates(@QueryParam( "query" ) final String input) throws UnsupportedEncodingException {

    use Patricia trie via SearchEngine in order to find candidates for given query

    return Response.ok().entity( candidates ).build();
}

}

最终,应该在启动时加载PatriciaTrie,因为它将数据加载到Trie中需要花费几分钟.

Ultimately it is the PatriciaTrie which should be loaded at startup as it takes several minutes to load the data into the trie.

推荐答案

默认行为是为每个请求实例化资源类的新实例.在这种情况下,不需要在启动时加载.如果您想要这种行为,则您的资源类需要为单例,这意味着将为整个应用程序创建一个实例.如果这样做,则有责任使类线程安全.

Default behavior is to instantiate a new instance of the resource class per request. In which case there isn't an expected need to load on start up. If you want this behavior, then your resource class needs to be a singleton, meaning only one instance is created for the whole application. If you do this, then you are responsible for making the class thread safe.

在泽西岛1中,您可以使用@Singleton注释使类成为单例,如此处.这也将在启动时加载该类.在Jersey 2中,@Singleton注释将使资源类成为单例,但在启动时不会加载.为此,您可以改为使用@Immediate批注,如此处

In Jersey 1, you can make the class a singleton with the @Singleton annotation, as mentioned here. This will also load the class on start up. In Jersey 2, the @Singleton annotation will make the resource class a singleton, but it will not load on start up. For that, you can instead use the @Immediate annotation, as seen here

除了您的描述之外,在我看来,这似乎是需要修复设计的需求.如果看不到您要尝试执行的代码,就无法真正分辨.

That aside, just from your description, this appears to me like maybe a need to fix the design. Can't really tell without seeing some code to what you're trying to do.

这篇关于在启动和泽西岛上加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆