Saturday, December 19, 2009

How do we implement a daemon (a program running all the time, like a scheduled program with a fixed-rate) inside Weblogic?

The best approach is to create an empty servlet, let's call it StartupServlet, that has standard web.xml option set to non-zero integer value. Place your code in the implementation of the servlet's "init" method. It's guaranteed that this method will be called on the server startup, that it will be called once, and that by that time all the server's services, including EJB, will be up.

Example:
import javax.servlet.*;
public class StartupServlet extends HttpServlet {

public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
ServiceManager serviceManager = ServiceManager.getInstance();
serviceManager.startServices();
}


public void destroy() {
ServiceManager serviceManager = ServiceManager.getInstance();
serviceManager.stopServices();
super.destroy();
}
}


web.xml




2 comments:

  1. ServiceManager serviceManager = ServiceManager.getInstance();

    What is ServiceManager class? and what it does?
    Which java package it belongs to?
    if it is a user defined class can you post snippet of it?

    Thanks,
    Srikanth

    ReplyDelete
  2. Hey Srikanth,

    ServiceManager is an example here. You need to add your required services in the init mehtod to start and in destroy mehtod stop services. Servlet will make sure that these methods are called.

    Moreover there are O/S related service managers available which can be included similar to this hence I have not included any specific methods.

    HTH
    RC2

    ReplyDelete