Skip to Content
I guess this will remind every one of automatically executing some code at specific time. Most of the times I have seen requirements for executing an application code on time based, suddenly this will be coming to your mind javax.ejb.TimedObject.  Now the set of options and issues arrive, normally java.ejb.TimedObject is available in EJB 2.1, most of the time we end up clients having lesser versions, and you cannot guarantee and portability because of this version issue.    Even I have seen many open source ways to tackle this. Like Quartz. But again attaching this source code with your project might attract some license issue.  Well, down to earth we have a simple way for it, a simple J2EE timer. We might need this fellow javax.util.Timer. I have seen many posts in SDN asking for a way to execute a set of code before deployment, for Database registration, Auto login and Automated Webservice clients etc… I would suggest this as the best possible way to create a simple timer.  Create a Servlet with a Context Listener once the Servlet is initialized the ContextListeners are notified. Here give the java.util.Timer.  Again this can be given in your  init method. (life cycle method of servlet and initialize the servlet on startup).  Enough stories, So Let us go little bit Hands On!!.  Create a Web Module Project. imageimage Now our project is ready. Right Click and New-> Listener  image Now let’s try to create a Servlet Context Listener.   image   Now give the implementation for the Timer method as given below. image This timer is now ready for adding this to an Application Project. Build and deploy this servlet in our favorite J2EE container.  image Now let us test your server trace or log file. Our timer is ready. “#Error” is because we are using System.Error stream to print the response. image  So our Timer is ready no version or vendor to stop you. Enjoy coding!    
To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Amol Joshi
    Hi Vivek,

    one quick question…

    AFAIK, this timer class internally creates threads.How safe it would be to have your own threads, inside a J2EE runtime environment which is generally not recommended?

    Regards,
    Amol

    (0) 
    1. Anonymous
      Hi,
      Its a major violation incase if we try to control or manupulate its behaviour(Since this is not same in all platform), Most of the time in XI Proxies we need to create thread to service requests and all these should be done from EJB.
      I have suggested this to be the easy substitute for Timer Object in case you dont have the right version.
      (0) 
      1. Anonymous
        Hi,

        I did some more study on this. Its restricted in EJB, since the container has the responsiblity to start, stop and pool threads, Using trying to do so might restrict the server from distributing the application. But you dont have a EJB here, this will be the replacement and I have used this in generating some clients for XI socket clients.

        regards
        Vivek Nidhi.

        (0) 
    2. Anonymous
      Hi,
      IMHO, Controlling a thread is not allowed in J2EE spec. since its behaviour is in different Platforms. J2EE strongly discourage manipulating based on this. But in case of Java Proxies in XI, many a times we need to spawn thread to execute mutiple tasks. Also I have suggest that its a simple way to substitute java.ejb.TimedObject where its not available.

      regards
      Vivek Nidhi

      (0) 
  2. FAHAD HAMSA
    Hi Vivek,

    Very nice blog. I think it will be very helpful for my requirement. So, I would like to confirm some points.

    1. When this scheduling starts. Will it start immediately after the deployment?
    2. Suppose the server got restarted. In that case, will this scheduler starts automatically??
    3. What is the significance of using Servlet listener here?
    4. Can I do the same coding in a simple webdynpro application, so that, on running the application, my scheduler starts, or whenever server starts?
    5. How to stop the scheduled job manually. Is it possible to do that?

    Thanks,
    Fahad Hamsa

    (0) 

Leave a Reply