• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Java run on Servlet

How can I schedule my java program to run every 1 min on servlet?

I got a java program running on Tomcat servlet. This program is used for checking the email, if found, open the attachment and then upload to our database. i want to run this program every minutest. I dont know how to do?

Are there any scheduler tool on servlet?
How can we schedule the job running on Tomcat servle?

Anyone who have an idea please let me know. Many thanks in advance

0
LeanMoreTryMore
Asked:
LeanMoreTryMore
  • 9
  • 6
1 Solution
 
objectsCommented:
Use a Timer, which you can start when your servlet loads (use load on startup)

0
 
LeanMoreTryMoreAuthor Commented:
I look at the web set as advised. But i dont understand
Is this just a java program or a tool we need to purchase?
How can the time program run on the Tomcat servlet? Do i need to configure the Apache Tomcat?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
> Is this just a java program or a tool we need to purchase?

Thats code you need to run from your servlet to start the timer

> How can the time program run on the Tomcat servlet?

Your servlet would start the Timer which would call your code every n minutes

> Do i need to configure the Apache Tomcat?

Only to start your servlet on startup (using load-on-startup)
0
 
bloodredsunCommented:
>> How can I schedule my java program to run every 1 min on servlet?

Due to the lifecycle of a servlet I'm not sure that it's possible to do this as the servlet may be reclaimed by the server.

From the sun website:

"The servlet engine is not required to keep a servlet loaded for any period of time or for the life of the server. Servlet engines are free to use servlets or retire them at any time. Therefore, you should not rely on class or instance members to store state information."

This means that while the servlet can be used to start and stop an external program, it shouldn't be used to run one.
0
 
LeanMoreTryMoreAuthor Commented:
Is that code save as a java program?
How do i put on the servlet?
0
 
objectsCommented:
The servlet would only need to start the timer, once that is done the timer would look after firing the task.
0
 
objectsCommented:
> Is that code save as a java program?
> How do i put on the servlet?

It would be put into the init() method of your servlet
0
 
objectsCommented:


public class TimerServlet extends HttpServlet
{
   public void init()
   {
    int delay = 5000;   // delay for 5 sec.
    int period = 1000;  // repeat every sec.
    Timer timer = new Timer();
   
    timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                // Task here ...
            }
        }, delay, period);

   }
}
0
 
LeanMoreTryMoreAuthor Commented:
Would you give me a very very simple program using init(0 method which is initiated from within the servlet when started, please?
0
 
LeanMoreTryMoreAuthor Commented:
Just make sure i do understand before I close this tar

your TimerServlet program is initiated when it the servlet is loaded. Thank you
0
 
objectsCommented:
Yes, by specify the load-on-startup parameter in your web.xml the servlet will get loaded when the webapp is started.
0
 
LeanMoreTryMoreAuthor Commented:
Lucky you told me because i dont know much about web.xml

Do you mean I need to add an entry to the $TOMCAT_HOME/conf/web.xml file?

Would you please give me the sample entry, thanks
0
 
objectsCommented:
Yes, each servlet should have an entry in web.xml

  <servlet>
      <servlet-name>Timer</servlet-name>
      <servlet-class>TimerServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
<servlet-mapping>
    <url-pattern>/Timer</servlet-class>
    <servlet-name>Timer</servlet-class>
  </servlet-mapping>


0
 
LeanMoreTryMoreAuthor Commented:
Is the Timer reserved word for servlet?
The tag <load-on-startup>1  what does it mean?
Does that mean only execute 1

0
 
objectsCommented:
> Is the Timer reserved word for servlet?

No you can name your server whatever you want.
Actually thinking about it you can get rid of the servlet-mapping, you don't need it

> The tag <load-on-startup>1  what does it mean?

That means to load your servlet when your webapp starts.
The 1 indicates what order to start it.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now