I'm working on a project should download some files. If a problem happened to the internet connection, it will be catch an exception. When this happened I should send an email to some people.
I need to send the email, but no internet connection, so I have 2 ideas:
1- trying to send the email, but because I don't have a connection, I need to save the email until the connection return back and send it again.
2- making a thread and check if the connection is stable, and with a condition if the internet is stable I will send the email.
I have another idea to make an infinity loop to check the internet connection and send the email and end the loop when connection is back.
Any one can help with that?
There isn't really a need to add a check for a stable internet connection. Just keep trying to send the email until it succeeds. The logic for your email notification thread seems like it would be very simple:
long RETRY_DELAY = 60*1000;
boolean emailSent = false;
while(!emailSent ) {
emailSent = sendEmail();
if (!emailSent) {
Thread.sleep(RETRY_DELAY);
}
}
The sendEmail()
method should return false if there was any issue sending the email. E.g. exceptions related to networking.
You can set the eventual status in some other object and add a maximum number of retries, or maximum length of time to continue retrying until you give up etc. Those would just add more conditions to terminate the loop. You can also interrupt that emailer thread if there is a manual abort.