Skip to content

Commit

Permalink
Remove use of reflection in Artemis connection factory creation
Browse files Browse the repository at this point in the history
Fixes gh-42414
  • Loading branch information
wilkinsona committed Sep 23, 2024
1 parent 0781e71 commit 2aea7ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,7 +56,7 @@ ActiveMQConnectionFactory jmsConnectionFactory(ArtemisProperties properties, Lis
private static ActiveMQConnectionFactory createJmsConnectionFactory(ArtemisProperties properties,
ListableBeanFactory beanFactory) {
return new ArtemisConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(ActiveMQConnectionFactory.class);
.createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new);
}

@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -89,7 +89,7 @@ static class PooledConnectionFactoryConfiguration {
@Bean(destroyMethod = "stop")
JmsPoolConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties) {
ActiveMQConnectionFactory connectionFactory = new ArtemisConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(ActiveMQConnectionFactory.class);
.createConnectionFactory(ActiveMQConnectionFactory::new, ActiveMQConnectionFactory::new);
return new JmsPoolConnectionFactoryFactory(properties.getPool())
.createPooledConnectionFactory(connectionFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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;
Expand Down Expand Up @@ -56,10 +56,11 @@ class ArtemisConnectionFactoryFactory {
this.properties = properties;
}

<T extends ActiveMQConnectionFactory> T createConnectionFactory(Class<T> factoryClass) {
<T extends ActiveMQConnectionFactory> T createConnectionFactory(Function<String, T> nativeFactoryCreator,
Function<ServerLocator, T> embeddedFactoryCreator) {
try {
startEmbeddedJms();
return doCreateConnectionFactory(factoryClass);
return doCreateConnectionFactory(nativeFactoryCreator, embeddedFactoryCreator);
}
catch (Exception ex) {
throw new IllegalStateException("Unable to create ActiveMQConnectionFactory", ex);
Expand All @@ -79,15 +80,16 @@ private void startEmbeddedJms() {
}
}

private <T extends ActiveMQConnectionFactory> T doCreateConnectionFactory(Class<T> factoryClass) throws Exception {
private <T extends ActiveMQConnectionFactory> T doCreateConnectionFactory(Function<String, T> nativeFactoryCreator,
Function<ServerLocator, T> embeddedFactoryCreator) throws Exception {
ArtemisMode mode = this.properties.getMode();
if (mode == null) {
mode = deduceMode();
}
if (mode == ArtemisMode.EMBEDDED) {
return createEmbeddedConnectionFactory(factoryClass);
return createEmbeddedConnectionFactory(embeddedFactoryCreator);
}
return createNativeConnectionFactory(factoryClass);
return createNativeConnectionFactory(nativeFactoryCreator);
}

/**
Expand All @@ -110,23 +112,22 @@ private boolean isEmbeddedJmsClassPresent() {
return false;
}

private <T extends ActiveMQConnectionFactory> T createEmbeddedConnectionFactory(Class<T> factoryClass)
throws Exception {
private <T extends ActiveMQConnectionFactory> T createEmbeddedConnectionFactory(
Function<ServerLocator, T> 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 "
+ "Artemis connection, ensure that artemis-jms-server.jar is in the classpath", ex);
}
}

private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Class<T> factoryClass)
throws Exception {
T connectionFactory = newNativeConnectionFactory(factoryClass);
private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Function<String, T> factoryCreator) {
T connectionFactory = newNativeConnectionFactory(factoryCreator);
String user = this.properties.getUser();
if (StringUtils.hasText(user)) {
connectionFactory.setUser(user);
Expand All @@ -135,12 +136,10 @@ private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Cl
return connectionFactory;
}

private <T extends ActiveMQConnectionFactory> T newNativeConnectionFactory(Class<T> factoryClass) throws Exception {
private <T extends ActiveMQConnectionFactory> T newNativeConnectionFactory(Function<String, T> factoryCreator) {
String brokerUrl = StringUtils.hasText(this.properties.getBrokerUrl()) ? this.properties.getBrokerUrl()
: DEFAULT_BROKER_URL;
Constructor<T> constructor = factoryClass.getConstructor(String.class);
return constructor.newInstance(brokerUrl);

return factoryCreator.apply(brokerUrl);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,14 +46,14 @@ class ArtemisXAConnectionFactoryConfiguration {
ConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties,
XAConnectionFactoryWrapper wrapper) throws Exception {
return wrapper.wrapConnectionFactory(new ArtemisConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(ActiveMQXAConnectionFactory.class));
.createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new));
}

@Bean
ActiveMQXAConnectionFactory nonXaJmsConnectionFactory(ListableBeanFactory beanFactory,
ArtemisProperties properties) {
return new ArtemisConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(ActiveMQXAConnectionFactory.class);
.createConnectionFactory(ActiveMQXAConnectionFactory::new, ActiveMQXAConnectionFactory::new);
}

}

0 comments on commit 2aea7ca

Please sign in to comment.