hello Poornima,$
Hope this helps.
import ;
public class ThreadTest extends HttpServlet implements Runnable
{
PrintWriter out = null;
private static boolean m_bIsRunning = false;
private static final int BATCH_RUN_TIME = 2; //every morning 2 O'clock
boolean isFirstTimeRunning = true;
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException
{
System.out.println("Inside doPost ");
resp.setContentType("text/html");
out = new PrintWriter(resp.getOutputStream());
HttpSession mySession = req.getSession(true);
if ( m_bIsRunning == true ) {
System.out.println("already running ..returning .." );
//do what ever u want
out.flush();
return;
}
m_bIsRunning = true;
mySession.setMaxInactiveInterval(-1);
while ( checkCondition() ) {
try {
System.out.println("Printed at + getCurrentDate() + " " +
getCurrentTime() + " );
//to do code
} catch ( Exception e) {
out.println( "error caught ..<BR></BR>");
}
}
/*
if u put the code in run method use this..
Thread t = new Thread(this, "Test Thread");
t.start();*/
}
private boolean checkCondition() {
try {
int iWaitingTimeInMinutes = 0;
if ( isFirstTimeRunning ) {
String sCurrentTime = getCurrentTime(); // HH:MM
String sHour = sCurrentTime.substring(0,2);
String sMinutes = sCurrentTime.substring(3,5);
int iHour = ( sHour.substring(0,1).equals("0") ) ?
Integer.parseInt(sHour.substring(1,2)) :
Integer.parseInt(sHour.substring(0,2)) ;
int iMinutes = ( sMinutes.substring(0,1).equals("0") ) ?
Integer.parseInt(sMinutes.substring(1,2)) :
Integer.parseInt(sMinutes.substring(0,2)) ;
if ( iHour == 0 ) {
iWaitingTimeInMinutes = ( BATCH_RUN_TIME * 60 ) - iMinutes
;
} else if ( iHour > 0 && iHour < BATCH_RUN_TIME ) {
iWaitingTimeInMinutes = ( ( BATCH_RUN_TIME - iHour) * 60 ) -
iMinutes;
} else {
iWaitingTimeInMinutes = ( ((24 - iHour) + BATCH_RUN_TIME) *
60 ) - iMinutes ;
}
isFirstTimeRunning = false;
} else {
iWaitingTimeInMinutes = 24 * 60;
}
Thread.sleep(1000 * 60 * iWaitingTimeInMinutes );
} catch ( Exception e) {
}
return true;
}
public void run() {
System.out.println("inside run method ");
//u can put the code in either dummy while loop in doPost method or in
run method
}
}
********************* end of program
all the best.
ram
>From: Paul McKernon <[EMAIL PROTECTED]>
>Reply-To: "A mailing list for discussion about Sun Microsystem's Java
> Servlet API Technology." <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: Automatic Servlet Execution?
>Date: Fri, 23 Feb 2001 10:06:46 +0000
>
>This is also totally irrelevant to a Servlet Forum. Asking Java related
>questions and tagging on 'from a servlet' is wasting bandwidth. Im sure
>there
>are many excellent Java forums out there. Asking about things like
>DB connectivity and Applets is fine but pure Java questions is too much.
>
>Anyway :-
>
>Can you not just keep the program running all the time and simply check the
>time? When a certain time comes send the mails. I realise this is somewhat
>inefficient but if it was implemented like (pseudocode):
>
>while(true)
>{
> check time
>
> if time is specified time (e.g. 13.00)
> {
> // check db
>
> // send mails
> }
> else
> {
> sleep for 30 mins
> }
>}
>
>You could change the resolution from 30 mins to 5 mins if necessary but it
>wouldnt be too inefficient and would be nice and easy to implement.
>
>
>-Paul
>
>
>
> > > ----- Original Message -----
> > > From: poornima visvesvaran <[EMAIL PROTECTED]>
> > > To: <[EMAIL PROTECTED]>
> > > Sent: Friday, February 23, 2001 12:54 PM
> > > Subject: Automatic Servlet Execution?
> > >
> > >
> > > Hi Friends,
> > >
> > > I want a servlet to be executed automatically
> > > everyday
> > > (same time) to check in the database and send mails
> > > to
> > > some people.
> > >
> > > Is it possible to run the servlet like mentioned
> > > above? If yes, How?
> > >
> > > Can you please send with details immediately.
> > >
> > > Thanks in advance.
> > >
> > > Poornima Visvesvaran.
>
>___________________________________________________________________________
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html