users@jersey.java.net

[Jersey] Re: Resource init/destroy

From: Guilhem <guilhem.legal_at_geomatys.fr>
Date: Tue, 22 Oct 2013 16:44:15 +0200

Hi,

I respond to myself, i managed to produce the behavior i was looking for
by creating a Application class and instatiate myself the singletons
resource in the constructor.

As i can not use anymore the resource scan, i had to add a META-INF
mecanism to load dynamically all the singletons located in my classpath


@ApplicationPath("/")
public class CstlApplication extends Application {

     private final Set<Object> singletons = new HashSet<>();

     public CstlApplication() {
         final Iterator<WebService> ite =
ServiceRegistry.lookupProviders(WebService.class);
         while (ite.hasNext()) {
             WebService ws = ite.next();
             singletons.add(ws);
         }
     }

     @Override
     public Set<Object> getSingletons() {
         return singletons;
     }


     @PreDestroy
     public void destroy() {
         for (Object o : singletons) {
             final WebService ws = (WebService) o;
             ws.destroy();
         }
     }
}

It forced me to add a file in Meta-INF for each singleton classes but,
it works (I'm still looking for a better/different solution).

For the 2), i see that now @PreDestroy is only called on Application
class, not @Path anotated class like in 1.X .


But i ran into a new bug after setting all that :

my injected field :

@Context
  private volatile HttpServletRequest httpServletRequest;

throw a exception at startup. I found the JIRA task
https://java.net/jira/browse/JERSEY-1960 and see that it has been
corrected in 2.4.

So i move on 2.4-SNAPSHOT to give it a try, the injected field is well
corrected but i ran into a new bug.

I still use classPath scanning for my MessageBodyWriter, and i got
several packages containing one or more MessageBodyWriter classes.

<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
      <param-value>package1;packages2</param-value>
  </init-param>

with 2.4-SNAPSHOT, it only detect one MessageBodyWriter by package (the
first it found i think).

Is anybody already spot this bug ?


Guilhem Legal





Le 21/10/2013 10:40, Guilhem a écrit :
> Hi,
>
> I'm currently updating my services from jersey 1.11 to 2.3.1.
> I'm facing two problem :
>
> 1) My classes anotated with @Path and @Singleton are no longer
> instanciate at startup, but at the first request on them.
> Is there any configuration to start all the resource at the
> application deployement ?
>
> 2) When i shutdown my application server, the @preDestroy method is no
> longer called. Do i need to use another annotation?
>
> Guilhem Legal
>
>