jboss-eap-6 HA单例在独立配置

时间:2017-08-07 23:01:05

标签: jboss singleton jboss-eap-6

我可以在我的独立群集中部署我的耳朵和战争。我的2场战争是针对HA单身人士的。启动第一个独立的jboss-eap-6后不久,我开始第二个。当我的所有应用程序都已成功部署后,我打开J-Console,我注意到我的一个单独战争正在第一个jboss-eap-6上运行而第二个单独战争正在第二个jboss-eap-6上运行。同样在Jconsole中,只有1个jboss-eap-6报告为主要报告。

我的问题是:在jboss-eap-6 standalone.xml中是否有某种方法我只能强制使用1个jboss-eap-6来运行单例HA战争。或者我是否必须将战争打包到耳中?

2 个答案:

答案 0 :(得分:0)

我认为standalone.xml中没有任何内容可以改变战争的行为。在任何情况下,您都应该将standalone-ha.xml用于已部署HA单例的群集。

JBoss高可用性Singleton架构在JBoss EAP 5和6之间发生了显着变化。

在JBoss EAP 5下,您只需将可部署对象放在deploy-hasingleton特殊部署文件夹中。在JBoss EAP 6下,您的类需要实现JBoss服务层,特别是org.jboss.msc.service.Serviceorg.jboss.msc.service.ServiceActivator。正是这些服务类的实现控制着HA Singleton的实例化和管理。我没有尝试将hasingleton部署为战争,我有一些疑问,因为我怀疑依赖服务类可能在Web容器中不可用。

ServiceActivator负责管理Service的生命周期。 ServiceActivator实现类需要在文件META-INF/service/org.jboss.msc.service.ServiceActivator中列出,以便JBoss在启动/部署期间激活它。

实施例: 创建服务激活器

public abstract class SingletonActivator<T extends Serializable> implements ServiceActivator {

    @Override
    public SingletonService<String> instantiateSingleton() {
        return new SingletonService<String>();
    }

    public ServiceName getServiceName() {
         return ServiceName.JBOSS.append("my", "ha", "singleton");
     }


    /**
     * Activated by the Service Activator
     *
     * @param service
     * @param serviceName
     *            - the Singleton Service Name that is registered in the JBOSS cluster
     */
    @Override
    public final void activate(ServiceActivatorContext context) {
        SingletonService<T> service = instantiateSingleton();
        SingletonService<T> singleton = new SingletonService<T>(service, getServiceName());

        /*
         * The NamePreference is a combination of the node name (-Djboss.node.name) and the name of
         * the configured cache "singleton". If there is more than 1 node, it is possible to add more than
         * one name and the election will use the first available node in that list.
         */
        // e.g. singleton.setElectionPolicy(new PreferredSingletonElectionPolicy(new SimpleSingletonElectionPolicy(), new NamePreference("node1/singleton")));
        // or singleton.setElectionPolicy(new PreferredSingletonElectionPolicy(new SimpleSingletonElectionPolicy(), new NamePreference("node1/singleton"), new
        // NamePreference("node2/singleton")));

        singleton.build(new DelegatingServiceContainer(context.getServiceTarget(), context.getServiceRegistry())).setInitialMode(ServiceController.Mode.ACTIVE).install();
    }
}

创建HA单例服务类,该类单独负责查找和调用包含业务逻辑的EJB

public class SingletonService<T> implements Service<T> {
    protected ScheduledExecutorService deployDelayThread = null;

    /**
     * The node we are running on
     */
    protected String nodeName;

    /**
     * A flag whether the service is started (or scheduled to be started)
     */
    protected final AtomicBoolean started = new AtomicBoolean(false);

    /**
     * Container life cycle call upon activation. This will construct the singleton instance in this JVM and start the Timer.
     */
    @Override
    public final void start(StartContext context) throws StartException {
        this.nodeName = System.getProperty("jboss.node.name");
        logger.info("Starting service '" + this.getClass().getName() + "' on node " + nodeName);
        if (!started.compareAndSet(false, true)) {
            throw new StartException("The service " + this.getClass().getName() + " is still started!");
        }
        // MSC does not allow this thread to be blocked so we let the service know that the start is asynchronous and the result will be advised later.
        // We delay the actual deployment of the Singleton for a few seconds to allow time for a HASingleton Election to be held and won by one of the instances.
        // If the winner is not this instance (prior to deployemnt) then stop(Context) is invoked which sets started to false and the deployment does not occur.
        // context.asynchronous();
        deployDelayThread.schedule(new StartSingletonAsync(context), 10, TimeUnit.SECONDS);
        context.complete();
    }

    /** Introduces a 5s delay in starting the Singleton bean giving time for the the ha singleton election to be held and won */
    private class StartSingletonAsync implements Runnable {

        private StartSingletonAsync(StartContext context) {
        }

        @Override
        public void run() {
            try {
                startSingletonBean();
            } catch (StartException e) {
                logger.info("Start Exception", e);
            }
            // be nice to the garbage collector, we don't need this any more
            deployDelayThread.shutdown();
            deployDelayThread = null;
        }
    }

    private void startSingletonBean() throws StartException {
        try {
            if (!started.get()) {
                throw new StartException("Aborted due to service stopping");
            }
            // Start your EJB
            InitialContext ic = new InitialContext();
            bean = ic.lookup(getJndiName());
            bean.startHaSingleton();
            logger.info("*** Master Only: HASingleton service " + getJndiName() + " started on master:" + nodeName);
            if (!bean.isRunning()) {
                logger.error("ERROR Bean should be running");
            }
        } catch (NamingException e) {
            throwStartException(e);
        }
    }

    private void throwStartException(Exception e) throws StartException {
        String message = "Could not initialize HASingleton" + getJndiName() + " on " + nodeName;
        logger.error(message, e);
        throw new StartException(message, e);
    }

    /**
     * Container life cycle call when activated
     */
    @Override
    public final void stop(StopContext context) {
        if (deployDelayThread != null) {
            deployDelayThread.shutdownNow();
        }
        if (!started.compareAndSet(true, false) || bean == null) {
            logger.warn("The service '" + this.getClass().getName() + "' is not active!");
        } else {
            try {
                InitialContext ic = new InitialContext();
                bean = (JmxMBean) ic.lookup(getJndiName());
                bean.stopHaSingleton();
                logger.info("*** Master Only: HASingleton service " + getJndiName() + " stopped on master:" + nodeName);
            } catch (EJBException e) {
                // Note: all these exceptions are already logged by JBoss 
            } catch (NamingException e) {
                logger.error("Could not stop HASingleton service " + getJndiName() + " on " + nodeName, e);
            }
            logger.info("MASTER ONLY HASingleton service '" + this.getClass().getName() + "' Stopped on node " + nodeName);
        }
    }

    private String getJndiName() {
        return "java.global/path/to/your/singleton/ejb";
    }
}

最后在META-INF/servcie/org.jboss.msc.service.ServiceActivator

中列出您的Activator类
com.mycompany.singletons.SingletonActivator

您可能还需要在jar中的manifest META-INF/MANIFEST.MF文件中添加依赖项,如下所示:Dependencies: org.jboss.msc, org.jboss.as.clustering.singleton, org.jboss.as.server

Redhat在https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html/Development_Guide/Implement_an_HA_Singleton.html提供了更广泛的实施指南。您可能需要创建一个Redhat帐户才能访问它。 JBoss发行版中还有一个快速入门的例子。

答案 1 :(得分:0)

因此,在进一步评估之后,2个单身人士最终会在几分钟后合并在一起,从而创造出1个单身人士。