liulingling.177216
2024-08-26 349f1cfc5fa77fbc636d542df0d8050fddec48c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.dingzhuo.compute.engine.actor.alarm;
 
import akka.actor.ActorSystem;
import akka.actor.Cancellable;
import akka.actor.UntypedAbstractActor;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
 
/**
 * @author fanxinfu
 */
@Component("saveAlarmActor")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SaveAlarmActor extends UntypedAbstractActor {
 
  private Cancellable timer;
  private final ActorSystem actorSystem;
 
  public SaveAlarmActor(ActorSystem actorSystem) {
    this.actorSystem = actorSystem;
  }
 
  @Override
  public void preStart() throws Exception {
    super.preStart();
    FiniteDuration interval = Duration.create(1, TimeUnit.SECONDS);
    FiniteDuration delay = Duration.Zero();
    timer = actorSystem.scheduler()
        .scheduleAtFixedRate(delay, interval, self(), Message.TIMER, actorSystem.dispatcher(),
            self());
  }
 
  @Override
  public void postStop() throws Exception {
    super.postStop();
    if (timer != null) {
      timer.cancel();
    }
  }
 
  @Override
  public void onReceive(Object message) throws Throwable {
 
  }
 
  private enum Message {
    /**
     * 时间触发
     */
    TIMER
  }
}