Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to handle success/failure in async mode (including testConnection()) #148

Closed
trathschlag opened this issue Jun 20, 2018 · 7 comments

Comments

@trathschlag
Copy link

Hi!
It would be really useful if you could somehow handle success and failures when sending asynchronously. Is it be possible to implement CompletableFuture as a return value for mailer.sendAsync(...) or something?
I am interested to prepare a pull request.

Thanks in advance!

@bbottema
Copy link
Owner

Hmm, Simple Java Mail uses JDK 1.7, so it would need to be something else. Any preference?

Also, this issue will be solved another way with #121 when you can add custom steps in between the various stages of emailing (including handling exceptions). However, that won't be in the near future.

@bbottema
Copy link
Owner

bbottema commented Jul 1, 2018

I've added Future to the API for release 6.0.0. To use it:

try {
	f.get(); // blocks
} catch (ExecutionException e) {
	System.err.println("Sending email failed");
	e.printStackTrace(System.err);
}

You can also implement your own (threaded) checking routine that checks f.isDone() on mutliple emails in your batch so your thread doesn't block for individual async emails being sent (otherwise what's the point of sending asynchronously).

Would this suit your needs?

@bbottema bbottema closed this as completed Jul 1, 2018
@bbottema bbottema self-assigned this Jul 1, 2018
@trathschlag
Copy link
Author

Hi, thanks for the reply!
I wanted to avoid Future as a solution, because there is no way to register a callback with it. You need to block or implement some polling mechanism, as you described.

Would it be possible to provide a basic interface

interface MailCallback {
    void onSuccess();
    void onFailure(Exception e);
}

which the user can implement and pass into mailer.sendAsync(callback)?
This way everyone can write their own wrappers for integration with JDK8/Guava/Kotlin/etc...

@bbottema bbottema reopened this Jul 17, 2018
@bbottema bbottema changed the title Provide a way to handle success/failure in async mode Provide a way to handle success/failure in async mode (including testConnection()) Jul 17, 2018
bbottema added a commit that referenced this issue Jul 21, 2018
…dition to Future. Also made testConnection support the async flag similarly
bbottema added a commit that referenced this issue Jul 21, 2018
@bbottema
Copy link
Owner

You got your wish @trathschlag!

Both sending emails and testing server connections can now be done asynchronously with either Future or Handlers.

Using Handlers

AsyncResponse asyncResponse = mailer.sendMail(email, true);
// or: mailer.testConnection(email, true);

asyncResponse.onSuccess(() -> System.out.println("Success"));
asyncResponse.onException((e) -> System.err.println("error"));

Using Future

Future<?> f = asyncResponse.getFuture();

// f.get() actually blocks until done, below is an example custom loop
// for checking result in a non-blocking way:
while (!f.isDone()) Thread.sleep(100);

// result is in, check it
try {
  f.get(); // without the above loop, this would actually block until done
  System.err.println("success");
} catch (ExecutionException e) {
  System.err.println("error");
}

@bbottema
Copy link
Owner

Released in 6.0.0-rc1!!

@bbottema
Copy link
Owner

6.0.0 has released as well, finally.

@bbottema
Copy link
Owner

bbottema commented Jan 2, 2022

7.0.0 just released, which is a switch to Java8 and I've replaced this system with CompletableFuture for #367.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants