From 5774e808c7cf582c64b3cf00499fa1eae57a8943 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 1 Sep 2014 14:05:39 -0700 Subject: [PATCH] Fix HornetQ ClassNotFound issues when not embedded Update HornetQConnectionFactoryConfiguration and HornetQXAConnectionFactoryConfiguration so that they no longer depend on the HornetQ EmbeddedJMS class. EmbeddedJMS beans are started (when possible) from the HornetQConnectionFactoryFactory. Fixes gh-1480 --- ...HornetQConnectionFactoryConfiguration.java | 20 ++++------------ .../HornetQConnectionFactoryFactory.java | 20 +++++++++++++++- ...rnetQXAConnectionFactoryConfiguration.java | 23 +++++-------------- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryConfiguration.java index 64e361c30d..daa2d5c4ff 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryConfiguration.java @@ -18,11 +18,8 @@ package org.springframework.boot.autoconfigure.jms.hornetq; import javax.jms.ConnectionFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.hornetq.jms.client.HornetQConnectionFactory; -import org.hornetq.jms.server.embedded.EmbeddedJMS; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -37,19 +34,10 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnMissingBean(ConnectionFactory.class) class HornetQConnectionFactoryConfiguration { - private static Log logger = LogFactory - .getLog(HornetQEmbeddedServerConfiguration.class); - - // Ensure JMS is setup before XA - @Autowired(required = false) - private EmbeddedJMS embeddedJMS; - @Bean - public ConnectionFactory jmsConnectionFactory(HornetQProperties properties) { - if (this.embeddedJMS != null && logger.isDebugEnabled()) { - logger.debug("Using embdedded HornetQ broker"); - } - return new HornetQConnectionFactoryFactory(properties) + public ConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, + HornetQProperties properties) { + return new HornetQConnectionFactoryFactory(beanFactory, properties) .createConnectionFactory(HornetQConnectionFactory.class); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryFactory.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryFactory.java index 496c73536a..ff6028b85a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryFactory.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQConnectionFactoryFactory.java @@ -27,6 +27,7 @@ import org.hornetq.core.remoting.impl.invm.InVMConnectorFactory; import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory; import org.hornetq.core.remoting.impl.netty.TransportConstants; import org.hornetq.jms.client.HornetQConnectionFactory; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -44,14 +45,20 @@ class HornetQConnectionFactoryFactory { private final HornetQProperties properties; - public HornetQConnectionFactoryFactory(HornetQProperties properties) { + private final ListableBeanFactory beanFactory; + + public HornetQConnectionFactoryFactory(ListableBeanFactory beanFactory, + HornetQProperties properties) { + Assert.notNull(beanFactory, "BeanFactory must not be null"); Assert.notNull(properties, "Properties must not be null"); + this.beanFactory = beanFactory; this.properties = properties; } public T createConnectionFactory( Class factoryClass) { try { + startEmbededJms(); return doCreateConnectionFactory(factoryClass); } catch (Exception ex) { @@ -60,6 +67,17 @@ class HornetQConnectionFactoryFactory { } } + private void startEmbededJms() { + if (ClassUtils.isPresent(EMBEDDED_JMS_CLASS, null)) { + try { + this.beanFactory.getBeansOfType(Class.forName(EMBEDDED_JMS_CLASS)); + } + catch (Exception ex) { + // Ignore + } + } + } + private T doCreateConnectionFactory( Class factoryClass) throws Exception { HornetQMode mode = this.properties.getMode(); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQXAConnectionFactoryConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQXAConnectionFactoryConfiguration.java index ae7d5b1b6a..529322253e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQXAConnectionFactoryConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/hornetq/HornetQXAConnectionFactoryConfiguration.java @@ -19,11 +19,8 @@ package org.springframework.boot.autoconfigure.jms.hornetq; import javax.jms.ConnectionFactory; import javax.transaction.TransactionManager; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.hornetq.jms.client.HornetQXAConnectionFactory; -import org.hornetq.jms.server.embedded.EmbeddedJMS; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -43,21 +40,13 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnBean(XAConnectionFactoryWrapper.class) class HornetQXAConnectionFactoryConfiguration { - private static Log logger = LogFactory - .getLog(HornetQEmbeddedServerConfiguration.class); - - // Ensure JMS is setup before XA - @Autowired(required = false) - private EmbeddedJMS embeddedJMS; - @Bean - public ConnectionFactory jmsConnectionFactory(HornetQProperties properties, - XAConnectionFactoryWrapper wrapper) throws Exception { - if (this.embeddedJMS != null && logger.isDebugEnabled()) { - logger.debug("Using embdedded HornetQ broker with XA"); - } + public ConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, + HornetQProperties properties, XAConnectionFactoryWrapper wrapper) + throws Exception { return wrapper.wrapConnectionFactory(new HornetQConnectionFactoryFactory( - properties).createConnectionFactory(HornetQXAConnectionFactory.class)); + beanFactory, properties) + .createConnectionFactory(HornetQXAConnectionFactory.class)); } }