View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.cluster.hasingleton.service.ejb;
18  
19  import org.jboss.as.clustering.singleton.SingletonService;
20  import org.jboss.as.clustering.singleton.election.NamePreference;
21  import org.jboss.as.clustering.singleton.election.PreferredSingletonElectionPolicy;
22  import org.jboss.as.clustering.singleton.election.SimpleSingletonElectionPolicy;
23  import org.jboss.logging.Logger;
24  import org.jboss.msc.service.DelegatingServiceContainer;
25  import org.jboss.msc.service.ServiceActivator;
26  import org.jboss.msc.service.ServiceActivatorContext;
27  import org.jboss.msc.service.ServiceController;
28  
29  /**
30   * Service activator that installs the HATimerService as a clustered singleton service
31   * during deployment.
32   *
33   * @author Paul Ferraro
34   * @author Wolf-Dieter Fink
35   */
36  public class HATimerServiceActivator implements ServiceActivator {
37      private final Logger log = Logger.getLogger(this.getClass());
38  
39      @Override
40      public void activate(ServiceActivatorContext context) {
41          log.info("HATimerService will be installed!");
42  
43          HATimerService service = new HATimerService();
44          SingletonService<String> singleton = new SingletonService<String>(service, HATimerService.SINGLETON_SERVICE_NAME);
45          /*
46           * The NamePreference is a combination of the node name (-Djboss.node.name) and the name of
47           * the configured cache "singleton". If there is more than 1 node, it is possible to add more than
48           * one name and the election will use the first available node in that list.
49           *   -  To pass a chain of election policies to the singleton and tell JGroups to run the
50           * singleton on a node with a particular name, uncomment the first line  and
51           * comment the second line below.
52           *   - To pass a list of more than one node, comment the first line and uncomment the
53           * second line below.
54           */
55          singleton.setElectionPolicy(new PreferredSingletonElectionPolicy(new SimpleSingletonElectionPolicy(), new NamePreference("node1/singleton")));
56          //singleton.setElectionPolicy(new PreferredSingletonElectionPolicy(new SimpleSingletonElectionPolicy(), new NamePreference("node1/singleton"), new NamePreference("node2/singleton")));
57  
58          singleton.build(new DelegatingServiceContainer(context.getServiceTarget(), context.getServiceRegistry()))
59                  .setInitialMode(ServiceController.Mode.ACTIVE)
60                  .install()
61          ;
62      }
63  }