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.

