private void checkStartState() { if (!this.isRunning()) { try { Assert.state(this.startedLatch.await(START_WAIT_TIME, TimeUnit.SECONDS), "Container is not started - cannot adjust queues"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AmqpException("Interrupted waiting for start", e); } } }
private void checkStartState() { if (!this.isRunning()) { try { Assert.state(this.startedLatch.await(START_WAIT_TIME, TimeUnit.SECONDS), "Container is not started - cannot adjust queues"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AmqpException("Interrupted waiting for start", e); } } }
/** * Construct a template using the supplied uri. * @param uri the uri - must include user info, e.g. * "http://guest:guest@localhost:15672/api/". * @since 2.0 */ public RabbitManagementTemplate(String uri) { try { this.rabbitClient = new Client(uri); } catch (Exception e) { throw new AmqpException(e); } }
/** * Construct a template using the supplied uri. * @param uri the uri - must include user info, e.g. * "http://guest:guest@localhost:15672/api/". * @since 2.0 */ public RabbitManagementTemplate(String uri) { try { this.rabbitClient = new Client(uri); } catch (Exception e) { throw new AmqpException(e); } }
/** * Construct a template using the supplied uri. * @param uri the uri. * @param username the user. * @param password the password. */ public RabbitManagementTemplate(String uri, String username, String password) { try { this.rabbitClient = new Client(uri, username, password); } catch (Exception e) { throw new AmqpException(e); } }
/** * Construct a template using the supplied uri. * @param uri the uri. * @param username the user. * @param password the password. */ public RabbitManagementTemplate(String uri, String username, String password) { try { this.rabbitClient = new Client(uri, username, password); } catch (Exception e) { throw new AmqpException(e); } }
@Nullable private ChannelCachingConnectionProxy waitForConnection(long now) { ChannelCachingConnectionProxy cachedConnection = null; while (cachedConnection == null && System.currentTimeMillis() - now < this.channelCheckoutTimeout) { if (countOpenConnections() >= this.connectionLimit) { try { this.connectionMonitor.wait(this.channelCheckoutTimeout); cachedConnection = findIdleConnection(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AmqpException("Interrupted while waiting for a connection", e); } } } return cachedConnection; }
/** * @param payloadClass the payload class. * @return the handler. */ protected InvocableHandlerMethod getHandlerForPayload(Class<? extends Object> payloadClass) { InvocableHandlerMethod handler = this.cachedHandlers.get(payloadClass); if (handler == null) { handler = findHandlerForPayload(payloadClass); if (handler == null) { throw new AmqpException("No method found for " + payloadClass); } this.cachedHandlers.putIfAbsent(payloadClass, handler); //NOSONAR setupReplyTo(handler); } return handler; }
@Nullable private ChannelCachingConnectionProxy waitForConnection(long now) { ChannelCachingConnectionProxy cachedConnection = null; while (cachedConnection == null && System.currentTimeMillis() - now < this.channelCheckoutTimeout) { if (countOpenConnections() >= this.connectionLimit) { try { this.connectionMonitor.wait(this.channelCheckoutTimeout); cachedConnection = findIdleConnection(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AmqpException("Interrupted while waiting for a connection", e); } } } return cachedConnection; }
/** * @param payloadClass the payload class. * @return the handler. */ protected InvocableHandlerMethod getHandlerForPayload(Class<? extends Object> payloadClass) { InvocableHandlerMethod handler = this.cachedHandlers.get(payloadClass); if (handler == null) { handler = findHandlerForPayload(payloadClass); if (handler == null) { throw new AmqpException("No method found for " + payloadClass); } this.cachedHandlers.putIfAbsent(payloadClass, handler); //NOSONAR setupReplyTo(handler); } return handler; }
protected InvocableHandlerMethod findHandlerForPayload(Class<? extends Object> payloadClass) { InvocableHandlerMethod result = null; for (InvocableHandlerMethod handler : this.handlers) { if (matchHandlerMethod(payloadClass, handler)) { if (result != null) { boolean resultIsDefault = result.equals(this.defaultHandler); if (!handler.equals(this.defaultHandler) && !resultIsDefault) { throw new AmqpException("Ambiguous methods for payload type: " + payloadClass + ": " + result.getMethod().getName() + " and " + handler.getMethod().getName()); } if (!resultIsDefault) { continue; // otherwise replace the result with the actual match } } result = handler; } } return result != null ? result : this.defaultHandler; }
protected boolean matchHandlerMethod(Class<? extends Object> payloadClass, InvocableHandlerMethod handler) { Method method = handler.getMethod(); Annotation[][] parameterAnnotations = method.getParameterAnnotations(); // Single param; no annotation or not @Header if (parameterAnnotations.length == 1) { MethodParameter methodParameter = new MethodParameter(method, 0); if ((methodParameter.getParameterAnnotations().length == 0 || !methodParameter.hasParameterAnnotation(Header.class)) && methodParameter.getParameterType().isAssignableFrom(payloadClass)) { return true; } } boolean foundCandidate = false; for (int i = 0; i < parameterAnnotations.length; i++) { MethodParameter methodParameter = new MethodParameter(method, i); if ((methodParameter.getParameterAnnotations().length == 0 || !methodParameter.hasParameterAnnotation(Header.class)) && methodParameter.getParameterType().isAssignableFrom(payloadClass)) { if (foundCandidate) { throw new AmqpException("Ambiguous payload parameter for " + method.toGenericString()); } foundCandidate = true; } } return foundCandidate; }
protected boolean matchHandlerMethod(Class<? extends Object> payloadClass, InvocableHandlerMethod handler) { Method method = handler.getMethod(); Annotation[][] parameterAnnotations = method.getParameterAnnotations(); // Single param; no annotation or not @Header if (parameterAnnotations.length == 1) { MethodParameter methodParameter = new MethodParameter(method, 0); if ((methodParameter.getParameterAnnotations().length == 0 || !methodParameter.hasParameterAnnotation(Header.class)) && methodParameter.getParameterType().isAssignableFrom(payloadClass)) { return true; } } boolean foundCandidate = false; for (int i = 0; i < parameterAnnotations.length; i++) { MethodParameter methodParameter = new MethodParameter(method, i); if ((methodParameter.getParameterAnnotations().length == 0 || !methodParameter.hasParameterAnnotation(Header.class)) && methodParameter.getParameterType().isAssignableFrom(payloadClass)) { if (foundCandidate) { throw new AmqpException("Ambiguous payload parameter for " + method.toGenericString()); } foundCandidate = true; } } return foundCandidate; }
protected InvocableHandlerMethod findHandlerForPayload(Class<? extends Object> payloadClass) { InvocableHandlerMethod result = null; for (InvocableHandlerMethod handler : this.handlers) { if (matchHandlerMethod(payloadClass, handler)) { if (result != null) { boolean resultIsDefault = result.equals(this.defaultHandler); if (!handler.equals(this.defaultHandler) && !resultIsDefault) { throw new AmqpException("Ambiguous methods for payload type: " + payloadClass + ": " + result.getMethod().getName() + " and " + handler.getMethod().getName()); } if (!resultIsDefault) { continue; // otherwise replace the result with the actual match } } result = handler; } } return result != null ? result : this.defaultHandler; }
/** * Determine a reply-to Address for the given message. * <p> * The default implementation first checks the Rabbit Reply-To Address of the supplied request; if that is not * <code>null</code> it is returned; if it is <code>null</code>, then the configured default Exchange and * routing key are used to construct a reply-to Address. If the exchange property is also <code>null</code>, * then an {@link AmqpException} is thrown. * @param request the original incoming Rabbit message * @return the reply-to Address (never <code>null</code>) * @throws AmqpException if no {@link Address} can be determined * @see org.springframework.amqp.core.Message#getMessageProperties() * @see org.springframework.amqp.core.MessageProperties#getReplyTo() */ private Address getReplyToAddress(Message request) throws AmqpException { Address replyTo = request.getMessageProperties().getReplyToAddress(); if (replyTo == null) { if (this.exchange == null) { throw new AmqpException( "Cannot determine ReplyTo message property value: " + "Request message does not contain reply-to property, and no default Exchange was set."); } replyTo = new Address(this.exchange, this.routingKey); } return replyTo; }
public void commitAll() throws AmqpException { try { for (Channel channel : this.channels) { if (this.deliveryTags.containsKey(channel)) { for (Long deliveryTag : this.deliveryTags.get(channel)) { channel.basicAck(deliveryTag, false); } } channel.txCommit(); } } catch (IOException e) { throw new AmqpException("failed to commit RabbitMQ transaction", e); } }
public void commitAll() throws AmqpException { try { for (Channel channel : this.channels) { if (this.deliveryTags.containsKey(channel)) { for (Long deliveryTag : this.deliveryTags.get(channel)) { channel.basicAck(deliveryTag, false); } } channel.txCommit(); } } catch (IOException e) { throw new AmqpException("failed to commit RabbitMQ transaction", e); } }
private void sendDirect(Channel channel, String exchange, String routingKey, Message message, CorrelationData correlationData) { message.getMessageProperties().setReplyTo(Address.AMQ_RABBITMQ_REPLY_TO); try { if (channel instanceof PublisherCallbackChannel) { this.template.addListener(channel); } this.template.doSend(channel, exchange, routingKey, message, this.template.isMandatoryFor(message), correlationData); } catch (Exception e) { throw new AmqpException("Failed to send request", e); } }
private void sendDirect(Channel channel, String exchange, String routingKey, Message message, CorrelationData correlationData) { message.getMessageProperties().setReplyTo(Address.AMQ_RABBITMQ_REPLY_TO); try { if (channel instanceof PublisherCallbackChannel) { this.template.addListener(channel); } this.template.doSend(channel, exchange, routingKey, message, this.template.isMandatoryFor(message), correlationData); } catch (Exception e) { throw new AmqpException("Failed to send request", e); } }
@Test public void testConvertRabbitAccessException() { assertThat(RabbitExceptionTranslator.convertRabbitAccessException(new PossibleAuthenticationFailureException(new RuntimeException())), instanceOf(AmqpAuthenticationException.class)); assertThat(RabbitExceptionTranslator.convertRabbitAccessException(new AmqpException("")), instanceOf(AmqpException.class)); assertThat(RabbitExceptionTranslator.convertRabbitAccessException(new ShutdownSignalException(false, false, null, null)), instanceOf(AmqpConnectException.class)); assertThat(RabbitExceptionTranslator.convertRabbitAccessException(new ConnectException()), instanceOf(AmqpConnectException.class)); assertThat(RabbitExceptionTranslator.convertRabbitAccessException(new IOException()), instanceOf(AmqpIOException.class)); assertThat(RabbitExceptionTranslator.convertRabbitAccessException(new UnsupportedEncodingException()), instanceOf(AmqpUnsupportedEncodingException.class)); assertThat(RabbitExceptionTranslator.convertRabbitAccessException(new Exception() { private static final long serialVersionUID = 1L; }), instanceOf(UncategorizedAmqpException.class)); }