This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes can a servlet be proactive? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "can a servlet be proactive?" Watch "can a servlet be proactive?" New topic
Author

can a servlet be proactive?

sean divine
Greenhorn

Joined: Oct 27, 2005
Posts: 14
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?

Thanks for any suggestions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39541
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
sean divine
Greenhorn

Joined: Oct 27, 2005
Posts: 14
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).
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

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,
Ram.
[ November 28, 2005: Message edited by: ramprasad madathil ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

For a quick comparison between Quartz and java.util.Timer, read this article:
http://www.onjava.com/pub/a/onjava/2004/03/10/quartz.html


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

Joined: Oct 27, 2005
Posts: 14
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

These are actually good concerns to have.

One issue to watch for is to make sure that, however you implement it, you use daemon threads.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#isDaemon()
Non-daemon threads could cause issues when trying to restart your app and/or container. ("issues" being that they don't die with the app/container and can cause the shutdown to hang)

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.
sean divine
Greenhorn

Joined: Oct 27, 2005
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can a servlet be proactive?
 
Similar Threads
How to Book for the Exam?
Sharpen PDFs posted
dealing with nontransactional web services
Visiting two pages for one action
Java, J2EE, Swing/SWT Developer (Work From Home)