Announcement Announcement Module
Collapse
No announcement yet.
SingleConnectionFactory does not support custom username and password Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Thanks Gary, deleted the question from the previous post.

      Comment


      • #4
        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?

        Comment


        • #5
          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, 03:44 PM.

          Comment


          • #6
            I copied your code and forgot to remove the parameters - see the example in my post #5 above now.

            Comment


            • #7
              That works perfectly thanks!

              Comment

              Working...
              X