I am writing a Web Service that launches background threads in a method annotated with the @PostConstruct annotation.
In a method annotated with the @PreDestroy annotation, I am trying to shut down those threads to cleanly exit.
However, I have noticed that the container seems to be invoking my methods in a manner inconsistent with my understanding.
This is what seems to be happening:
On Deploy and first access to service:
The container invokes the Endpoint Constructor.
The container invokes the method labeled with the @PostConstruct annotation.
The container invokes the EndpointConstructor
The container invokes the method labeled with the @PreDestroy annotation.
Why does the container do this? This results in many NullPointerExceptions and leaked resources and threads. Why can't the container just call the @PreDestroy method using the object it already created?
Is this a matter of scope? If it is, what setting do I use to make sure that the container reuses the Service endpoint object it already has?
Please reply ASAP.