使用Java Mailer API和Actor发送邮件

时间:2016-03-19 15:58:54

标签: java playframework akka

我正在构建使用java邮件api在游戏2.4中发送邮件的应用程序。现在我想让这个服务异步和非阻塞。 实现异步和非阻止的最佳做法是什么?我在scala中找到了一些例子,但我想在java中实现。我使用actor模型实现了邮件服务但是我收到了错误。以下是我的代码

 public class MailerActor extends UntypedActor {
    MailerClient mailerClient;
    @Inject
    public MailerActor(MailerClient mailerClient) {
        System.out.println("inside MailerActor constructor");
        this.mailerClient = mailerClient;
    }
    @Override
    public void onReceive(Object message) throws Exception {
        if(message instanceof NewRequest) {
            Email email = new Email();
            email.setSubject("Activation Link");
            email.setFrom("rec@gmail.com");
            email.addTo("receiver@gmail.com");
            email.setBodyText("hello");
            mailerClient.send(email);
        }

    }
    public static Props props(final MailerClient mailerClient) {
        return Props.create(new Creator<MailerActor>() {
          private static final long serialVersionUID = 1L;
          @Override
          public MailerActor create() throws Exception {
            return new MailerActor(mailerClient);
          }
        });
      }
}

我使用下面的代码

来调用这个演员
 class Controller {
    @Inject MailerClient mailerClient;
        final ActorSystem actorSystem = ActorSystem.create();
        final ActorRef mailerActor = actorSystem.actorOf(MailerActor.props(mailerClient));

public Result sendMail(){ 
    NewRequest newRequest = new NewRequest();
    mailerActor.tell(newRequest, mailerActor);
    return ok("done");
}

以下是我的配置

play{
  modules {
    enabled += "play.api.libs.mailer.MailerModule"
    #enabled += "play.api.db.DBModule"
    #enabled += "play.api.db.HikariCPModule"
  }

  mailer {
      host=smtp.gmail.com
      port=465
      ssl=yes
      tls=no
      user="sender@gmail.com"
      password="axy"
      debug=yes
  }
}

我正在接受例外

[ERROR] [03/19/2016 21:20:54.473] [default-akka.actor.default-dispatcher-5] [akka://default/user/$a] null
java.lang.NullPointerException
    at actors.MailerActor.onReceive(MailerActor.java

0 个答案:

没有答案