1

I have a class, Services, that extends javax.ws.rs.core.Application as shown below. It overrides getSingletons() to ensure that there is only a single instance of the resource class, CommonServices.

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

  @Override
  public Set<Object> getSingletons() {
      Set<Object> resources = new HashSet<>();
      set.add(new CommonServices());
      return set;
  }
}

In my resource class:

@Path("/")
public class CommonServices{

  public CommonServices() {
  }

  ...
}

I deployed the application to WebLogic, which uses Jersey implementations of JAX-RS. After starting the application, I noticed that two instances of CommonServices were instantiated instead of just one (I put debug statements inside its constructor). These instantiations happened before any service calls were made. Why are two instances of CommonServices instantiated?

Below is a snippet of my web.xml showing the relevant part for JAX-RS. I have to comment out the servlet-mapping part. If I don't comment out, during deployment, WebLogic will complain that the url-pattern /* in the web application is mapped to multiple servlets. Why?

<servlet>
    <servlet-name>JAX-RS Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.xxx.xxx.xxx</param-value>
    </init-param>
    <init-param>
      <param-name>wl-dispatch-policy</param-name>
      <param-value>HighPriorityWorkManager</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<!--<servlet-mapping>
<servlet-name>JAX-RS Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>-->

EDIT

I found that the issue is due to getSingletons() and getClasses() being called twice by the container. To resolve the issue, I just need to add the resources to the set in the constructor instead of in getSingletons(). This way, only one instance of CommonServices is ever called.

user3573403
  • 1,780
  • 5
  • 38
  • 64
  • You might have two separate applications started. [This](https://stackoverflow.com/a/45627178/2587435) might give you an understanding of how that could happen. – Paul Samsotha Jan 08 '19 at 07:26
  • Hi Paul, you are actually right that I have two separate applications started! Based on your given link, I was using both (1) With only web.xml and (3) With only an Application/ResourceConfig class annotated with @ApplicationPath. So what I just did now was to completely remove the Services class, which extends Application, and just to rely on web.xml. – user3573403 Jan 08 '19 at 08:33
  • There is just another problem though. I want CommonServices to be a singleton and not having to be instantiated for each request, so I annotated it with @Singleton. But it doesn't seem to work as CommonServices is instantiated each time I call its service. – user3573403 Jan 08 '19 at 08:33
  • Are you using `@javax.inject.Singleton`? – Paul Samsotha Jan 08 '19 at 08:46
  • Yes, I'm using @javax.inject.Singleton. – user3573403 Jan 08 '19 at 08:56
  • Yeah, Im not sure then. It _should_ work. – Paul Samsotha Jan 08 '19 at 08:58
  • I tried using (3) With only an Application/ResourceConfig class annotated with @ApplicationPath now. This time I used back Services class, which extends Application, and I removed the servlet and servlet-mapping in web.xml. I would expect my CommonServices class to be instantiated just once, but no, it's instantiated two times as before when the application is deployed. – user3573403 Jan 08 '19 at 09:04
  • getSingletons is probably called twice. Add it in the constructor. – Paul Samsotha Jan 08 '19 at 09:05
  • I put a debug statement in getSingletons(). It's called only once. But CommonServices was instantiated twice. – user3573403 Jan 08 '19 at 09:30
  • Oh wait, actually getSingletons() was called twice. I am not sure why. The constructed was called only once so it means that Services was instantiated just once. – user3573403 Jan 08 '19 at 09:42
  • I think it's a bug. – Paul Samsotha Jan 08 '19 at 20:17

0 Answers0