我正在开发一个使用Play-Scala框架和akka actor的应用程序,以便在发生数据库的CRUD操作时发送电子邮件。重启服务器时遇到问题。
当Play框架重新启动或持久化akka调度程序时重新加载akka调度程序的任何方法,当重新启动应用程序时,它将运行调度程序。这是我的代码。
@Singleton
class SendEmailSaveDraftActor @Inject()(system: ActorSystem,
config: Configuration,
mailService: MailService)(implicit exec: ExecutionContext) extends Actor {
val userSchedules: mutable.HashMap[String, Seq[Cancellable]] = mutable.HashMap()
private val emailAdmin = config.getString("email.admin").getOrElse("")
private val interval24Hours = config.getInt("batch.saveDarf24.extractIntervalHours").getOrElse(1)
private val interval72Hours = config.getInt("batch.saveDarf72.extractIntervalHours").getOrElse(1)
private val interval14Days = config.getInt("batch.saveDarf14.extractIntervalDays").getOrElse(1)
def receive = {
case s: SendEmailSaveDrafModel =>
userSchedules.get(s.userName) match {
case Some(schedules) =>
schedules.map(_.cancel())
userSchedules.update(s.userName, getSchedules(s))
case None =>
userSchedules += (s.userName -> getSchedules(s))
}
case userName: String =>
userSchedules.get(userName) match {
case Some(schedules) =>
schedules.map(_.cancel())
case None =>
}
}
def getSchedules(s: SendEmailSaveDrafModel): Seq[Cancellable] = {
val schedule1 = system.scheduler.scheduleOnce(5 minutes) {
mailService Send EmailSaveDraft24Hours(to = Seq(s.to), bccEmails = Seq(emailAdmin),id = s.id, orderNo = s.orderNo,designId = s.designId)
}
val schedule2 = system.scheduler.scheduleOnce(10 minutes) {
mailService Send EmailSaveDraft72Hours(to = Seq(s.to), bccEmails = Seq(emailAdmin),id = s.id, orderNo = s.orderNo,designId = s.designId)
}
val schedule3 = system.scheduler.scheduleOnce(15 minutes) {
mailService Send EmailSaveDraft14Days(to = Seq(s.to), bccEmails = Seq(emailAdmin),id = s.id, orderNo = s.orderNo,designId = s.designId)
}
Seq(schedule1, schedule2, schedule3)
}
}
答案 0 :(得分:0)
我正在使用akka调度程序,如下面的代码所示。
每个用户都可以登录我的应用程序并可以为后台任务创建调度程序。为此,我正在使用Akka调度程序。
public Cancellable buildScheduler(String actorName, SchedulerActorMessage message, long interval, TimeUnit timeUnit, long initialDelay, String actorMapKey) {
ActorRef daemonRef = actorSystem.actorOf(Props.create(SchedulerActor.class), actorName);
Cancellable cancellableActor = actorSystem.scheduler().schedule(FiniteDuration.apply(initialDelay, timeUnit),
FiniteDuration.apply(interval, timeUnit), daemonRef, message,
actorSystem.dispatcher(), ActorRef.noSender());
actorMap.put(actorMapKey, cancellableActor);
return cancellableActor;
}
我正在数据库中存储必要的信息,当应用程序启动时,我再次在全局类中启动调度程序。