aspose file tools*
The moose likes Linux / UNIX and the fly likes Write a cron job to call struts action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Write a cron job to call struts action class" Watch "Write a cron job to call struts action class" New topic
Author

Write a cron job to call struts action class

Komal Khanna
Greenhorn

Joined: May 05, 2008
Posts: 5
I would like to write a cron job in the Linux server that calls my foo.action class that has code that I want to run once in a day. How can I do that?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This was the first entry in a Google Search for "crontab tutorial".
http://www.adminschoice.com/docs/crontab.htm

Combine this with the wget program and you should be able to do this in one line.
[ May 05, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Komal Khanna
Greenhorn

Joined: May 05, 2008
Posts: 5
I want to call my struts action class from the crontab???
Komal Khanna
Greenhorn

Joined: May 05, 2008
Posts: 5
I would appreciate if someone can give small example on this.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

Komal,
First let's figure out what you want to run. You could connect to the URL that calls your Struts action. You could call a Java class with a main method that calls your Struts action.

This means the first step is to come up with one line that runs what you want. It could be "java MyProgram" or a UNIX script/program. wget allows you to make a http connection which would connect to the URL.

Once you have that working, the link Ben provided shows how to set up a cron job.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Just in case there are people who missed the point:

You cannot "call a struts action class", via crontab or not. Struts is a web application framework, and struts action classes are defined in terms of that framework, so what you actually would need to to is invoke the framework.

Making a long story short, that means that you have to make a web request to the server containing a Struts app which employs the action class. You can use tools like wget or curl to do that. In fact, I'm in the process of setting up a cron job to pull in a morning newspaper (PDF) on a daily basis using that technique right now. It can be as simple as this:

wget -O /home/sales/dailyspecials.txt http://localhost/myapp/todaysspecials.do

(I hope the forum software didn't break that into 2 lines.)

One of the reasons why it's considered bad practice to put business logic into Struts action processors (or JSF backing beans) is exactly because you might want to be able to invoke those business functions outside of the original framework.

For those of your who Already Knew All This, I apologize. But I've discovered that enough people don't realize things like this have to be allowed for that I felt obliged to point it out explicitly.


Customer surveys are for companies who didn't pay proper attention to begin with.
Komal Khanna
Greenhorn

Joined: May 05, 2008
Posts: 5
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write a cron job to call struts action class
 
Similar Threads
cron job
Delete the log using cron job
command to run a cron job
Execute Servlet At Given Time Instance
Cron job and Action class