@ -16,10 +16,15 @@
package org.springframework.boot.autoconfigure.jms ;
import javax.jms.ConnectionFactory ;
import org.apache.activemq.ActiveMQConnectionFactory ;
import org.apache.activemq.pool.PooledConnectionFactory ;
import org.springframework.boot.bind.RelaxedPropertyResolver ;
import org.springframework.boot.context.properties.ConfigurationProperties ;
import org.springframework.core.env.Environment ;
import org.springframework.core.env.PropertyResolver ;
import org.springframework.util.StringUtils ;
/ * *
* Configuration properties for ActiveMQ
@ -34,35 +39,18 @@ public class ActiveMQProperties {
public static final String DEFAULT_NETWORK_BROKER_URL = "tcp://localhost:61616" ;
private String brokerUrl = null ;
private String brokerUrl ;
private boolean inMemory = true ;
private boolean pooled = false ;
private boolean pooled ;
private String user ;
private String password ;
/ * *
* Determine the broker url to use for the specified { @link Environment } .
* < p > If no broker url is specified through configuration , a default
* broker is provided , that is { @value # DEFAULT_EMBEDDED_BROKER_URL } if
* the { @code inMemory } flag is { @code null } or { @code true } ,
* { @value # DEFAULT_NETWORK_BROKER_URL } otherwise .
*
* @param environment the environment to extract configuration from
* @return the broker url to use
* /
public static String determineBrokerUrl ( Environment environment ) {
PropertyResolver resolver = new RelaxedPropertyResolver ( environment , "spring.activemq." ) ;
String brokerUrl = resolver . getProperty ( "brokerUrl" ) ;
Boolean inMemory = resolver . getProperty ( "inMemory" , Boolean . class ) ;
return determineBrokerUrl ( brokerUrl , inMemory ) ;
}
public String getBrokerUrl ( ) {
return determineBrokerUrl ( this . brokerUrl , this . inMemory ) ;
return this . brokerUrl ;
}
public void setBrokerUrl ( String brokerUrl ) {
@ -70,8 +58,8 @@ public class ActiveMQProperties {
}
/ * *
* Specify if the default broker url should be in memory . Ignored
* if an explicit broker has been specified .
* Specify if the default broker url should be in memory . Ignored if an explicit
* broker has been specified .
* /
public boolean isInMemory ( ) {
return this . inMemory ;
@ -105,22 +93,52 @@ public class ActiveMQProperties {
this . password = password ;
}
/ * *
* Return a new { @link ConnectionFactory } from these properties .
* /
public ConnectionFactory createConnectionFactory ( ) {
ConnectionFactory connectionFactory = createActiveMQConnectionFactory ( ) ;
if ( isPooled ( ) ) {
PooledConnectionFactory pool = new PooledConnectionFactory ( ) ;
pool . setConnectionFactory ( connectionFactory ) ;
return pool ;
}
return connectionFactory ;
}
private ConnectionFactory createActiveMQConnectionFactory ( ) {
String brokerUrl = determineBrokerUrl ( ) ;
if ( StringUtils . hasLength ( this . user ) & & StringUtils . hasLength ( this . password ) ) {
return new ActiveMQConnectionFactory ( this . user , this . password , brokerUrl ) ;
}
return new ActiveMQConnectionFactory ( brokerUrl ) ;
}
String determineBrokerUrl ( ) {
return determineBrokerUrl ( this . brokerUrl , this . inMemory ) ;
}
/ * *
* @see # determineBrokerUrl ( Environment )
* Determine the broker url to use for the specified { @link Environment } . If no broker
* url is specified through configuration , a default broker is provided , that is
* { @value # DEFAULT_EMBEDDED_BROKER_URL } if the { @code inMemory } flag is { @code null }
* or { @code true } , { @value # DEFAULT_NETWORK_BROKER_URL } otherwise .
* @param environment the environment to extract configuration from
* @return the broker url to use
* /
private static String determineBrokerUrl ( String brokerUrl , Boolean inMemory ) {
boolean embedded = ( inMemory ! = null ) ? inMemory : true ;
public static String determineBrokerUrl ( Environment environment ) {
PropertyResolver resolver = new RelaxedPropertyResolver ( environment ,
"spring.activemq." ) ;
String brokerUrl = resolver . getProperty ( "brokerUrl" ) ;
Boolean inMemory = resolver . getProperty ( "inMemory" , Boolean . class ) ;
return determineBrokerUrl ( brokerUrl , inMemory ) ;
}
private static String determineBrokerUrl ( String brokerUrl , Boolean inMemory ) {
if ( brokerUrl ! = null ) {
return brokerUrl ;
}
else if ( embedded ) {
return DEFAULT_EMBEDDED_BROKER_URL ;
}
else {
return DEFAULT_NETWORK_BROKER_URL ;
}
boolean embedded = inMemory = = null | | inMemory ;
return ( embedded ? DEFAULT_EMBEDDED_BROKER_URL : DEFAULT_NETWORK_BROKER_URL ) ;
}
}