Search in sources :

Example 1 with RMQJMSException

use of com.rabbitmq.jms.util.RMQJMSException in project rabbitmq-jms-client by rabbitmq.

the class RMQMessageProducer method sendAMQPMessage.

private void sendAMQPMessage(RMQDestination destination, RMQMessage msg, int deliveryMode, int priority, long timeToLive) throws JMSException {
    if (!destination.amqpWritable()) {
        this.logger.error("Cannot write to AMQP destination {}", destination);
        throw new RMQJMSException("Cannot write to AMQP destination", new UnsupportedOperationException("MessageProducer.send to undefined AMQP resource"));
    }
    if (msg instanceof RMQBytesMessage || msg instanceof RMQTextMessage) {
        try {
            AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
            bob.contentType("application/octet-stream");
            bob.deliveryMode(RMQMessage.rmqDeliveryMode(deliveryMode));
            bob.priority(priority);
            bob.expiration(rmqExpiration(timeToLive));
            bob.headers(msg.toAmqpHeaders());
            bob = amqpPropertiesCustomiser.apply(bob, msg);
            byte[] data = msg.toAmqpByteArray();
            this.session.getChannel().basicPublish(destination.getAmqpExchangeName(), destination.getAmqpRoutingKey(), bob.build(), data);
        } catch (IOException x) {
            throw new RMQJMSException(x);
        }
    } else {
        this.logger.error("Unsupported message type {} for AMQP destination {}", msg.getClass().getName(), destination);
        throw new RMQJMSException("Unsupported message type for AMQP destination", new UnsupportedOperationException("MessageProducer.send to AMQP resource: Message not Text or Bytes"));
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) AMQP(com.rabbitmq.client.AMQP) RMQTextMessage(com.rabbitmq.jms.client.message.RMQTextMessage) RMQBytesMessage(com.rabbitmq.jms.client.message.RMQBytesMessage) IOException(java.io.IOException)

Example 2 with RMQJMSException

use of com.rabbitmq.jms.util.RMQJMSException in project rabbitmq-jms-client by rabbitmq.

the class RMQMessageProducer method sendJMSMessage.

// protected for testing
protected void sendJMSMessage(RMQDestination destination, RMQMessage msg, int deliveryMode, int priority, long timeToLive) throws JMSException {
    this.session.declareDestinationIfNecessary(destination);
    try {
        AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
        bob.contentType("application/octet-stream");
        bob.deliveryMode(RMQMessage.rmqDeliveryMode(deliveryMode));
        bob.priority(priority);
        bob.expiration(rmqExpiration(timeToLive));
        bob.headers(msg.toHeaders());
        byte[] data = msg.toByteArray();
        this.session.getChannel().basicPublish(destination.getAmqpExchangeName(), destination.getAmqpRoutingKey(), bob.build(), data);
    } catch (IOException x) {
        throw new RMQJMSException(x);
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) AMQP(com.rabbitmq.client.AMQP) IOException(java.io.IOException)

Example 3 with RMQJMSException

use of com.rabbitmq.jms.util.RMQJMSException in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method createConsumerInternal.

/**
 * Creates a consumer for a destination.
 * @param dest internal destination object
 * @param uuidTag only used for topics, if null, one is generated as the queue name for this topic
 * @param durableSubscriber true if this is a durable topic subscription
 * @param jmsSelector selector expression - null if no selection required
 * @return {@link #createConsumer(Destination)}
 * @throws JMSException if destination is null or we fail to create the destination on the broker
 * @see #createConsumer(Destination)
 */
private RMQMessageConsumer createConsumerInternal(RMQDestination dest, String uuidTag, boolean durableSubscriber, String jmsSelector) throws JMSException {
    String consumerTag = uuidTag != null ? uuidTag : Util.generateUUID("jms-cons-");
    logger.trace("create consumer for destination '{}' with consumerTag '{}' and selector '{}'", dest, consumerTag, jmsSelector);
    declareDestinationIfNecessary(dest);
    if (!dest.isQueue()) {
        // The queue name is distinct for each consumer.
        try {
            String queueName = consumerTag;
            this.declareRMQQueue(dest, queueName, durableSubscriber);
            if (nullOrEmpty(jmsSelector)) {
                // bind the queue to the exchange with the correct routing key
                this.channel.queueBind(queueName, dest.getAmqpExchangeName(), dest.getAmqpRoutingKey());
            } else {
                // get this session's topic selector exchange (name)
                String selectionExchange = this.getSelectionExchange(durableSubscriber);
                // bind it to the topic exchange with the topic routing key
                this.channel.exchangeBind(selectionExchange, dest.getAmqpExchangeName(), dest.getAmqpRoutingKey());
                this.bindSelectorQueue(dest, jmsSelector, queueName, selectionExchange);
            }
        } catch (IOException x) {
            logger.error("consumer with tag '{}' could not be created", consumerTag, x);
            throw new RMQJMSException("RabbitMQ Exception creating Consumer", x);
        }
    }
    RMQMessageConsumer consumer = new RMQMessageConsumer(this, dest, consumerTag, getConnection().isStopped(), jmsSelector, this.requeueOnMessageListenerException);
    this.consumers.add(consumer);
    return consumer;
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) IOException(java.io.IOException)

Example 4 with RMQJMSException

use of com.rabbitmq.jms.util.RMQJMSException in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method getBrowsingChannel.

/**
 * Get a (new) channel for queue browsing.
 * @return channel for browsing queues
 * @throws JMSException if channel not available
 */
Channel getBrowsingChannel() throws JMSException {
    try {
        synchronized (this.bcLock) {
            // not transactional
            Channel chan = this.getConnection().createRabbitChannel(false);
            this.browsingChannels.add(chan);
            return chan;
        }
    } catch (Exception e) {
        // includes unchecked exceptions, e.g. ShutdownSignalException
        throw new RMQJMSException("Cannot create browsing channel", e);
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) Channel(com.rabbitmq.client.Channel) TimeoutException(java.util.concurrent.TimeoutException) RMQJMSSelectorException(com.rabbitmq.jms.util.RMQJMSSelectorException) IllegalStateException(javax.jms.IllegalStateException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException) InvalidSelectorException(javax.jms.InvalidSelectorException)

Example 5 with RMQJMSException

use of com.rabbitmq.jms.util.RMQJMSException in project rabbitmq-jms-client by rabbitmq.

the class RMQBytesMessage method recreate.

public static RMQMessage recreate(BytesMessage msg) throws JMSException {
    msg.reset();
    long bodyLength = msg.getBodyLength();
    int bodySize = (int) Math.min(Math.max(0L, bodyLength), Integer.MAX_VALUE);
    if (bodyLength != bodySize)
        throw new JMSException(String.format("BytesMessage body invalid length (%s). Negative or too large.", bodyLength));
    try {
        RMQBytesMessage rmqBMsg = new RMQBytesMessage();
        RMQMessage.copyAttributes(rmqBMsg, msg);
        byte[] byteArray = new byte[bodySize];
        if (bodyLength > 0) {
            if (// must read the whole body at once
            bodySize != msg.readBytes(byteArray, bodySize))
                throw new MessageEOFException("Cannot read all of non-RMQ Message body.");
        }
        rmqBMsg.writeBytes(byteArray);
        // make body read-only and set pointer to start.
        rmqBMsg.reset();
        return rmqBMsg;
    } catch (OutOfMemoryError e) {
        throw new RMQJMSException("Body too large for conversion to RMQMessage.", e);
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) MessageEOFException(javax.jms.MessageEOFException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException)

Aggregations

RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)16 IOException (java.io.IOException)15 JMSException (javax.jms.JMSException)6 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)5 IllegalStateException (javax.jms.IllegalStateException)5 RMQJMSSelectorException (com.rabbitmq.jms.util.RMQJMSSelectorException)4 TimeoutException (java.util.concurrent.TimeoutException)4 InvalidSelectorException (javax.jms.InvalidSelectorException)4 AMQP (com.rabbitmq.client.AMQP)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 MessageEOFException (javax.jms.MessageEOFException)2 MessageNotWriteableException (javax.jms.MessageNotWriteableException)2 Channel (com.rabbitmq.client.Channel)1 RMQBytesMessage (com.rabbitmq.jms.client.message.RMQBytesMessage)1 RMQTextMessage (com.rabbitmq.jms.client.message.RMQTextMessage)1 RMQMessageFormatException (com.rabbitmq.jms.util.RMQMessageFormatException)1 WhiteListObjectInputStream (com.rabbitmq.jms.util.WhiteListObjectInputStream)1 EOFException (java.io.EOFException)1