How do I get my Jersey 2 Endpoints to eagerly initialize on startup?

前端 未结 1 515
刺人心
刺人心 2020-12-04 00:17

I am porting some code from Jersey 1.x and my implementation of various Health Check endpoints relies on all the @Singleton endpoint resources being initialized

相关标签:
1条回答
  • 2020-12-04 00:53

    "Or some HK2 annotation or trick?"

    You can use HK2's Immediate Scope. Just annotate the resource class with @Immediate (which acts like @Singleton, so you can get rid of that), then enable the immediate scope on the ServiceLocator. An example:

    import org.glassfish.hk2.api.ServiceLocator;
    import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
    ...
    
    @ApplicationPath("/rest")
    public class JerseyApplication extends ResourceConfig {
    
        @Inject
        public JerseyApplication(ServiceLocator locator) {
            ServiceLocatorUtilities.enableImmediateScope(locator);
            packages("thepackages.to.scan");
        }
    }
    

    UPDATE

    Based on this related question, if you need to explicitly instantiate the ResourceConfig, as in the case of the linked question, you can create a Feature and register the feature, as seen in this answer

    UPDATE 2

    Please see related issue

    UPDATE 3

    Looks like Immediate scope memory leak issue previously linked to has been resolved in version 2.22.1

    0 讨论(0)
提交回复
热议问题