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