<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,這樣會(huì)覆蓋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 閱讀(622) 評(píng)論(0)  編輯  收藏 所屬分類: JMS

    主站蜘蛛池模板: 美女露隐私全部免费直播| 亚洲人成影院在线观看| 精品国产呦系列在线观看免费| 国产精品成人免费一区二区 | 日韩视频在线免费观看| 亚洲国产综合自在线另类| 亚洲电影在线免费观看| 亚洲最大的视频网站| 免费三级毛片电影片| 亚洲AV无码国产精品色| 成人免费视频网址| 亚洲.国产.欧美一区二区三区| a级在线免费观看| 亚洲大尺度无码无码专区| 99视频免费播放| 久久夜色精品国产亚洲av | 91精品成人免费国产| 亚洲国产精品不卡在线电影| 97国产在线公开免费观看| 亚洲最大的黄色网| 国产成人免费片在线观看 | 久久精品国产亚洲网站| 一级毛片不卡片免费观看| 亚洲色偷偷偷网站色偷一区| 免费无码又黄又爽又刺激| 羞羞视频免费观看| 亚洲日产无码中文字幕| 男人扒开添女人下部免费视频| 日韩一卡2卡3卡4卡新区亚洲| 国产亚洲视频在线| 国产AV无码专区亚洲Av| 特级做A爰片毛片免费69| 羞羞的视频在线免费观看| 亚洲精品自在在线观看| aa午夜免费剧场| 亚洲乱亚洲乱少妇无码| 免费视频成人片在线观看| 亚洲s码欧洲m码吹潮| 国产∨亚洲V天堂无码久久久| 97国产免费全部免费观看| 窝窝影视午夜看片免费|