Can a servlet only act as a passive responder? Or is it possible for a servlet to undertake a periodic housekeeping procedure on it's own initiative? My understanding is that a servlet is only passive.
Here is my situation...
I'm attempting to interface a shopping cart with paypal mechanism for an artist who makes rice paper lamp shades. Because every shade is unique, I want to be careful to avoid taking money from two or more buyers for the same shade.
And for this reason, I don't want to wait until Paypal sends back an order confirmation before making a shade unavailable to other buyers. i have no control over how long the process may take on their end and could feasibly be sending another order to paypal for the same shade before the confirmation comes thru.
So instead, I will make a shade unavailable to other buyers at the exact moment that the order gets sent to paypal. The problem with this approach of course is that if the buyer get's cold feet while on the paypal website and never completes the order, the shade will still be unavailable to other buyers.
To overcome this, I plan to record every order that is sent to paypal in a database. Every recorded order will be date stamped and will be initially marked as unconfirmed. If paypal responds indicating the transaction was completed, the order will be marked as confirmed.
However, if the transaction is not processed by paypal within 3 days, I want the items in the order to be made available again. To do this, I will set up hidden housekeeping procedures on two pages in the site. If someone requests either of these two pages, a servlet will check the orders database to see if there are any orders that were placed at least 3 days ago that are still unconfirmed. The order would then be marked as canceled and the items would again be made available in the inventory database.
This will probably work but it seems like a bit of a hack solution. It relies on someeone hitting one of the two pages on the site. Is there a way to arrange for the housekeeping to be done periodically whether or not anyone visits the trigger pages?
The java.util.Timer and TimerTask classes allow you to schedule regularly recurring events, which are independent on anyone hitting on a servlet. If you're running those inside a servlet container, you could start them up initially from a servlet that runs at container startup time, and then never again.
Joined: Oct 27, 2005
I did think of that option briefly. Doesn't this involve having the server count in increments of 1/1000 of a second? Wouldn't this require continous counting in the background forever? And would this drag down the server in any way? The site is hosted on a shared jsp server (Resin).
Recommend that you take a look at this post. I had posted a similar question there. Alternatively you could use the quartz scheduler. However that would only take the headache out of writing the scheduler itself. It would still use threads that runs periodically at a configurable time interval.
Thanks very much for these excellent recommendations. All are very useful and interesting. And thanks for the link to the article, Ben. Both the Quartz and the java.util.Timer approaches look like they could satisfy my scheduling requirements.
However, I'm a little uneasy about using persistant threads in a shared JVM where I can't control restarts and the administrators frown on any loose ends running in the background. Unfortunately, the artist for whom I'm building the site can't afford a private JVM at this point.
Although the article mentioned above detailed two very good solutions to my problem, I couldn't find anything in it that addressed my concerns about running either option on a shared JVM. But my grasp of the mechanism is limited and maybe this concern is unwarranted.
Does anyone have any insight into the risks of running these solutions on a shared JVM?
Unless I'm led to believe that this won't be a possible problem, I'm inclined to pursue my "hack" approach in which the housekeeping procedure only occurs when a user requests a certain page.
Another alternative would be to put this timed code into a servlet and invoke it over HTTP from either a Unix cron job or a Windows Task. If the server is hosted on a Linux box, there is a good chance that it's already got wget installed which means your whole timer can be written with one line of code. If not, it would be pretty easy to write a small command line java app that uses java.util.URLConnection to hit the servlet.
Joined: Oct 27, 2005
Thanks for these other suggstions, Ben. I looked into some of the features available on the server we are using. The administrators disable wget for "security reasons" but there is a cron manager available on their control panel. I'll take a closer look at how I might use this. Could be just the ticket. Thanks for all your great advice.