Pages

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

7 comments:

  1. This information you provided in the blog that was really unique I love it!!, Thanks for sharing such a great blog..Keep posting..

    JAVA Training in Chennai

    ReplyDelete
  2. wow, thx it's working (:

    Best regards
    Toby, data room software

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Online Java Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

    ReplyDelete
  4. Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.

    Online Training for Big Data
    best Apache Spark online course

    ReplyDelete
  5. New players depositing with crypto can grab a 125% match bonus up to as} $1250, which is legitimate 3 times. It’s the identical for fiat deposits solely they get 100 percent up to as} $1000. That mentioned, we’d prefer to see more game 카지노 studio partnerships in the future here. The 150 mobile video games is sufficient for players in search of a basic on line casino library, but a more constantly refreshed ‘new games’ menu would enhance this on line casino a lot.

    ReplyDelete