About Rajith Delantha

Rajith is an software engineer, open source contributor and love to develop application based on open source projects.

Sending Email with Java and Akka actors

Akka is a concurrent framework written by Scala.

Here I demonstrate sample application to send emails with Akka and implemented in Java.

Reasons I decided to use Akka framework other than concurrency.

  • Built-in configurable supervisor strategy to monitor child workers and decide what policy applies when there is an exception.
  • Can reschedule delivery when application throwing some specific exception.
  • Use of actor routers and allow them to use actor connection pool.

Here is how to create supervision strategy.

class EmailServiceActor extends UntypedActor {  
   private static SupervisorStrategy strategy =  
       new OneForOneStrategy(10, Duration.create("1 minute"),  
           new Function<Throwable, Directive>() {  
             public Directive apply(Throwable t) {  
               if (t instanceof MessagingException) {  
                 return resume();  
               } else if (t instanceof Exception) {  
                 return stop();  
               } else {  
                 return escalate();  
   public void onReceive(Object message) {  
     getContext().actorOf(new Props(EmailServiceWorker.class)).tell(message, self());  
   public SupervisorStrategy supervisorStrategy() {  
     return strategy;  

Here is how child worker create

class EmailServiceWorker extends UntypedActor {  
   public void onReceive(Object message) {  
     try {  
       EmailService emailService = new EmailService();  
     } catch (IOException e) {  
       e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.  
     } catch (MessagingException e) {  
       e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.  
   public void preStart() {  
 //    getContext().system().scheduler().scheduleOnce(Duration.create(5, TimeUnit.SECONDS), self(), "emailWorker", getContext().system().dispatcher(), null);  
   public void postStop() {  

Sample application – https://github.com/rajithd/email-service-akka

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Leave a Reply

× 4 = twelve

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books