When we develop a thread in such a way that it has to execute some logic until the tomcat is shutdown.
Basically the idea goes this way, A servlet will be called upon the tomcat startup and in turn will start the thread.This thread has a logic in an infinite loop.
There are two cases to be analysed ,
Case 1 : even if the tomcat is shutdown the jvm will be kept busy because of our infinite loop i.e, daemon thread class.This we shall call it as manual daemon thread.
In the following example, when you deploy it in linux you shall see that even after the tomcat is being shutdown , you shall look for println statements in the catalina.out. This tells us that the jvm is kept busy and and it cannot be pulled off.
Example (case 1)::
/**
*
* @author Raghuram
*/
public class SMSGateWayServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
new TestThread().start();
} catch (Exception ex) {
Logger.getLogger(SMSGateWayServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
} finally {
out.close();
}
}
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
class TestThread extends Thread {
private long testMethod() {
try {
System.out.println("called test method");
} catch (Exception e) {
System.err.println(e);
}
return System.currentTimeMillis();
}
public void run() {
try {
long start = System.currentTimeMillis();
System.out.println("begin start time -- >" + start);
long end=start+65000;
int i=0;
while (start > 0) {
System.out.println("run method while -->"+i);
if ((end - start) <= 60000) {
System.out.println("run method if-->"+(end - start));
System.out.println("setting the sleep -- > "+(60000-(end - start)));
try {
Thread.sleep(60000 - (end - start));
start = System.currentTimeMillis();
System.out.println("b4 call start time -->"+start);
end = testMethod();
System.out.println("after call end time -->"+end);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} else {
System.out.println("else");
start = System.currentTimeMillis();
end = testMethod();
System.out.println("else start time -->"+start);
System.out.println("else end time -->"+end);
}
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Case 2 : once the tomcat is shutdown thread and related servlets all will be closed and garbage collected. To make this happen we should design our thread in such a way it is daemon to the jvm. This can be done by using the method setDaemon(true). following example will illustrate that.
Example (case 2)::
/**
*
* @author Raghuram
*/
public class SMSGateWayServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
new TestThread().start();
TestThread tt=new TestThread();
tt.setDaemon(true) ;
tt.start();
} catch (Exception ex) {
Logger.getLogger(SMSGateWayServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
} finally {
out.close();
}
}
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
class TestThread extends Thread {
private long testMethod() {
try {
System.out.println("called test method");
} catch (Exception e) {
System.err.println(e);
}
return System.currentTimeMillis();
}
public void run() {
try {
long start = System.currentTimeMillis();
System.out.println("begin start time -- >" + start);
long end=start+65000;
int i=0;
while (start > 0) {
System.out.println("run method while -->"+i);
if ((end - start) <= 60000) {
System.out.println("run method if-->"+(end - start));
System.out.println("setting the sleep -- > "+(60000-(end - start)));
try {
Thread.sleep(60000 - (end - start));
start = System.currentTimeMillis();
System.out.println("b4 call start time -->"+start);
end = testMethod();
System.out.println("after call end time -->"+end);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} else {
System.out.println("else");
start = System.currentTimeMillis();
end = testMethod();
System.out.println("else start time -->"+start);
System.out.println("else end time -->"+end);
}
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment