diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java index 5f4f879431d..ab4e1f6939c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java @@ -57,7 +57,7 @@ class ArtemisConnectionFactoryConfiguration { private static ActiveMQConnectionFactory createJmsConnectionFactory(ArtemisProperties properties, ArtemisConnectionDetails connectionDetails, ListableBeanFactory beanFactory) { return new ArtemisConnectionFactoryFactory(beanFactory, properties, connectionDetails) - .createConnectionFactory(ActiveMQConnectionFactory.class); + .createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new); } @Configuration(proxyBeanMethods = false) @@ -93,7 +93,7 @@ class ArtemisConnectionFactoryConfiguration { ArtemisConnectionDetails connectionDetails) { ActiveMQConnectionFactory connectionFactory = new ArtemisConnectionFactoryFactory(beanFactory, properties, connectionDetails) - .createConnectionFactory(ActiveMQConnectionFactory.class); + .createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new); return new JmsPoolConnectionFactoryFactory(properties.getPool()) .createPooledConnectionFactory(connectionFactory); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java index 70e74e4b3e4..957c17cc4b8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java @@ -16,7 +16,7 @@ package org.springframework.boot.autoconfigure.jms.artemis; -import java.lang.reflect.Constructor; +import java.util.function.Function; import org.apache.activemq.artemis.api.core.TransportConfiguration; import org.apache.activemq.artemis.api.core.client.ActiveMQClient; @@ -61,10 +61,11 @@ class ArtemisConnectionFactoryFactory { this.connectionDetails = connectionDetails; } - T createConnectionFactory(Class factoryClass) { + T createConnectionFactory(Function nativeFactoryCreator, + Function embeddedFactoryCreator) { try { startEmbeddedJms(); - return doCreateConnectionFactory(factoryClass); + return doCreateConnectionFactory(nativeFactoryCreator, embeddedFactoryCreator); } catch (Exception ex) { throw new IllegalStateException("Unable to create ActiveMQConnectionFactory", ex); @@ -84,15 +85,16 @@ class ArtemisConnectionFactoryFactory { } } - private T doCreateConnectionFactory(Class factoryClass) throws Exception { + private T doCreateConnectionFactory(Function nativeFactoryCreator, + Function embeddedFactoryCreator) throws Exception { ArtemisMode mode = this.connectionDetails.getMode(); if (mode == null) { mode = deduceMode(); } if (mode == ArtemisMode.EMBEDDED) { - return createEmbeddedConnectionFactory(factoryClass); + return createEmbeddedConnectionFactory(embeddedFactoryCreator); } - return createNativeConnectionFactory(factoryClass); + return createNativeConnectionFactory(nativeFactoryCreator); } /** @@ -115,13 +117,13 @@ class ArtemisConnectionFactoryFactory { return false; } - private T createEmbeddedConnectionFactory(Class factoryClass) - throws Exception { + private T createEmbeddedConnectionFactory( + Function factoryCreator) throws Exception { try { TransportConfiguration transportConfiguration = new TransportConfiguration( InVMConnectorFactory.class.getName(), this.properties.getEmbedded().generateTransportParameters()); - ServerLocator serviceLocator = ActiveMQClient.createServerLocatorWithoutHA(transportConfiguration); - return factoryClass.getConstructor(ServerLocator.class).newInstance(serviceLocator); + ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(transportConfiguration); + return factoryCreator.apply(serverLocator); } catch (NoClassDefFoundError ex) { throw new IllegalStateException("Unable to create InVM " @@ -129,9 +131,8 @@ class ArtemisConnectionFactoryFactory { } } - private T createNativeConnectionFactory(Class factoryClass) - throws Exception { - T connectionFactory = newNativeConnectionFactory(factoryClass); + private T createNativeConnectionFactory(Function factoryCreator) { + T connectionFactory = newNativeConnectionFactory(factoryCreator); String user = this.connectionDetails.getUser(); if (StringUtils.hasText(user)) { connectionFactory.setUser(user); @@ -140,12 +141,10 @@ class ArtemisConnectionFactoryFactory { return connectionFactory; } - private T newNativeConnectionFactory(Class factoryClass) throws Exception { + private T newNativeConnectionFactory(Function factoryCreator) { String brokerUrl = StringUtils.hasText(this.connectionDetails.getBrokerUrl()) ? this.connectionDetails.getBrokerUrl() : DEFAULT_BROKER_URL; - Constructor constructor = factoryClass.getConstructor(String.class); - return constructor.newInstance(brokerUrl); - + return factoryCreator.apply(brokerUrl); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java index 89e7823ca22..0d71430d595 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisXAConnectionFactoryConfiguration.java @@ -47,14 +47,14 @@ class ArtemisXAConnectionFactoryConfiguration { ArtemisConnectionDetails connectionDetails, XAConnectionFactoryWrapper wrapper) throws Exception { return wrapper .wrapConnectionFactory(new ArtemisConnectionFactoryFactory(beanFactory, properties, connectionDetails) - .createConnectionFactory(ActiveMQXAConnectionFactory.class)); + .createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new)); } @Bean ActiveMQXAConnectionFactory nonXaJmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties, ArtemisConnectionDetails connectionDetails) { return new ArtemisConnectionFactoryFactory(beanFactory, properties, connectionDetails) - .createConnectionFactory(ActiveMQXAConnectionFactory.class); + .createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new); } }