3

I have one plain .java class. In that class I'm using a Timer class schedule method, to schedule a task.

The problem is I'm using a Java EE application, and I dont know where to intantiate this class; from a Servlet or any thing like that? I want to instantiate that class only once when my application goes up.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
user1305398
  • 3,550
  • 5
  • 26
  • 42
  • By the way, rather than a `Timer` you may want to use a [`ScheduledExecutorService`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html). – Basil Bourque Feb 23 '14 at 11:20

3 Answers3

4

You probably need a ServletContextListener and its method contextInitialized(..). It is invoked once, when your application is initialized.

You map the listener with either @WebListener or with <listener><listener-class>..</...> in web.xml

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I am completely unaware of that.can you please explain it by an example. – user1305398 Apr 23 '12 at 20:25
  • I added a link and one more paragraph. You just implement the interface, do what you need to do in the method, and map the class in either of the ways proposed – Bozho Apr 23 '12 at 20:26
  • ok but i have one question.Now this interfcae will be implemented by a servlet,i got that.But what if i call the servlet again,when the application is up.Will it work then? – user1305398 Apr 23 '12 at 20:34
  • This interface is implemented by any class, preferably not a servlet. It will be invoked just once, regardless of where you implement it. – Bozho Apr 23 '12 at 20:38
  • See this [answer by BalusC](http://stackoverflow.com/a/9186070/642706) for example code. – Basil Bourque Feb 23 '14 at 11:18
1
 public class YourServlet extends HttpServlet {
 private YourClass instance;

 public void init() throws ServletException {
      instance = new YourClass();
 }
 //code
 }

By instantiating your class in the init method, you will make sure that your class will be instantiated only once, because in Java EE applications, Servlets are loaded into the server memory only once.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Hidalgo
  • 765
  • 1
  • 9
  • 28
  • irrespective of how many times i call this servlet,but the object will be created only once right?unless and until I bring the application down? – user1305398 Apr 23 '12 at 20:52
  • Right, the object will be created only once untill the application is restarted. – Hidalgo Apr 23 '12 at 21:14
  • +1, this is the other option, that is per-servlet, rather than per-application, as in my answer. Also viable, depending on the use-case – Bozho Apr 23 '12 at 21:31
0

In Quartz -a popular scheduler- is a common practice to configure Jobs in a the init method of a Servlet with the load-on-startup attribute set to true:

From this article, in web.xml you should do this:

<servlet>
    <servlet-name>QuartzInitializer</servlet-name>
    <display-name>Quartz Initializer Servlet</display-name>
    <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

And then conigure Jobs in your servlet:

public class QuartzServlet extends GenericServlet {
    public void init(ServletConfig config) throws ServletException {
    super.init(config);
    // And continue with your configuration

PS: I strongly recomend you to use Quartz

Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59