首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jboss-eap-6 HA singleton在独立配置中部署多个web归档

jboss-eap-6 HA singleton在独立配置中部署多个web归档
EN

Stack Overflow用户
提问于 2017-08-08 07:01:05
回答 2查看 418关注 0票数 0

我能够在我的独立集群中部署我的耳朵和wars。我的两场战争都是为了HA单身人士。在启动第一个独立的jboss-eap-6之后不久,我启动了第二个。当我的所有应用程序都部署成功后,我打开J-Console,我注意到我的一个单例war运行在第一个jboss-eap-6上,第二个单例war运行在第二个jboss-eap-6上。同样在Jconsole中,只有1个jboss-eap-6报告作为主要报告。

我的问题是:在jboss-eap-6 standalone.xml中有没有什么方法我只能强制1个jboss-eap-6运行单例HA wars?或者我必须把战争装进耳朵里?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-11 22:05:32

因此,经过进一步的评估,两个单例最终将在几分钟后合并在一起,从而创建一个预期的单例。

票数 0
EN

Stack Overflow用户

发布于 2017-08-21 15:39:42

我不认为standalone.xml中有任何东西会改变战争的行为。在任何情况下,您都应该为部署了HA单例的集群使用standalone-ha.xml

在JBoss EAP5和6之间,JBoss高可用性单例架构发生了重大变化。

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

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

示例:创建服务激活器

代码语言:javascript
复制
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

代码语言:javascript
复制
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中列出您的激活器类

代码语言:javascript
复制
com.mycompany.singletons.SingletonActivator

您可能还需要将依赖项添加到jar中的清单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发行版中也有一个快速入门示例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45556880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档