0

I want to send email just after user registering himself. Here is the scenario:

  • User will submit sign up form
  • Spring controller will accept the request, insert the data into db, send email to user.

The thing is user should get the successful sign up message instantly and the email sending process should also run in parallel but it should not affect sign up success response. i.e delay in email is accepted but sign up success message response should be delayed because of email process.

4

2 回答 2

2

The Spring way to do that would be to use an Async service to send the email:

The @Async annotation can be provided on a method so that invocation of that method will occur asynchronously. In other words, the caller will return immediately upon invocation and the actual execution of the method will occur in a task that has been submitted to a Spring TaskExecutor.

于 2013-10-05T07:11:08.013 回答
0

If you doesn't care about transactions (email should be send even if database insert was failed) you can parallelize the execution with Execution framework by example:

ExecutorService executor = Executors.newFixedThreadPool(2);

executor.execute(new DbInsertRunnable());
executor.execute(new EmailSendingRunnable());

// This will make the executor accept no new threads
// and finish all existing threads in the queue
executor.shutdown();
// Wait until all threads are finish
executor.awaitTermination(); 

You can find more about concurrency here http://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html

于 2013-10-05T07:21:04.857 回答