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>() {  
             @Override  
             public Directive apply(Throwable t) {  
               if (t instanceof MessagingException) {  
                 return resume();  
               } else if (t instanceof Exception) {  
                 return stop();  
               } else {  
                 return escalate();  
               }  
             }  
           });  
   @Override  
   public void onReceive(Object message) {  
     getContext().actorOf(new Props(EmailServiceWorker.class)).tell(message, self());  
   }  
   @Override  
   public SupervisorStrategy supervisorStrategy() {  
     return strategy;  
   }  
 }

Here is how child worker create

class EmailServiceWorker extends UntypedActor {  
   @Override  
   public void onReceive(Object message) {  
     try {  
       EmailService emailService = new EmailService();  
       emailService.sendEmail();  
     } 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.  
     }  
   }  
   @Override  
   public void preStart() {  
 //    getContext().system().scheduler().scheduleOnce(Duration.create(5, TimeUnit.SECONDS), self(), "emailWorker", getContext().system().dispatcher(), null);  
   }  
   @Override  
   public void postStop() {  
   }  
 }

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


two × = 10



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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close