<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    在SPRING BOOT中使用多JMS CONNECTION

    使用自定義CONNECTION FACTORY,這樣會覆蓋SPRING 的AUTO CONFIGURATION。

    ActiveMQConnectionFactoryFactory.java
    import java.lang.reflect.InvocationTargetException;
    import java.util.Collections;
    import java.util.List;

    import org.apache.activemq.ActiveMQConnectionFactory;
    import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionFactoryCustomizer;
    import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
    import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties.Packages;
    import org.springframework.util.Assert;
    import org.springframework.util.StringUtils;


    /**
     * Factory to create a {
    @link ActiveMQConnectionFactory} instance from properties defined
     * in {
    @link SecondaryActiveMQProperties}.
     *
     * 
    @author Phillip Webb
     * 
    @author Venil Noronha
     
    */
    class ActiveMQConnectionFactoryFactory {

        private static final String DEFAULT_EMBEDDED_BROKER_URL = "vm://localhost?broker.persistent=false";

        private static final String DEFAULT_NETWORK_BROKER_URL = "tcp://localhost:61616";

        private final ActiveMQProperties properties;

        private final List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers;

        ActiveMQConnectionFactoryFactory(ActiveMQProperties properties,
                List<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
            Assert.notNull(properties, "Properties must not be null");
            this.properties = properties;
            this.factoryCustomizers = (factoryCustomizers != null) ? factoryCustomizers : Collections.emptyList();
        }

        public <T extends ActiveMQConnectionFactory> T createConnectionFactory(Class<T> factoryClass) {
            try {
                return doCreateConnectionFactory(factoryClass);
            }
            catch (Exception ex) {
                throw new IllegalStateException("Unable to create " + "ActiveMQConnectionFactory", ex);
            }
        }

        private <T extends ActiveMQConnectionFactory> T doCreateConnectionFactory(Class<T> factoryClass) throws Exception {
            T factory = createConnectionFactoryInstance(factoryClass);
            if (this.properties.getCloseTimeout() != null) {
                factory.setCloseTimeout((intthis.properties.getCloseTimeout().toMillis());
            }
            factory.setNonBlockingRedelivery(this.properties.isNonBlockingRedelivery());
            if (this.properties.getSendTimeout() != null) {
                factory.setSendTimeout((intthis.properties.getSendTimeout().toMillis());
            }
            Packages packages = this.properties.getPackages();
            if (packages.getTrustAll() != null) {
                factory.setTrustAllPackages(packages.getTrustAll());
            }
            if (!packages.getTrusted().isEmpty()) {
                factory.setTrustedPackages(packages.getTrusted());
            }
            customize(factory);
            return factory;
        }

        private <T extends ActiveMQConnectionFactory> T createConnectionFactoryInstance(Class<T> factoryClass)
                throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
            String brokerUrl = determineBrokerUrl();
            String user = this.properties.getUser();
            String password = this.properties.getPassword();
            if (StringUtils.hasLength(user) && StringUtils.hasLength(password)) {
                return factoryClass.getConstructor(String.class, String.class, String.class).newInstance(user, password,
                        brokerUrl);
            }
            return factoryClass.getConstructor(String.class).newInstance(brokerUrl);
        }

        private void customize(ActiveMQConnectionFactory connectionFactory) {
            for (ActiveMQConnectionFactoryCustomizer factoryCustomizer : this.factoryCustomizers) {
                factoryCustomizer.customize(connectionFactory);
            }
        }

        String determineBrokerUrl() {
            if (this.properties.getBrokerUrl() != null) {
                return this.properties.getBrokerUrl();
            }
            if (this.properties.isInMemory()) {
                return DEFAULT_EMBEDDED_BROKER_URL;
            }
            return DEFAULT_NETWORK_BROKER_URL;
        }
    }

    TwinJmsConnectionFactoryConfiguration.java
    import java.util.stream.Collectors;

    import org.apache.activemq.ActiveMQConnectionFactory;
    import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;
    import org.springframework.beans.factory.ObjectProvider;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryFactory;
    import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionFactoryCustomizer;
    import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.context.annotation.Profile;


    @Configuration
    @Profile({"local"})
    public class TwinJmsConnectionFactoryConfiguration {

        @Bean
        @ConfigurationProperties(prefix = "spring.activemq.primary")
        public ActiveMQProperties primaryActiveMQProperties() {
            return new ActiveMQProperties();
        }
        
        @Bean(destroyMethod = "stop")
        @Primary
        @ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true")
        public JmsPoolConnectionFactory connectionFactory(ActiveMQProperties primaryActiveMQProperties,
                ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(primaryActiveMQProperties,
                    factoryCustomizers.orderedStream().collect(Collectors.toList()))
                    .createConnectionFactory(ActiveMQConnectionFactory.class);
            return new JmsPoolConnectionFactoryFactory(primaryActiveMQProperties.getPool())
                    .createPooledConnectionFactory(connectionFactory);
        }
        
        ////////////////////////////////////////////////////////////////////////////////
        @Bean
        @ConfigurationProperties(prefix = "spring.activemq.sescond")
        public ActiveMQProperties sescondActiveMQProperties() {
            return new ActiveMQProperties();
        }
        
        @Bean(destroyMethod = "stop")
        @ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true")
        public JmsPoolConnectionFactory sescondPooledJmsConnectionFactory(ActiveMQProperties sescondActiveMQProperties,
                ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(sescondActiveMQProperties,
                    factoryCustomizers.orderedStream().collect(Collectors.toList()))
                    .createConnectionFactory(ActiveMQConnectionFactory.class);
            return new JmsPoolConnectionFactoryFactory(sescondActiveMQProperties.getPool())
                    .createPooledConnectionFactory(connectionFactory);
        }
        
    }


    posted on 2020-03-19 09:45 paulwong 閱讀(623) 評論(0)  編輯  收藏 所屬分類: JMS

    主站蜘蛛池模板: 免费的一级黄色片| 日韩在线不卡免费视频一区| 国产v精品成人免费视频400条| 久久国产精品亚洲综合| 日本一区午夜艳熟免费| 亚洲成av人影院| 久久久99精品免费观看| 亚洲黄色高清视频| 97视频免费在线| 亚洲中文字幕无码爆乳| 国产jizzjizz免费视频| 免费人成再在线观看网站 | 亚洲免费在线视频| 亚洲视频免费一区| 免费精品国产自产拍在线观看图片| 亚洲人成电影在线观看网| 无码人妻久久一区二区三区免费丨 | 亚洲精品在线视频观看| 91九色精品国产免费| 亚洲熟女乱色一区二区三区| 国产成人aaa在线视频免费观看| 黄页网址在线免费观看| 亚洲春色在线视频| 97在线线免费观看视频在线观看| 亚洲精品无码少妇30P| 亚洲中文字幕视频国产| 中文字幕免费在线| 亚洲综合无码无在线观看| 亚洲日本中文字幕天堂网| 无码AV片在线观看免费| 亚洲AV成人影视在线观看| 亚洲精品一级无码中文字幕 | 日韩一区二区a片免费观看| 国产亚洲福利一区二区免费看| 久久久久亚洲AV成人网人人网站 | 久久99精品国产免费观看| 亚洲一卡2卡3卡4卡乱码 在线| 亚洲av区一区二区三| 久久久久免费看成人影片| 最新亚洲人成网站在线观看| 国产AV无码专区亚洲Av|