Search in sources :

Example 1 with AMQCommand

use of com.rabbitmq.client.impl.AMQCommand in project pinpoint by naver.

the class RabbitMQConsumerHandleCompleteInboundCommandInterceptor method after.

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (!validate(target, args)) {
        return;
    }
    AMQCommand command = (AMQCommand) args[0];
    Method method = command.getMethod();
    if (!(method instanceof AMQP.Basic.GetOk)) {
        return;
    }
    if (isDebug) {
        logger.afterInterceptor(target, args, result, throwable);
    }
    final Trace trace = traceContext.currentRawTraceObject();
    if (trace == null) {
        return;
    }
    if (!trace.canSampled()) {
        traceContext.removeTraceObject();
    }
    try {
        SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        recorder.recordApi(methodDescriptor);
        if (throwable != null) {
            recorder.recordException(throwable);
        }
    } catch (Throwable th) {
        if (logger.isWarnEnabled()) {
            logger.warn("AFTER. Caused:{}", th.getMessage(), th);
        }
    } finally {
        traceContext.removeTraceObject();
        trace.traceBlockEnd();
        trace.close();
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) AMQP(com.rabbitmq.client.AMQP) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) Method(com.rabbitmq.client.impl.Method) AMQCommand(com.rabbitmq.client.impl.AMQCommand)

Example 2 with AMQCommand

use of com.rabbitmq.client.impl.AMQCommand in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method correctProtocolHeader.

@Test
public void correctProtocolHeader() throws IOException {
    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();
    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 : 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 3 with AMQCommand

use of com.rabbitmq.client.impl.AMQCommand 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);
    when(connection.getTrafficListener()).thenReturn(TrafficListener.NO_OP);
    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()).isEqualTo(channel);
        assertThat(e.getChannelNumber()).isEqualTo(channel.getChannelNumber());
        assertThat(e.getMethod()).isEqualTo(method);
        assertThat(channel.nextOutstandingRpc()).as("outstanding RPC should have been cleaned").isNull();
    }
    // 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((Callable<Void>) () -> {
        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()).isEqualTo(response2);
}
Also used : AMQConnection(com.rabbitmq.client.impl.AMQConnection) Method(com.rabbitmq.client.Method) ChannelContinuationTimeoutException(com.rabbitmq.client.ChannelContinuationTimeoutException) AMQImpl(com.rabbitmq.client.impl.AMQImpl) AMQCommand(com.rabbitmq.client.impl.AMQCommand) Test(org.junit.Test)

Example 4 with AMQCommand

use of com.rabbitmq.client.impl.AMQCommand 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);
    when(connection.getTrafficListener()).thenReturn(TrafficListener.NO_OP);
    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()).isEqualTo(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 5 with AMQCommand

use of com.rabbitmq.client.impl.AMQCommand in project pinpoint by naver.

the class RabbitMQConsumerHandleCompleteInboundCommandInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (!validate(target, args)) {
        return;
    }
    AMQCommand command = (AMQCommand) args[0];
    Method method = command.getMethod();
    if (!(method instanceof AMQP.Basic.GetOk)) {
        return;
    }
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    try {
        AMQChannel channel = (AMQChannel) target;
        final Trace trace = createTrace(channel, command);
        if (trace == null) {
            return;
        }
        if (!trace.canSampled()) {
            return;
        }
        SpanEventRecorder recorder = trace.traceBlockBegin();
        recorder.recordServiceType(RabbitMQClientConstants.RABBITMQ_CLIENT_INTERNAL);
    } catch (Throwable th) {
        if (logger.isWarnEnabled()) {
            logger.warn("BEFORE. Caused:{}", th.getMessage(), th);
        }
    }
}
Also used : AMQChannel(com.rabbitmq.client.impl.AMQChannel) Trace(com.navercorp.pinpoint.bootstrap.context.Trace) AMQP(com.rabbitmq.client.AMQP) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) Method(com.rabbitmq.client.impl.Method) AMQCommand(com.rabbitmq.client.impl.AMQCommand)

Aggregations

AMQCommand (com.rabbitmq.client.impl.AMQCommand)6 AMQP (com.rabbitmq.client.AMQP)3 Method (com.rabbitmq.client.Method)3 Test (org.junit.Test)3 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)2 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)2 ChannelContinuationTimeoutException (com.rabbitmq.client.ChannelContinuationTimeoutException)2 AMQConnection (com.rabbitmq.client.impl.AMQConnection)2 Method (com.rabbitmq.client.impl.Method)2 AMQChannel (com.rabbitmq.client.impl.AMQChannel)1 AMQImpl (com.rabbitmq.client.impl.AMQImpl)1 SocketFrameHandler (com.rabbitmq.client.impl.SocketFrameHandler)1 IOException (java.io.IOException)1