|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.jms.activemq;
|
18 | 18 |
|
19 |
| -import java.lang.reflect.Method; |
20 |
| - |
21 | 19 | import javax.jms.ConnectionFactory;
|
22 | 20 |
|
23 | 21 | import org.apache.activemq.ActiveMQConnectionFactory;
|
24 | 22 | import org.apache.activemq.pool.PooledConnectionFactory;
|
25 | 23 |
|
| 24 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
26 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
27 | 27 | import org.springframework.context.annotation.Bean;
|
28 | 28 | import org.springframework.context.annotation.Configuration;
|
29 |
| -import org.springframework.util.Assert; |
30 |
| -import org.springframework.util.ReflectionUtils; |
31 | 29 |
|
32 | 30 | /**
|
33 | 31 | * Configuration for ActiveMQ {@link ConnectionFactory}.
|
|
43 | 41 | class ActiveMQConnectionFactoryConfiguration {
|
44 | 42 |
|
45 | 43 | @Bean
|
46 |
| - public ConnectionFactory jmsConnectionFactory(ActiveMQProperties properties) { |
47 |
| - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory( |
48 |
| - properties).createConnectionFactory(ActiveMQConnectionFactory.class); |
49 |
| - if (properties.isPooled()) { |
50 |
| - PooledConnectionFactory pool = new PooledConnectionFactory(); |
51 |
| - Method setConnectionFactory = findConnectionFactorySetter(); |
52 |
| - Assert.state(setConnectionFactory != null, |
53 |
| - "No supported " + "setConnectionFactory method was found"); |
54 |
| - ReflectionUtils.invokeMethod(setConnectionFactory, pool, connectionFactory); |
55 |
| - return pool; |
56 |
| - } |
57 |
| - return connectionFactory; |
| 44 | + @ConditionalOnProperty(prefix = "spring.activemq", name = "pooled", havingValue = "false", matchIfMissing = true) |
| 45 | + public ActiveMQConnectionFactory jmsConnectionFactory(ActiveMQProperties properties) { |
| 46 | + return new ActiveMQConnectionFactoryFactory(properties) |
| 47 | + .createConnectionFactory(ActiveMQConnectionFactory.class); |
58 | 48 | }
|
59 | 49 |
|
60 |
| - private Method findConnectionFactorySetter() { |
61 |
| - Method setter = findConnectionFactorySetter(ConnectionFactory.class); |
62 |
| - if (setter == null) { |
63 |
| - setter = findConnectionFactorySetter(Object.class); |
| 50 | + @ConditionalOnClass(PooledConnectionFactory.class) |
| 51 | + static class PooledConnectionFactoryConfiguration { |
| 52 | + |
| 53 | + @Bean(destroyMethod = "stop") |
| 54 | + @ConditionalOnProperty(prefix = "spring.activemq", name = "pooled", havingValue = "true", matchIfMissing = false) |
| 55 | + public PooledConnectionFactory pooledJmsConnectionFactory( |
| 56 | + ActiveMQProperties properties) { |
| 57 | + return new PooledConnectionFactory( |
| 58 | + new ActiveMQConnectionFactoryFactory(properties) |
| 59 | + .createConnectionFactory(ActiveMQConnectionFactory.class)); |
64 | 60 | }
|
65 |
| - return setter; |
66 |
| - } |
67 | 61 |
|
68 |
| - private Method findConnectionFactorySetter(Class<?> param) { |
69 |
| - return ReflectionUtils.findMethod(PooledConnectionFactory.class, |
70 |
| - "setConnectionFactory", param); |
71 | 62 | }
|
72 | 63 |
|
73 | 64 | }
|
0 commit comments