Scheduling

Good day!

I磎 trying to schedule an email sending in a servlet. The email should be sent after one day.

The servlet:

publicclass EmailSendingextends HttpServlet{

private File dirEmailTimerTask;

publicvoid init(){

dirEmailTimerTask =new File("D:\\test\\");

}

publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

doPost(request, response);

}

publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

try{

SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd-HH:mm:ss");

GregorianCalendar gregCalendarStart =new GregorianCalendar();

GregorianCalendar gregCalendarExecution = (GregorianCalendar) gregCalendarStart.clone();

gregCalendarExecution.add(GregorianCalendar.DATE, 1);

runProgram("java -cp " + dirEmailTimerTask.getAbsolutePath()

+" com.vion.EmailTimerTask "

+ sdf.format(gregCalendarStart.getTime()).toString() +" "

+ sdf.format(gregCalendarExecution.getTime()).toString() +" "

+"test@vion.com",

null,null);

}catch (Exception e){

e.printStackTrace();

}

}

privatevoid runProgram(String command, String[] envp, File directory)

throws IOException, InterruptedException{

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(command, envp, directory);

AuxiliarExec output =new AuxiliarExec(proc.getInputStream(),"OUTPUT");

output.setDaemon(true);

output.start();

}

}

The AuxiliarExec class:

package com.vion;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

publicclass AuxiliarExecextends Thread{

private InputStream objInputStream;

private String type;

public AuxiliarExec(InputStream is, String tipo)throws IOException{

this.objInputStream = is;

this.type = tipo;

}

publicvoid run(){

try{

InputStreamReader isr =new InputStreamReader(objInputStream);

BufferedReader br =new BufferedReader(isr);

String line =null;

while ((line = br.readLine()) !=null){

System.out.println(type +">" + line);

}

}catch (Exception e){

e.printStackTrace();

}

}

}

The Email sending program:

package com.vion;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

publicclass EmailTimerTaskextends TimerTask{

private String start;

private String momentOfExecution;

private String emailTo;

public EmailTimerTask(String start, String moment, String emailTo){

this.start = start;

this.momentOfExecution = moment;

this.emailTo = emailTo;

}

publicstaticvoid main(String args[]){

try{

System.out.println("### Staring EmailTimerTask ###");

System.out.println("### args[0] [" + args[0] +"]");

System.out.println("### args[1] [" + args[1] +"]");

System.out.println("### args[2] [" + args[2] +"]");

SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd-HH:mm:ss");

Date dateOfExecution = sdf.parse(args[1]);

System.out.println("### dateOfExecution [" + dateOfExecution.toString() +"]");

EmailTimerTask testeTimerTask =new EmailTimerTask(args[0], args[1],args[2]);

Timer timer =new Timer();

timer.schedule(testeTimerTask, dateOfExecution);

}catch (Exception e){

System.out.println("### ERROR IN MAIN!!!");

e.printStackTrace();

}

}

publicvoid run(){

System.out.println("### Executing EmailTimerTask ###");

System.out.println("### start [" + start +"]");

System.out.println("### momentOfExecution [" + momentOfExecution +"]");

System.out.println("### emailTo [" + emailTo +"]");

sendEmail(emailTo);

}

}

The problem is that when Tomcat is stopped all the scheduling is broken. I am trying to run the email sending process without depending on the Tomcat. How?

Thank you.

[8150 byte] By [TheManFromJavaa] at [2007-11-27 9:58:03]
# 1
Why don't you store your data in some persistent type of storage and check the storage each n minutes/hours?
_helloWorld_a at 2007-7-13 0:28:29 > top of Java-index,Java Essentials,Java Programming...
# 2
> Why don't you store your data in some persistent type> of storage and check the storage each n> minutes/hours?This is exactly the thing I do not want to do.
TheManFromJavaa at 2007-7-13 0:28:29 > top of Java-index,Java Essentials,Java Programming...
# 3
Why? And what are you doing at the moment, holding all the info in memory?
_helloWorld_a at 2007-7-13 0:28:29 > top of Java-index,Java Essentials,Java Programming...
# 4

> Why? And what are you doing at the moment, holding

> all the info in memory?

There will not be many emails to be sent, so memory usage will not be the problem. And there will not be too much data in memory as well.

In summary, as you can notice, I am trying to get a java.lang.Process independent from Tomcat process. Do you know how?

TheManFromJavaa at 2007-7-13 0:28:29 > top of Java-index,Java Essentials,Java Programming...
# 5
Can you use cookies here to store last time. ?.I never tried but ...
Adi1000a at 2007-7-13 0:28:29 > top of Java-index,Java Essentials,Java Programming...