Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Write a cron job to call struts action class

 
Komal Khanna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Komal Khanna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to call my struts action class from the crontab???
 
Komal Khanna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would appreciate if someone can give small example on this.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Komal Khanna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic