M
Monty
I have to write code to send an email and if for some reason it is not
sent it should keep retrying, i am new to java but coded for a few
years in c++ so i jot down kinda rough algorithm
can you guys see the below code and comment whether it looks ok or not,
any suggestions are welcome
--------------------
import java.util.Timer;
import java.util.TimerTask;
// Usage
// EmailSender Sender = new EmailSender();
// Sender.sTo =
// Sender.sFrom =
// Sender.sSubject =
// Sender.sBody =
// Sender.SendMail();
public class EmailSender {
//Email Related Data
String sTo;
String sFrom;
String sSubject;
String sBody;
//End
//Tries related data
int nNumTries;
int nRetryTimeOut; //in secs
//end
Timer timer;
public int SendMail()
{
//mail send logic here
//if failed
MailSendRetryTask Task = new MailSendRetryTask();
timer.schedule(Task,nRetryTimeOut * 1000);
return 0;
//if success
//Need to delete this object here, no idea how this happens in java
garbage collector might interfere
return 1;
}
public EmailSender() {
nNumTries = 0;
nRetryTimeOut = 10; //10 seconds
timer = new Timer();
}
//////////////////////////////////////////////////
//Timer Class
//run function will be called automatically by timer proc
///////////////////////////////////////////
class MailSendRetryTask extends TimerTask {
public void run() {
timer.cancel(); //Terminate the timer thread
nNumTries ++;
SendMail(); //Send mail will send the mail if it cannot for some
reason it will create another timer
//Need to delete this object here
//no sure how to do that in java
//in c++ delete this;
}
}
//////////////////////////////////////////////
}
sent it should keep retrying, i am new to java but coded for a few
years in c++ so i jot down kinda rough algorithm
can you guys see the below code and comment whether it looks ok or not,
any suggestions are welcome
--------------------
import java.util.Timer;
import java.util.TimerTask;
// Usage
// EmailSender Sender = new EmailSender();
// Sender.sTo =
// Sender.sFrom =
// Sender.sSubject =
// Sender.sBody =
// Sender.SendMail();
public class EmailSender {
//Email Related Data
String sTo;
String sFrom;
String sSubject;
String sBody;
//End
//Tries related data
int nNumTries;
int nRetryTimeOut; //in secs
//end
Timer timer;
public int SendMail()
{
//mail send logic here
//if failed
MailSendRetryTask Task = new MailSendRetryTask();
timer.schedule(Task,nRetryTimeOut * 1000);
return 0;
//if success
//Need to delete this object here, no idea how this happens in java
garbage collector might interfere
return 1;
}
public EmailSender() {
nNumTries = 0;
nRetryTimeOut = 10; //10 seconds
timer = new Timer();
}
//////////////////////////////////////////////////
//Timer Class
//run function will be called automatically by timer proc
///////////////////////////////////////////
class MailSendRetryTask extends TimerTask {
public void run() {
timer.cancel(); //Terminate the timer thread
nNumTries ++;
SendMail(); //Send mail will send the mail if it cannot for some
reason it will create another timer
//Need to delete this object here
//no sure how to do that in java
//in c++ delete this;
}
}
//////////////////////////////////////////////
}