Results 1 to 7 of 7

Thread: SingleConnectionFactory does not support custom username and password

  1. #1
    Join Date
    May 2012
    Location
    NY
    Posts
    23

    Default SingleConnectionFactory does not support custom username and password

    For the following config:

    Code:
    <bean id="subUserCredentialsConnectionFactory" 
    		class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
    		<property name="targetConnectionFactory"  ref="connFactory"/>
    		<property name="username" value="abc" />
    		<property name="password" value="xyz" />
    	</bean>
    I get the error "SingleConnectionFactory does not support custom username and password" and the connection never gets created if I use a CachingConnectionFactory as the targetConnectionFactory. Works fine if I use a vanilla ConnectionFactory. I want to have both, credentials as well as caching of connections/sessions/consumers, How do I do this?

  2. #2
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,142

    Default

    Please don't post the same question in two threads.

    Probably the easiest way to do this is to create a subclass of (Single|Cached)ConnectionFactory and override the doCreateConnection() method. The default implementation is in SingleConnectionFactory.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  3. #3
    Join Date
    May 2012
    Location
    NY
    Posts
    23

    Default

    Thanks Gary, deleted the question from the previous post.

  4. #4
    Join Date
    May 2012
    Location
    NY
    Posts
    23

    Default

    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?

  5. #5
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,142

    Default

    Why can you not simply override doCreateConnection() as I suggested...

    Code:
        private String user;
        private String password;
    
    //  setters for the above
    
        protected Connection doCreateConnection() throws JMSException {
            return getTargetConnectionFactory().createConnection(user,password);
        }
    Store the user and password as fields; and let the standard code call your overridden doCreateConnection(); you shouldn't need access to those private fields.
    Last edited by Gary Russell; May 14th, 2012 at 02:44 PM.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  6. #6
    Join Date
    Mar 2010
    Location
    Gtr Philadelphia, PA
    Posts
    2,142

    Default

    I copied your code and forgot to remove the parameters - see the example in my post #5 above now.
    Gary P. Russell
    Spring Integration Team
    SpringSource, a division of VMware

  7. #7
    Join Date
    May 2012
    Location
    NY
    Posts
    23

    Default

    That works perfectly thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •