Example usage for com.rabbitmq.client.impl AMQConnection processControlCommand

List of usage examples for com.rabbitmq.client.impl AMQConnection processControlCommand

Introduction

In this page you can find the example usage for com.rabbitmq.client.impl AMQConnection processControlCommand.

Prototype

@SuppressWarnings("unused")
public boolean processControlCommand(Command c) throws IOException 

Source Link

Document

Handles incoming control commands on channel zero.

Usage

From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryLifecycleTests.java

License:Apache License

@Test
public void testBlockedConnection() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

    AtomicReference<ConnectionBlockedEvent> blockedConnectionEvent = new AtomicReference<>();
    AtomicReference<ConnectionUnblockedEvent> unblockedConnectionEvent = new AtomicReference<>();

    context.addApplicationListener((ApplicationListener<ConnectionBlockedEvent>) blockedConnectionEvent::set);

    context.addApplicationListener(//ww w.  ja v  a2s .  c o  m
            (ApplicationListener<ConnectionUnblockedEvent>) unblockedConnectionEvent::set);

    CachingConnectionFactory cf = context.getBean(CachingConnectionFactory.class);

    CountDownLatch blockedConnectionLatch = new CountDownLatch(1);
    CountDownLatch unblockedConnectionLatch = new CountDownLatch(1);

    Connection connection = cf.createConnection();
    connection.addBlockedListener(new BlockedListener() {

        @Override
        public void handleBlocked(String reason) throws IOException {
            blockedConnectionLatch.countDown();
        }

        @Override
        public void handleUnblocked() throws IOException {
            unblockedConnectionLatch.countDown();
        }

    });

    AMQConnection amqConnection = TestUtils.getPropertyValue(connection, "target.delegate",
            AMQConnection.class);
    amqConnection
            .processControlCommand(new AMQCommand(new AMQImpl.Connection.Blocked("Test connection blocked")));

    assertTrue(blockedConnectionLatch.await(10, TimeUnit.SECONDS));

    ConnectionBlockedEvent connectionBlockedEvent = blockedConnectionEvent.get();
    assertNotNull(connectionBlockedEvent);
    assertEquals("Test connection blocked", connectionBlockedEvent.getReason());
    assertSame(TestUtils.getPropertyValue(connection, "target"), connectionBlockedEvent.getConnection());

    amqConnection.processControlCommand(new AMQCommand(new AMQImpl.Connection.Unblocked()));

    assertTrue(unblockedConnectionLatch.await(10, TimeUnit.SECONDS));

    ConnectionUnblockedEvent connectionUnblockedEvent = unblockedConnectionEvent.get();
    assertNotNull(connectionUnblockedEvent);
    assertSame(TestUtils.getPropertyValue(connection, "target"), connectionUnblockedEvent.getConnection());
}