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);
}
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;
});
}
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()));
}
}
}
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();
}
}
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");
}
Aggregations