File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to stop Servlet when reloading Application

 
John Norris
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
John Norris
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, That's exactly what I was looking for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic