LeanMoreTryMore
asked on
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
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
Use a Timer, which you can start when your servlet loads (use load on startup)
ASKER
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?
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?
> 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)
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)
>> 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.
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.
ASKER
Is that code save as a java program?
How do i put on the servlet?
How do i put on the servlet?
The servlet would only need to start the timer, once that is done the timer would look after firing the task.
> 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
> How do i put on the servlet?
It would be put into the init() method of your servlet
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(
public void run() {
// Task here ...
}
}, delay, period);
}
}
ASKER
Would you give me a very very simple program using init(0 method which is initiated from within the servlet when started, please?
ASKER
Just make sure i do understand before I close this tar
your TimerServlet program is initiated when it the servlet is loaded. Thank you
your TimerServlet program is initiated when it the servlet is loaded. Thank you
Yes, by specify the load-on-startup parameter in your web.xml the servlet will get loaded when the webapp is started.
ASKER
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
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
Yes, each servlet should have an entry in web.xml
<servlet>
<servlet-name>Timer</servl et-name>
<servlet-class>TimerServle t</servlet -class>
<load-on-startup>1</load-o n-startup>
</servlet>
<servlet-mapping>
<url-pattern>/Timer</servl et-class>
<servlet-name>Timer</servl et-class>
</servlet-mapping>
<servlet>
<servlet-name>Timer</servl
<servlet-class>TimerServle
<load-on-startup>1</load-o
</servlet>
<servlet-mapping>
<url-pattern>/Timer</servl
<servlet-name>Timer</servl
</servlet-mapping>
ASKER
Is the Timer reserved word for servlet?
The tag <load-on-startup>1 what does it mean?
Does that mean only execute 1
The tag <load-on-startup>1 what does it mean?
Does that mean only execute 1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.