Register context listener for jersey
<p>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 <a href="https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html" rel="nofollow noreferrer">ServletContextListener</a>. The question of course is how to add this listener to the application. I have seen examples where this is done in the file <code>web.xml</code> like this:</p>

<pre><code>&lt;listener&gt;
  &lt;listener-class&gt;org.SomeCompany.SomePackage.server.MyListener&lt;/listener-class&gt;
&lt;/listener&gt;
</code></pre>

<p>where the <code>MyListener</code> class looks as follows:</p>

<pre><code>@WebListener
public class MyListener implements ServletContextListener {
</code></pre>

<p>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:</p>

<pre><code>HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI,
                new MyServerConfig());
</code></pre>

<p>The class <code>MyServerConfig</code> subclasses from <a href="https://jersey.java.net/apidocs/2.0/jersey/org/glassfish/jersey/server/ResourceConfig.html" rel="nofollow noreferrer">ResourceConfig</a> and uses the various <code>register</code> methods. I would like to add the listener programmatically as well, but calling <code>register</code> doesn't seem to do the job. Any ideas how to fix this?</p>