Two Laptop Bag
The moose likes Servlets and the fly likes How to stop Servlet when reloading Application Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "How to stop Servlet when reloading Application" Watch "How to stop Servlet when reloading Application" New topic
Author

How to stop Servlet when reloading Application

John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
I have a servlet will not stop/start when the application is reloaded. The only way to currently get rid of it is to cycle the JVM. Currently each time I attempt to reload the application, I get an additional servlet running. The premise of this servlet is to process a resend queue every 5 minutes. The servlet starts onload, creates a thread that sleeps for 5 minutes then runs the resend logic.
This is old code that I'm maintaining and if there is a better way to activate the resend logic, I'm all ears. Here's the current code:

public class MgBridgeResendHelper extends HttpServlet {
private static final long serialVersionUID = 6047999961405830191L;

private static String MG_BRIDGE_MESSAGE_HANDLER_HOME = "MG_BRIDGE_MESSAGE_HANDLER_HOME";

private static long RESEND_INTERVAL;

private ResendThread thread;

MgBridgeMessageHandlerHome aiMsgHome = null;

public void init() throws ServletException {
super.init();
ResourceBundle resourceBundle = PropertyResourceBundle
.getBundle("MgBridge");
MG_BRIDGE_MESSAGE_HANDLER_HOME = resourceBundle
.getString("MG_BRIDGE_MESSAGE_HANDLER_HOME");
logger = Logger.getLogger(MgBridgeResendHelper.class);
logger
.info("Got the JNDI name MG_BRIDGE_MESSAGE_HANDLER_HOME from Resoucebundle");
String resendInterval = resourceBundle
.getString("MgBridge.cacsSend.resendInterval");
if (resendInterval == null) {
RESEND_INTERVAL = 3000 * 60;
} else {
RESEND_INTERVAL = Long.valueOf(resendInterval).longValue();
}
try {
Context ctx = new InitialContext();
aiMsgHome = (MgBridgeMessageHandlerHome) ctx
.lookup(MG_BRIDGE_MESSAGE_HANDLER_HOME);
thread = new ResendThread();
thread.setDaemon(true);
thread.start();
} catch (NamingException e) {
e.printStackTrace();
}
}

public void destroy() {
thread.interrupt();
super.destroy();
}

private class ResendThread extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(RESEND_INTERVAL);
serviceResends();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}

private void serviceResends() {
}
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The 'resend thread' is not a good idea in the servlet.

Firstly it should be moved into a separate processing class, but it should also have a shutdown hook to stop the thread gracefully but you will also need to have a ContextListener that stops the thread when the context gets reloaded, as this will not cause the shutdown hook to fire.

/Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Oh, now i see it isn't even a real servlet, just a class that initialises when the servlet starts up.

Therefore you should stop it being a srevlet, and start and stop it from a ContextListener instead.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3026
    
    4

Originally posted by David O'Meara:
Oh, now i see it isn't even a real servlet, just a class that initialises when the servlet starts up.

Therefore you should stop it being a srevlet, and start and stop it from a ContextListener instead.


Here is an example:

And it is mapped in the web.xml like this:


Steve
John Norris
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Thanks, That's exactly what I was looking for.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to stop Servlet when reloading Application
 
Similar Threads
dopost() and doget() methods
Help? - Reloading log4j.xml config without jboss restart
question on instance variables in servlets
multiple get requests - one thread serving them all?
post request from jsp not received by Servlet