16
16
17
17
package org .springframework .boot .autoconfigure .jms .artemis ;
18
18
19
- import java .lang . reflect . Constructor ;
19
+ import java .util . function . Function ;
20
20
21
21
import org .apache .activemq .artemis .api .core .TransportConfiguration ;
22
22
import org .apache .activemq .artemis .api .core .client .ActiveMQClient ;
@@ -61,10 +61,11 @@ class ArtemisConnectionFactoryFactory {
61
61
this .connectionDetails = connectionDetails ;
62
62
}
63
63
64
- <T extends ActiveMQConnectionFactory > T createConnectionFactory (Class <T > factoryClass ) {
64
+ <T extends ActiveMQConnectionFactory > T createConnectionFactory (Function <String , T > nativeFactoryCreator ,
65
+ Function <ServerLocator , T > embeddedFactoryCreator ) {
65
66
try {
66
67
startEmbeddedJms ();
67
- return doCreateConnectionFactory (factoryClass );
68
+ return doCreateConnectionFactory (nativeFactoryCreator , embeddedFactoryCreator );
68
69
}
69
70
catch (Exception ex ) {
70
71
throw new IllegalStateException ("Unable to create ActiveMQConnectionFactory" , ex );
@@ -84,15 +85,16 @@ private void startEmbeddedJms() {
84
85
}
85
86
}
86
87
87
- private <T extends ActiveMQConnectionFactory > T doCreateConnectionFactory (Class <T > factoryClass ) throws Exception {
88
+ private <T extends ActiveMQConnectionFactory > T doCreateConnectionFactory (Function <String , T > nativeFactoryCreator ,
89
+ Function <ServerLocator , T > embeddedFactoryCreator ) throws Exception {
88
90
ArtemisMode mode = this .connectionDetails .getMode ();
89
91
if (mode == null ) {
90
92
mode = deduceMode ();
91
93
}
92
94
if (mode == ArtemisMode .EMBEDDED ) {
93
- return createEmbeddedConnectionFactory (factoryClass );
95
+ return createEmbeddedConnectionFactory (embeddedFactoryCreator );
94
96
}
95
- return createNativeConnectionFactory (factoryClass );
97
+ return createNativeConnectionFactory (nativeFactoryCreator );
96
98
}
97
99
98
100
/**
@@ -115,23 +117,22 @@ private boolean isEmbeddedJmsClassPresent() {
115
117
return false ;
116
118
}
117
119
118
- private <T extends ActiveMQConnectionFactory > T createEmbeddedConnectionFactory (Class < T > factoryClass )
119
- throws Exception {
120
+ private <T extends ActiveMQConnectionFactory > T createEmbeddedConnectionFactory (
121
+ Function < ServerLocator , T > factoryCreator ) throws Exception {
120
122
try {
121
123
TransportConfiguration transportConfiguration = new TransportConfiguration (
122
124
InVMConnectorFactory .class .getName (), this .properties .getEmbedded ().generateTransportParameters ());
123
- ServerLocator serviceLocator = ActiveMQClient .createServerLocatorWithoutHA (transportConfiguration );
124
- return factoryClass . getConstructor ( ServerLocator . class ). newInstance ( serviceLocator );
125
+ ServerLocator serverLocator = ActiveMQClient .createServerLocatorWithoutHA (transportConfiguration );
126
+ return factoryCreator . apply ( serverLocator );
125
127
}
126
128
catch (NoClassDefFoundError ex ) {
127
129
throw new IllegalStateException ("Unable to create InVM "
128
130
+ "Artemis connection, ensure that artemis-jms-server.jar is in the classpath" , ex );
129
131
}
130
132
}
131
133
132
- private <T extends ActiveMQConnectionFactory > T createNativeConnectionFactory (Class <T > factoryClass )
133
- throws Exception {
134
- T connectionFactory = newNativeConnectionFactory (factoryClass );
134
+ private <T extends ActiveMQConnectionFactory > T createNativeConnectionFactory (Function <String , T > factoryCreator ) {
135
+ T connectionFactory = newNativeConnectionFactory (factoryCreator );
135
136
String user = this .connectionDetails .getUser ();
136
137
if (StringUtils .hasText (user )) {
137
138
connectionFactory .setUser (user );
@@ -140,12 +141,10 @@ private <T extends ActiveMQConnectionFactory> T createNativeConnectionFactory(Cl
140
141
return connectionFactory ;
141
142
}
142
143
143
- private <T extends ActiveMQConnectionFactory > T newNativeConnectionFactory (Class < T > factoryClass ) throws Exception {
144
+ private <T extends ActiveMQConnectionFactory > T newNativeConnectionFactory (Function < String , T > factoryCreator ) {
144
145
String brokerUrl = StringUtils .hasText (this .connectionDetails .getBrokerUrl ())
145
146
? this .connectionDetails .getBrokerUrl () : DEFAULT_BROKER_URL ;
146
- Constructor <T > constructor = factoryClass .getConstructor (String .class );
147
- return constructor .newInstance (brokerUrl );
148
-
147
+ return factoryCreator .apply (brokerUrl );
149
148
}
150
149
151
150
}
0 commit comments