Search in sources :

Example 6 with AMQConnection

use of com.rabbitmq.client.impl.AMQConnection 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 7 with AMQConnection

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

the class AutorecoveringConnection method setupErrorOnWriteListenerForPotentialRecovery.

private void setupErrorOnWriteListenerForPotentialRecovery() {
    final ThreadFactory threadFactory = this.params.getThreadFactory();
    final Lock errorOnWriteLock = new ReentrantLock();
    this.params.setErrorOnWriteListener((connection, exception) -> {
        // we should trigger the error handling and the recovery only once
        if (errorOnWriteLock.tryLock()) {
            try {
                Thread recoveryThread = threadFactory.newThread(() -> {
                    AMQConnection c = (AMQConnection) connection;
                    c.handleIoError(exception);
                });
                recoveryThread.setName("RabbitMQ Error On Write Thread");
                recoveryThread.start();
            } finally {
                errorOnWriteLock.unlock();
            }
        }
        throw exception;
    });
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ThreadFactory(java.util.concurrent.ThreadFactory) AMQConnection(com.rabbitmq.client.impl.AMQConnection) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 8 with AMQConnection

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

the class RabbitMQConsumerDispatchInterceptor method recordRootSpan.

private void recordRootSpan(SpanRecorder recorder, Connection connection, Envelope envelope, Map<String, Object> headers) {
    recorder.recordServiceType(RabbitMQClientConstants.RABBITMQ_CLIENT);
    recorder.recordApi(CONSUMER_ENTRY_METHOD_DESCRIPTOR);
    String endPoint = RabbitMQClientConstants.UNKNOWN;
    String remoteAddress = RabbitMQClientConstants.UNKNOWN;
    if (connection instanceof AMQConnection) {
        AMQConnection amqConnection = (AMQConnection) connection;
        FrameHandler frameHandler = amqConnection.getFrameHandler();
        // Endpoint should be the local socket address of the consumer.
        if (frameHandler instanceof LocalAddressAccessor) {
            endPoint = ((LocalAddressAccessor) frameHandler)._$PINPOINT$_getLocalAddress();
        }
        // Remote address is the socket address of where the consumer is connected to.
        if (frameHandler instanceof RemoteAddressAccessor) {
            remoteAddress = ((RemoteAddressAccessor) frameHandler)._$PINPOINT$_getRemoteAddress();
        }
    }
    recorder.recordEndPoint(endPoint);
    recorder.recordRemoteAddress(remoteAddress);
    String exchange = StringUtils.defaultIfEmpty(envelope.getExchange(), RabbitMQClientConstants.UNKNOWN);
    recorder.recordRpcName("rabbitmq://exchange=" + exchange);
    recorder.recordAcceptorHost("exchange-" + exchange);
    if (isDebug) {
        logger.debug("endPoint={}->{}", envelope.getExchange(), exchange);
    }
    recorder.recordAttribute(RabbitMQClientConstants.RABBITMQ_ROUTINGKEY_ANNOTATION_KEY, envelope.getRoutingKey());
    if (MapUtils.hasLength(headers)) {
        Object parentApplicationName = headers.get(RabbitMQClientConstants.META_PARENT_APPLICATION_NAME);
        if (!recorder.isRoot() && parentApplicationName != null) {
            Object parentApplicationType = headers.get(RabbitMQClientConstants.META_PARENT_APPLICATION_TYPE);
            recorder.recordParentApplication(parentApplicationName.toString(), NumberUtils.parseShort(parentApplicationType.toString(), ServiceType.UNDEFINED.getCode()));
        }
    }
}
Also used : LocalAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.LocalAddressAccessor) FrameHandler(com.rabbitmq.client.impl.FrameHandler) AMQConnection(com.rabbitmq.client.impl.AMQConnection) RemoteAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.RemoteAddressAccessor)

Example 9 with AMQConnection

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

the class ChannelBasicPublishInterceptor method after.

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args);
    }
    if (!validate(target, args)) {
        return;
    }
    final Trace trace = traceContext.currentTraceObject();
    if (trace == null) {
        return;
    }
    try {
        String exchange = (String) args[0];
        String routingKey = (String) args[1];
        if (exchange == null || exchange.equals("")) {
            exchange = RabbitMQClientConstants.UNKNOWN;
        }
        SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        recorder.recordApi(descriptor);
        String endPoint = RabbitMQClientConstants.UNKNOWN;
        // Producer's endPoint should be the socket address of where the producer is actually connected to.
        final Connection connection = ((Channel) target).getConnection();
        if (connection instanceof AMQConnection) {
            AMQConnection amqConnection = (AMQConnection) connection;
            FrameHandler frameHandler = amqConnection.getFrameHandler();
            if (frameHandler instanceof RemoteAddressAccessor) {
                endPoint = ((RemoteAddressAccessor) frameHandler)._$PINPOINT$_getRemoteAddress();
            }
        }
        recorder.recordEndPoint(endPoint);
        // DestinationId is used to render the virtual queue node.
        // We choose the exchange name as the logical name of the queue node.
        recorder.recordDestinationId("exchange-" + exchange);
        recorder.recordAttribute(RabbitMQClientConstants.RABBITMQ_EXCHANGE_ANNOTATION_KEY, exchange);
        recorder.recordAttribute(RabbitMQClientConstants.RABBITMQ_ROUTINGKEY_ANNOTATION_KEY, routingKey);
        recorder.recordException(throwable);
    } finally {
        trace.traceBlockEnd();
    }
}
Also used : Trace(com.navercorp.pinpoint.bootstrap.context.Trace) FrameHandler(com.rabbitmq.client.impl.FrameHandler) AMQConnection(com.rabbitmq.client.impl.AMQConnection) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) Channel(com.rabbitmq.client.Channel) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) RemoteAddressAccessor(com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.RemoteAddressAccessor)

Example 10 with AMQConnection

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

the class BrokenFramesTest method methodThenBody.

@Test
public void methodThenBody() throws Exception {
    List<Frame> frames = new ArrayList<Frame>();
    byte[] contentBody = new byte[10];
    int channelNumber = 0;
    Publish method = new Publish(1, "test", "test", false, false);
    frames.add(method.toFrame(0));
    frames.add(Frame.fromBodyFragment(channelNumber, contentBody, 0, contentBody.length));
    myFrameHandler.setFrames(frames.iterator());
    try {
        new AMQConnection(factory.params(Executors.newFixedThreadPool(1)), myFrameHandler).start();
    } catch (IOException e) {
        UnexpectedFrameError unexpectedFrameError = findUnexpectedFrameError(e);
        assertNotNull(unexpectedFrameError);
        assertEquals(AMQP.FRAME_BODY, unexpectedFrameError.getReceivedFrame().getType());
        assertEquals(AMQP.FRAME_HEADER, unexpectedFrameError.getExpectedFrameType());
        return;
    }
    fail("No UnexpectedFrameError thrown");
}
Also used : Frame(com.rabbitmq.client.impl.Frame) AMQConnection(com.rabbitmq.client.impl.AMQConnection) ArrayList(java.util.ArrayList) UnexpectedFrameError(com.rabbitmq.client.UnexpectedFrameError) IOException(java.io.IOException) Publish(com.rabbitmq.client.impl.AMQImpl.Basic.Publish) Test(org.junit.Test)

Aggregations

AMQConnection (com.rabbitmq.client.impl.AMQConnection)14 Test (org.junit.Test)9 IOException (java.io.IOException)6 ChannelContinuationTimeoutException (com.rabbitmq.client.ChannelContinuationTimeoutException)3 Connection (com.rabbitmq.client.Connection)3 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)3 Method (com.rabbitmq.client.Method)3 ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)3 FrameHandler (com.rabbitmq.client.impl.FrameHandler)3 ArrayList (java.util.ArrayList)3 Trace (com.navercorp.pinpoint.bootstrap.context.Trace)2 RemoteAddressAccessor (com.navercorp.pinpoint.plugin.rabbitmq.client.field.accessor.RemoteAddressAccessor)2 UnexpectedFrameError (com.rabbitmq.client.UnexpectedFrameError)2 AMQCommand (com.rabbitmq.client.impl.AMQCommand)2 Frame (com.rabbitmq.client.impl.Frame)2 SocketTimeoutException (java.net.SocketTimeoutException)2 TimeoutException (java.util.concurrent.TimeoutException)2 BrokerAddresses (com.meltwater.rxrabbit.BrokerAddresses)1 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)1 SpanRecorder (com.navercorp.pinpoint.bootstrap.context.SpanRecorder)1