1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.cluster.hasingleton.service.ejb;
18
19 import java.util.Date;
20 import java.util.concurrent.atomic.AtomicBoolean;
21
22 import javax.naming.InitialContext;
23 import javax.naming.NamingException;
24
25 import org.jboss.logging.Logger;
26 import org.jboss.msc.service.Service;
27 import org.jboss.msc.service.ServiceName;
28 import org.jboss.msc.service.StartContext;
29 import org.jboss.msc.service.StartException;
30 import org.jboss.msc.service.StopContext;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class HATimerService implements Service<String> {
47 private static final Logger LOGGER = Logger.getLogger(HATimerService.class);
48 public static final ServiceName SINGLETON_SERVICE_NAME = ServiceName.JBOSS.append("quickstart", "ha", "singleton", "timer");
49
50
51
52
53 private final AtomicBoolean started = new AtomicBoolean(false);
54
55
56
57
58 public String getValue() throws IllegalStateException, IllegalArgumentException {
59 LOGGER.infof("%s is %s at %s", HATimerService.class.getSimpleName(), (started.get() ? "started" : "not started"), System.getProperty("jboss.node.name"));
60 return "";
61 }
62
63 public void start(StartContext arg0) throws StartException {
64 if (!started.compareAndSet(false, true)) {
65 throw new StartException("The service is still started!");
66 }
67 LOGGER.info("Start HASingleton timer service '" + this.getClass().getName() + "'");
68
69 final String node = System.getProperty("jboss.node.name");
70 try {
71 InitialContext ic = new InitialContext();
72 ((Scheduler) ic.lookup("global/jboss-cluster-ha-singleton-service/SchedulerBean!org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.Scheduler")).initialize("HASingleton timer @" + node + " " + new Date());
73 } catch (NamingException e) {
74 throw new StartException("Could not initialize timer", e);
75 }
76 }
77
78 public void stop(StopContext arg0) {
79 if (!started.compareAndSet(true, false)) {
80 LOGGER.warn("The service '" + this.getClass().getName() + "' is not active!");
81 } else {
82 LOGGER.info("Stop HASingleton timer service '" + this.getClass().getName() + "'");
83 try {
84 InitialContext ic = new InitialContext();
85 ((Scheduler) ic.lookup("global/jboss-cluster-ha-singleton-service/SchedulerBean!org.jboss.as.quickstarts.cluster.hasingleton.service.ejb.Scheduler")).stop();
86 } catch (NamingException e) {
87 LOGGER.error("Could not stop timer", e);
88 }
89 }
90 }
91 }