Search in sources :

Example 1 with Method

use of com.rabbitmq.client.Method in project rabbitmq-java-client by rabbitmq.

the class AMQChannelTest method rpcReturnsResultWhenResponseHasCome.

@Test
public void rpcReturnsResultWhenResponseHasCome() throws IOException {
    int rpcTimeout = 1000;
    AMQConnection connection = mock(AMQConnection.class);
    when(connection.getChannelRpcTimeout()).thenReturn(rpcTimeout);
    final DummyAmqChannel channel = new DummyAmqChannel(connection, 1);
    Method method = new AMQImpl.Queue.Declare.Builder().queue("").durable(false).exclusive(true).autoDelete(true).arguments(null).build();
    final Method response = new AMQImpl.Queue.DeclareOk.Builder().queue("whatever").consumerCount(0).messageCount(0).build();
    scheduler.schedule(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            channel.handleCompleteInboundCommand(new AMQCommand(response));
            return null;
        }
    }, (long) (rpcTimeout / 2.0), TimeUnit.MILLISECONDS);
    AMQCommand rpcResponse = channel.rpc(method);
    assertThat(rpcResponse.getMethod(), is(response));
}
Also used : AMQConnection(com.rabbitmq.client.impl.AMQConnection) Method(com.rabbitmq.client.Method) ChannelContinuationTimeoutException(com.rabbitmq.client.ChannelContinuationTimeoutException) IOException(java.io.IOException) AMQCommand(com.rabbitmq.client.impl.AMQCommand) Test(org.junit.Test)

Example 2 with Method

use of com.rabbitmq.client.Method in project rabbitmq-java-client by rabbitmq.

the class AMQChannelTest method testRpcTimeoutReplyComesDuringNexRpc.

@Test
public void testRpcTimeoutReplyComesDuringNexRpc() throws Exception {
    int rpcTimeout = 100;
    AMQConnection connection = mock(AMQConnection.class);
    when(connection.getChannelRpcTimeout()).thenReturn(rpcTimeout);
    when(connection.willCheckRpcResponseType()).thenReturn(Boolean.TRUE);
    final DummyAmqChannel channel = new DummyAmqChannel(connection, 1);
    Method method = new AMQImpl.Queue.Declare.Builder().queue("123").durable(false).exclusive(true).autoDelete(true).arguments(null).build();
    try {
        channel.rpc(method);
        fail("Should time out and throw an exception");
    } catch (final ChannelContinuationTimeoutException e) {
        // OK
        assertThat((DummyAmqChannel) e.getChannel(), is(channel));
        assertThat(e.getChannelNumber(), is(channel.getChannelNumber()));
        assertThat(e.getMethod(), is(method));
        assertNull("outstanding RPC should have been cleaned", channel.nextOutstandingRpc());
    }
    // now do a basic.consume request and have the queue.declareok returned instead
    method = new AMQImpl.Basic.Consume.Builder().queue("123").consumerTag("").arguments(null).build();
    final Method response1 = new AMQImpl.Queue.DeclareOk.Builder().queue("123").consumerCount(0).messageCount(0).build();
    final Method response2 = new AMQImpl.Basic.ConsumeOk.Builder().consumerTag("456").build();
    scheduler.schedule(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            channel.handleCompleteInboundCommand(new AMQCommand(response1));
            Thread.sleep(10);
            channel.handleCompleteInboundCommand(new AMQCommand(response2));
            return null;
        }
    }, (long) (rpcTimeout / 2.0), TimeUnit.MILLISECONDS);
    AMQCommand rpcResponse = channel.rpc(method);
    assertThat(rpcResponse.getMethod(), is(response2));
}
Also used : AMQConnection(com.rabbitmq.client.impl.AMQConnection) Method(com.rabbitmq.client.Method) ChannelContinuationTimeoutException(com.rabbitmq.client.ChannelContinuationTimeoutException) ChannelContinuationTimeoutException(com.rabbitmq.client.ChannelContinuationTimeoutException) IOException(java.io.IOException) AMQImpl(com.rabbitmq.client.impl.AMQImpl) AMQCommand(com.rabbitmq.client.impl.AMQCommand) Test(org.junit.Test)

Example 3 with Method

use of com.rabbitmq.client.Method in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method correctProtocolHeader.

@Test
public void correctProtocolHeader() throws IOException {
    ConnectionFactory factory = TestUtils.connectionFactory();
    SocketFrameHandler fh = new SocketFrameHandler(SocketFactory.getDefault().createSocket("localhost", AMQP.PROTOCOL.PORT));
    fh.sendHeader();
    AMQCommand command = new AMQCommand();
    while (!command.handleFrame(fh.readFrame())) {
    }
    Method m = command.getMethod();
    // System.out.println(m.getClass());
    assertTrue("First command must be Connection.start", m instanceof AMQP.Connection.Start);
    AMQP.Connection.Start start = (AMQP.Connection.Start) m;
    assertTrue("Version in Connection.start is <= what we sent", start.getVersionMajor() < AMQP.PROTOCOL.MAJOR || (start.getVersionMajor() == AMQP.PROTOCOL.MAJOR && start.getVersionMinor() <= AMQP.PROTOCOL.MINOR));
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) AMQP(com.rabbitmq.client.AMQP) Method(com.rabbitmq.client.Method) SocketFrameHandler(com.rabbitmq.client.impl.SocketFrameHandler) AMQCommand(com.rabbitmq.client.impl.AMQCommand) Test(org.junit.Test)

Example 4 with Method

use of com.rabbitmq.client.Method in project rabbitmq-java-client by rabbitmq.

the class ChannelN method basicGet.

/**
 * Public API - {@inheritDoc}
 */
@Override
public GetResponse basicGet(String queue, boolean autoAck) throws IOException {
    validateQueueNameLength(queue);
    AMQCommand replyCommand = exnWrappingRpc(new Basic.Get.Builder().queue(queue).noAck(autoAck).build());
    Method method = replyCommand.getMethod();
    if (method instanceof Basic.GetOk) {
        Basic.GetOk getOk = (Basic.GetOk) method;
        Envelope envelope = new Envelope(getOk.getDeliveryTag(), getOk.getRedelivered(), getOk.getExchange(), getOk.getRoutingKey());
        BasicProperties props = (BasicProperties) replyCommand.getContentHeader();
        byte[] body = replyCommand.getContentBody();
        int messageCount = getOk.getMessageCount();
        metricsCollector.consumedMessage(this, getOk.getDeliveryTag(), autoAck);
        return new GetResponse(envelope, props, body, messageCount);
    } else if (method instanceof Basic.GetEmpty) {
        return null;
    } else {
        throw new UnexpectedMethodError(method);
    }
}
Also used : Basic(com.rabbitmq.client.impl.AMQImpl.Basic) BasicProperties(com.rabbitmq.client.AMQP.BasicProperties) Method(com.rabbitmq.client.Method)

Example 5 with Method

use of com.rabbitmq.client.Method in project rabbitmq-java-client by rabbitmq.

the class AMQBuilderApiTest method particularBuilderForBasicSanityWithRpc.

@Test
public void particularBuilderForBasicSanityWithRpc() throws IOException {
    Method retVal = channel.rpc(new AMQP.Exchange.Declare.Builder().exchange(XCHG_NAME).type("direct").durable(false).build()).getMethod();
    assertTrue("Channel should still be open.", channel.isOpen());
    assertTrue(retVal instanceof AMQP.Exchange.DeclareOk);
    retVal = channel.rpc(new AMQP.Exchange.Delete.Builder().exchange(XCHG_NAME).build()).getMethod();
    assertTrue("Channel should still be open.", channel.isOpen());
    assertTrue(retVal instanceof AMQP.Exchange.DeleteOk);
}
Also used : AMQP(com.rabbitmq.client.AMQP) Method(com.rabbitmq.client.Method) Test(org.junit.Test)

Aggregations

Method (com.rabbitmq.client.Method)10 Test (org.junit.Test)5 IOException (java.io.IOException)4 ChannelContinuationTimeoutException (com.rabbitmq.client.ChannelContinuationTimeoutException)3 AMQCommand (com.rabbitmq.client.impl.AMQCommand)3 AMQConnection (com.rabbitmq.client.impl.AMQConnection)3 AMQP (com.rabbitmq.client.AMQP)2 Basic (com.rabbitmq.client.impl.AMQImpl.Basic)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 AMQImpl (com.rabbitmq.client.impl.AMQImpl)1 Channel (com.rabbitmq.client.impl.AMQImpl.Channel)1 SocketFrameHandler (com.rabbitmq.client.impl.SocketFrameHandler)1 SocketTimeoutException (java.net.SocketTimeoutException)1