Example usage for com.rabbitmq.client.impl AMQCommand AMQCommand

List of usage examples for com.rabbitmq.client.impl AMQCommand AMQCommand

Introduction

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

Prototype

public AMQCommand(com.rabbitmq.client.Method method) 

Source Link

Document

Construct a command with just a method, and without header or body.

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(/*from   w  ww . j a  v  a2 s .  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());
}