• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

How to schedule a java program in Tomcat 8

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can someone tell if it is possible to schedule a java program in Tomcat 8 or an alternate solution?
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Tomcat is a web server which serves content over the web. What do you exactly mean by schedule a program?

For scheduled job requirements (in general) I would recommend Quartz
 
Muthuraj Kv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant to run a java program on a specified time on a day. I think I will go with Quartz. I hope I can integrate this with my app which I will be hosting in tomcat
 
Maneesh Godbole
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope it is clear that for Quartz to run you need to have a running JVM.
 
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Webapp servers such as Tomcat do not "run programs". They host webapps.

The difference is this: a program runs as a process (thread). It has a definite starting point and ending point(s). The Tomcat server itself is therefore a "program". Or in Java terminology, an Application.

Webapps do not run as threads. They are strictly re-active. When an HTTP/HTTPS URL request is received at one of Tomcat's tcp listener ports, Tomcat decodes that request, pulls a thread from its request processor thread pool, and tells the thread to go call a webapp resource (servlet or JSP) that has been mapped to handle it. The resource processes the request, generates results, then returns to the Tomcat dispatcher, which puts the now-idle thread back in its process pool. So webapp resources don't run continuously, unlike programs. They're more like DLL calls, to put it in Windows terms.

It's not uncommon for a webapp to run in conjunction with a scheduled process or a long-running process. However, since the HTTP request/response mode of operation isn't suitable for that, the recommended alternative is to spawn an offline process, which can be integrated into a webapp by starting and ending it using a ServletContextListener.

If you don't need web interfacing, there are other alternatives. You can run a completely stand-alone Java application without modification by using an OS-supplied program scheduler (cron, for Linux, Unix, and MacOS or the Windows Scheduler for Windows). If you want a general non-web container to run an application (or multiple applications) based on a schedule, you might want to look at a general process container such as Apache Karaf.
 
Muthuraj Kv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your detailed explanation.

I have scheduled few jobs in Windows scheduler and that works for me. In this case, I need to get the response from the user on the pop up window and use it in the application. Can I achieve this by scheduling a job in Windows Scheduler or what am I supposed to do?
 
Tim Holloway
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your expectations are mis-matched to the platform.

No webapp server can spontaneously pop up a dialog on a user's desktop. Webapp servers can only operate in a strict request/response mode. To get something (a response) from a webapp server, the client must make a request. It's a strict 1-to-1 correspondence. One response per request. Not like a time-sharing type app where you connect and stay connected and either side can send messages to the other side spontaneously.

You CAN design a webapp such that an app webpage contains polling of a resource for status. For full-page refresh, there's a META HTML tag that has been available since forever. News websites like to use this tag, since they often do periodic headline refreshes. For situations where you don't want to fetch an entire page over and over, you can put an AJAX call on the status webpage.

To interface these mechanisms is not difficult, but the exact mechanism depend on how your overall system architecture is designed. A very simple way of doing it would be to simply have the scheduled batch process create a file named "C:\temp\mybatch-done" with no data in it, have the webpage logic check for when this file has been created and display whether or not the "mybatch-done" file exists (and thus the process status is "Done"). Variations on that include putting a percent-complete value in the monitored file, running multiple monitor files for multiple scheduled processes, using a database or other mechanism to hold the status instead of a temp file, and so forth and so on.
 
Sheriff
Posts: 67503
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And bear in mind that your user must have your page displayed in his or her browser for any polling technique to work.

What you really need is a standalone agent that the user installs and runs on their system. Whether your users will do that or not (I certainly would not unless your company is very trusted) is a business decision.

The bottom line is that your web application, as Tim pointed out, is not the place to be doing this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic