Example usage for org.springframework.amqp AmqpIOException AmqpIOException

List of usage examples for org.springframework.amqp AmqpIOException AmqpIOException

Introduction

In this page you can find the example usage for org.springframework.amqp AmqpIOException AmqpIOException.

Prototype

public AmqpIOException(IOException cause) 

Source Link

Document

Construct an instance with the provided cause.

Usage

From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.java

/**
 * Obtain a RabbitMQ Channel that is synchronized with the current
 * transaction, if any./*from w  ww .j a v a  2 s.  c o m*/
 * 
 * @param connectionFactory
 *            the RabbitMQ ConnectionFactory to bind for (used as
 *            TransactionSynchronizationManager key)
 * @param resourceFactory
 *            the ResourceFactory to use for extracting or creating RabbitMQ
 *            resources
 * @return the transactional Channel, or <code>null</code> if none found
 */
private static RabbitResourceHolder doGetTransactionalResourceHolder(ConnectionFactory connectionFactory,
        ResourceFactory resourceFactory) {

    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    Assert.notNull(resourceFactory, "ResourceFactory must not be null");

    RabbitResourceHolder resourceHolder = (RabbitResourceHolder) TransactionSynchronizationManager
            .getResource(connectionFactory);
    if (resourceHolder != null) {
        Channel channel = resourceFactory.getChannel(resourceHolder);
        if (channel != null) {
            return resourceHolder;
        }
    }
    RabbitResourceHolder resourceHolderToUse = resourceHolder;
    if (resourceHolderToUse == null) {
        resourceHolderToUse = new RabbitResourceHolder();
    }
    Connection connection = resourceFactory.getConnection(resourceHolderToUse);
    Channel channel = null;
    try {
        boolean isExistingCon = (connection != null);
        if (!isExistingCon) {
            connection = resourceFactory.createConnection();
            resourceHolderToUse.addConnection(connection);
        }
        channel = consumerChannel.get();
        if (channel == null) {
            channel = resourceFactory.createChannel(connection);
        }
        if (null != channel && channel.isOpen()) {
            resourceHolderToUse.addChannel(channel, connection);
        }

        if (resourceHolderToUse != resourceHolder) {
            bindResourceToTransaction(resourceHolderToUse, connectionFactory,
                    resourceFactory.isSynchedLocalTransactionAllowed());
        }

        return resourceHolderToUse;

    } catch (IOException ex) {
        RabbitUtils.closeChannel(channel);
        RabbitUtils.closeConnection(connection);
        throw new AmqpIOException(ex);
    }
}

From source file:org.springframework.amqp.rabbit.connection.RabbitResourceHolder.java

public void rollbackAll() {
    for (Channel channel : this.channels) {
        if (logger.isDebugEnabled()) {
            logger.debug("Rolling back messages to channel: " + channel);
        }//w ww.ja  va2 s .c  o  m
        RabbitUtils.rollbackIfNecessary(channel);
        if (deliveryTags.containsKey(channel)) {
            for (Long deliveryTag : deliveryTags.get(channel)) {
                try {
                    channel.basicReject(deliveryTag, true);
                } catch (IOException ex) {
                    throw new AmqpIOException(ex);
                }
            }
            // Need to commit the reject (=nack)
            RabbitUtils.commitIfNecessary(channel);
        }
    }
}

From source file:org.springframework.amqp.rabbit.connection.RabbitUtils.java

/**
 * Commit the Channel if not within a JTA transaction.
 * @param channel the RabbitMQ Channel to commit
 *//*w  w  w.  ja  v  a2s.  com*/
public static void commitIfNecessary(Channel channel) {
    Assert.notNull(channel, "Channel must not be null");
    try {
        channel.txCommit();
    } catch (IOException ex) {
        throw new AmqpIOException(ex);
    }
}

From source file:org.springframework.amqp.rabbit.connection.RabbitUtils.java

public static void rollbackIfNecessary(Channel channel) {
    Assert.notNull(channel, "Channel must not be null");
    try {/*w w  w. ja v  a 2s.  c o  m*/
        channel.txRollback();
    } catch (IOException ex) {
        throw new AmqpIOException(ex);
    }
}

From source file:org.springframework.amqp.rabbit.connection.RabbitUtils.java

public static RuntimeException convertRabbitAccessException(Throwable ex) {
    Assert.notNull(ex, "Exception must not be null");
    if (ex instanceof AmqpException) {
        return (AmqpException) ex;
    }/*from   ww w .  ja v  a  2 s.  c o  m*/
    if (ex instanceof ShutdownSignalException) {
        return new AmqpConnectException((ShutdownSignalException) ex);
    }
    if (ex instanceof ConnectException) {
        return new AmqpConnectException((ConnectException) ex);
    }
    if (ex instanceof IOException) {
        return new AmqpIOException((IOException) ex);
    }
    if (ex instanceof UnsupportedEncodingException) {
        return new AmqpUnsupportedEncodingException(ex);
    }
    // fallback
    return new UncategorizedAmqpException(ex);
}

From source file:org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.java

/**
 * Invoke the specified listener method.
 * @param methodName the name of the listener method
 * @param arguments the message arguments to be passed in
 * @return the result returned from the listener method
 * @throws Exception if thrown by Rabbit API methods
 * @see #getListenerMethodName//from w  w  w. j  a  v a 2 s .  c  o  m
 * @see #buildListenerArguments
 */
protected Object invokeListenerMethod(String methodName, Object[] arguments) throws Exception {
    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetObject(getDelegate());
        methodInvoker.setTargetMethod(methodName);
        methodInvoker.setArguments(arguments);
        methodInvoker.prepare();
        return methodInvoker.invoke();
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (targetEx instanceof IOException) {
            throw new AmqpIOException((IOException) targetEx);
        } else {
            throw new ListenerExecutionFailedException("Listener method '" + methodName + "' threw exception",
                    targetEx);
        }
    } catch (Throwable ex) {
        ArrayList<String> arrayClass = new ArrayList<String>();
        if (arguments != null) {
            for (int i = 0; i < arguments.length; i++) {
                arrayClass.add(arguments[i].getClass().toString());
            }
        }
        throw new ListenerExecutionFailedException("Failed to invoke target method '" + methodName
                + "' with argument type = [" + StringUtils.collectionToCommaDelimitedString(arrayClass)
                + "], value = [" + ObjectUtils.nullSafeToString(arguments) + "]", ex);
    }
}

From source file:org.springframework.amqp.rabbit.support.RabbitUtils.java

public static AmqpException convertRabbitAccessException(Throwable ex) {
    Assert.notNull(ex, "Exception must not be null");
    if (ex instanceof AmqpException) {
        return (AmqpException) ex;
    }//from  w ww  . j ava2 s . co  m
    if (ex instanceof ConnectException) {
        return new AmqpConnectException((ConnectException) ex);
    }
    if (ex instanceof IOException) {
        return new AmqpIOException((IOException) ex);
    }
    if (ex instanceof UnsupportedEncodingException) {
        return new AmqpUnsupportedEncodingException(ex);
    }
    // fallback
    return new UncategorizedAmqpException(ex);
}

From source file:org.springframework.amqp.support.postprocessor.AbstractCompressingPostProcessor.java

@Override
public Message postProcessMessage(Message message) throws AmqpException {
    ByteArrayOutputStream zipped = new ByteArrayOutputStream();
    try {//from  w w  w .  j  a va  2  s. co  m
        OutputStream zipper = getCompressorStream(zipped);
        FileCopyUtils.copy(new ByteArrayInputStream(message.getBody()), zipper);
        MessageProperties messageProperties = message.getMessageProperties();
        String currentEncoding = messageProperties.getContentEncoding();
        messageProperties
                .setContentEncoding(getEncoding() + (currentEncoding == null ? "" : ":" + currentEncoding));
        if (this.autoDecompress) {
            messageProperties.setHeader(MessageProperties.SPRING_AUTO_DECOMPRESS, true);
        }
        byte[] compressed = zipped.toByteArray();
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("Compressed " + message.getBody().length + " to " + compressed.length);
        }
        return new Message(compressed, messageProperties);
    } catch (IOException e) {
        throw new AmqpIOException(e);
    }
}