Announcement Announcement Module
Collapse
No announcement yet.
Connection Shutdown listener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connection Shutdown listener

    In the previous API I used I had the ability to register a shutdown listener if the underlying connection to the amqp server was lost for whatever reason. This helps us monitor when the amqp server goes down and we have to then restart the app since the api didn't reconnect to the server once the connection was lost. So my questions are two fold,
    - Does this api reconnect to the amqp server if the underlying connection is lost.
    - If not is there a way to register a listener on the connection used by the listener objects.
    I've tried this
    Code:
    rabbitTemplate().getConnectionFactory().createConnection()
    				.addShutdownListener()
    but im not sure if this is what i want since it looks like it creates a new connection and registers a listener to that and not to the connection used by the listener container.

    Thanks
    Irfan

  • #2
    Hi!

    I'm going to suppose that you are using Spring XML configuration to setup your connection factory. Let's say you have something like this:

    Code:
    <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
      <constructor-arg value="localhost"/>
      <property name="port" value="5672"/>
      <property name="username" value="I'am"/>
      <property name="password" value="beautiful"/>
    </bean>
    Now, the shutdown listeners are not really worked out yet in this early version of Spring AMQP. But if you inspect the class SingleconnectionFactory, you will see some tips on how to add the listeners. You should see this method:

    Code:
    protected void prepareConnection(Connection con) throws IOException {
      //TODO configure ShutdownListener, investigate reconnection exceptions
    }
    If you override that class and implement that method to add the listener to the "con" object, you should be ready to go.

    Code:
    public class RabbitConnectionFactory extends SingleConnectionFactory {
        public RabbitConnectionFactory() {
        }
    
        public RabbitConnectionFactory(final String hostname) {
            super(hostname);
        }
    
        @Override
        protected void prepareConnection(Connection con) throws IOException {
            con.addShutdownListener(new ShutdownListener(){
                @Override
                public void shutdownCompleted(final ShutdownSignalException cause) {
                    //TODO: Implement a way to reconnect? Perhaps with a timer that
                    //periodically tries to reconnect or by reconnecting next time a RabbitTemplate is used...
                    System.out.printf("Aye! %s \n", cause.getMessage());
                }
            });
        }
    }
    Then, instead of declaring the original SingleConnectionFactory in the Spring XML config, use your implementation.

    Code:
    <bean id="connectionFactory" class="my.package.RabbitConnectionFactory">
      <constructor-arg value="localhost"/>
      <property name="port" value="5672"/>
      <property name="username" value="I'am"/>
      <property name="password" value="beautiful"/>
    </bean>

    Hope it helps.

    Comment


    • #3
      Hi All,

      Have the same problem.

      monzonj

      In accordance with latest API of spring-amqp-1.0.0.RC1 there is no method prepareConnection:
      http://static.springsource.org/sprin...onFactory.html

      Your example is actual for spring-amqp-1.0.0.M1(2) but not for spring-amqp-1.0.0.RC1.

      Thanks.

      Comment


      • #4
        Look at ConnectionListener (a Spring AMQP callback).

        Comment

        Working...
        X