Package net.joshdevins.rabbitmq.client.ha.it

Source Code of net.joshdevins.rabbitmq.client.ha.it.TestHaConnectionListener

package net.joshdevins.rabbitmq.client.ha.it;

import java.util.HashMap;

import net.joshdevins.rabbitmq.client.ha.AbstractHaConnectionListener;
import net.joshdevins.rabbitmq.client.ha.HaConnectionProxy;

import com.rabbitmq.client.Address;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class TestHaConnectionListener extends AbstractHaConnectionListener {

    private final ConnectionFactory connectionFactory;

    private final String host;

    TestHaConnectionListener(final ConnectionFactory connectionFactory, final String host) {

        this.connectionFactory = connectionFactory;
        this.host = host;
    }
   
    @Override
    public void onReconnection(final HaConnectionProxy connectionProxy) {

        // use a separate connection and channel to avoid race conditions with any operations that are blocked
        // waiting for the reconnection to finish...and don't cache anything otherwise you get the same problem!
        try {
            Connection connection = connectionFactory.newConnection(new Address[] { new Address(host) });
            Channel channel = connection.createChannel();

            channel.queueDeclare("testQueue", false, false, false, new HashMap<String, Object>());
            channel.queueBind("testQueue", "amq.topic", "#");

            channel.close();
            connection.close();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
TOP

Related Classes of net.joshdevins.rabbitmq.client.ha.it.TestHaConnectionListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.