Register context listener for jersey

Viewed 2902

I need to perform some clean up steps after shutting down a jersey server. To my mind this could be easily accomplished by implementing a ServletContextListener. The question of course is how to add this listener to the application. I have seen examples where this is done in the file web.xml like this:

<listener>
  <listener-class>org.SomeCompany.SomePackage.server.MyListener</listener-class>
</listener>

where the MyListener class looks as follows:

@WebListener
public class MyListener implements ServletContextListener {

The problem is that this approach only works for deployment as a war file. However, I do also ship my software as a standalone jar file which creates a Grizzly web server to deploy the servlet:

HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI,
                new MyServerConfig());

The class MyServerConfig subclasses from ResourceConfig and uses the various register methods. I would like to add the listener programmatically as well, but calling register doesn't seem to do the job. Any ideas how to fix this?

1 Answers
Related