Hi Gary, the straightforward way to do this would be to write the following class
Code:
public class CustomConnectionFactory extends CachingConnectionFactory{
public CustomConnectionFactory(ConnectionFactory factory){
super(factory);
}
/**
* Initialize the single Connection.
* @throws javax.jms.JMSException if thrown by JMS API methods
*/
protected void init(String user, String password) throws JMSException {
if (getTargetConnectionFactory() == null) {
throw new IllegalStateException("targetConnectionFactory is required for lazily initializing a connection");
}
Connection target = doCreateConnection();
if (logger.isDebugEnabled()) {
logger.debug("Created single connection: " + target);
}
this.target = target;
this.connection = getCloseSuppressingConnectionProxy(target);
}
public Connection createConnection(String user, String password) throws JMSException {
synchronized (this) {
if (getTargetConnectionFactory()== null) {
init(user, password);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Returning single connection: " + this.connection);
}
return this.connection;
}
/**
* Create a JMS Connection via this template's ConnectionFactory.
* <p>This implementation uses JMS 1.1 API.
* @return the new JMS Connection
* @throws javax.jms.JMSException if thrown by JMS API methods
*/
protected Connection doCreateConnection(String user, String password) throws JMSException {
return getTargetConnectionFactory().createConnection(user,password);
}
}
Unfortunately both target and connection are private to SingleConnectionFactory and are not visible in my custom class. So I would have to duplicate the SingleConnectionFactory class and add the above methods to it
Do you know
1) Why this override createConnection(String,String) was excluded from the SingleConnectionFactory, it seems like a common use-case
2) Any better way to allow passing credentials while creating the connection?