Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Email send retry

Thread view: 
Monty - 01 Jun 2006 12:00 GMT
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;
       }
   }
    //////////////////////////////////////////////
}
Alex Hunsley - 01 Jun 2006 15:27 GMT
> 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

Why don't you try actually compiling and running it? We're not a
compiler here! It looks ok on a very quick look, but I haven't looked at
it in depth.
Alex Hunsley - 01 Jun 2006 15:28 GMT
>> 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
[quoted text clipped - 5 lines]
> compiler here! It looks ok on a very quick look, but I haven't looked at
> it in depth.

Actually, scrub "it looks ok" at first look, I've not given the code
enough attention to warrant that!
Monty - 04 Jun 2006 17:57 GMT
I've not given the code
> enough attention to warrant that!

Actually i don't know much about the Java garbage collector, the code
given creates a new Object every time a timer expires (which might be a
few seconds) how does the java garbage collector know when i am through
with the object, when will it delete it, can i delete an object
forcibly ?

> >> 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
[quoted text clipped - 8 lines]
> Actually, scrub "it looks ok" at first look, I've not given the code
> enough attention to warrant that!
Owen Jacobson - 05 Jun 2006 06:43 GMT
> I've not given the code
>> enough attention to warrant that!
[quoted text clipped - 4 lines]
> with the object, when will it delete it, can i delete an object
> forcibly ?

In reverse order:

- No, you can't forcibly delete an object.  To do so would risk creating
  "invalid" references that point to a deleted object.

- Broadly speaking the garbage collector starts with all references
  (variables, members, statics, &c) that are directly reachable from a
  thread and marks those as "reachable", then repeats the process for
  every reachable object until it reaches the end of the reachable object
  graph.  Any objects not reachable from a live thread are eligible for
  garbage collection.

  So as soon as you stop referencing an object (eg., because the only
  reference was in a function that's since returned) it might be GCed.

Oh, and don't top-post.
Gordon Beaton - 01 Jun 2006 15:51 GMT
> 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

Here's a suggestion: your MTA (i.e. your mail server) already does
this for you, so just let it do its job.

Just deliver the mail to your local mail server and *it* will keep
trying for (normally) 5 days. It will even send you a warning after a
few hours telling you what's going on.

What specific kind of failures are you trying to recover from?

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Timo Stamm - 08 Jun 2006 18:22 GMT
Gordon Beaton schrieb:
>> 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
[quoted text clipped - 4 lines]
> Here's a suggestion: your MTA (i.e. your mail server) already does
> this for you, so just let it do its job.

Absolutely!

Depending on the application, it might be an option to use a bounce
management that analyzes _incoming_ mails for bounce messages. This is a
huge help in maintaining customer data in a newsletter system, for example.

Timo
Greg R. Broderick - 08 Jun 2006 15:52 GMT
"Monty" <xmonty@gmail.com> wrote in news:1149159613.164843.274140
@j55g2000cwa.googlegroups.com:

> I have to write code to send an email and if for some reason it is not
> sent it should keep retrying

Depends on what sort of result code is returned by the recipient mail
server, see RFC 2822 for details.  In brief, I would probably requeue and
retry a mail if the result code is 4xx (indicates transient failure), but
would not automagically retry sending a mail if the result code is 5xx,
indicating a permanent failure.

Cheers
GRB


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.