use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.
the class ActiveMQMessageProducer method doSendx.
private void doSendx(ActiveMQDestination destination, final Message jmsMessage, final int deliveryMode, final int priority, final long timeToLive, CompletionListener completionListener) throws JMSException {
jmsMessage.setJMSDeliveryMode(deliveryMode);
jmsMessage.setJMSPriority(priority);
if (timeToLive == 0) {
jmsMessage.setJMSExpiration(0);
} else {
jmsMessage.setJMSExpiration(System.currentTimeMillis() + timeToLive);
}
if (!disableMessageTimestamp) {
jmsMessage.setJMSTimestamp(System.currentTimeMillis());
} else {
jmsMessage.setJMSTimestamp(0);
}
SimpleString address = null;
if (destination == null) {
if (defaultDestination == null) {
throw new UnsupportedOperationException("Destination must be specified on send with an anonymous producer");
}
destination = defaultDestination;
} else {
if (defaultDestination != null) {
if (!destination.equals(defaultDestination)) {
throw new UnsupportedOperationException("Where a default destination is specified " + "for the sender and a destination is " + "specified in the arguments to the send, " + "these destinations must be equal");
}
}
address = destination.getSimpleAddress();
if (!connection.containsKnownDestination(address)) {
try {
ClientSession.AddressQuery query = clientSession.addressQuery(address);
if (!query.isExists()) {
if (destination.isQueue() && query.isAutoCreateQueues()) {
clientSession.createAddress(address, RoutingType.ANYCAST, true);
if (destination.isTemporary()) {
// TODO is it right to use the address for the queue name here?
clientSession.createTemporaryQueue(address, RoutingType.ANYCAST, address);
} else {
createQueue(destination, RoutingType.ANYCAST, address, null, true, true, query.getDefaultMaxConsumers(), query.isDefaultPurgeOnNoConsumers(), query.isDefaultExclusive(), query.isDefaultLastValueQueue());
}
} else if (!destination.isQueue() && query.isAutoCreateAddresses()) {
clientSession.createAddress(address, RoutingType.MULTICAST, true);
} else if ((destination.isQueue() && !query.isAutoCreateQueues()) || (!destination.isQueue() && !query.isAutoCreateAddresses())) {
throw new InvalidDestinationException("Destination " + address + " does not exist");
}
} else {
ClientSession.QueueQuery queueQuery = clientSession.queueQuery(address);
if (queueQuery.isExists()) {
connection.addKnownDestination(address);
} else if (destination.isQueue() && query.isAutoCreateQueues()) {
if (destination.isTemporary()) {
clientSession.createTemporaryQueue(address, RoutingType.ANYCAST, address);
} else {
createQueue(destination, RoutingType.ANYCAST, address, null, true, true, query.getDefaultMaxConsumers(), query.isDefaultPurgeOnNoConsumers(), query.isDefaultExclusive(), query.isDefaultLastValueQueue());
}
}
}
} catch (ActiveMQQueueExistsException e) {
// The queue was created by another client/admin between the query check and send create queue packet
} catch (ActiveMQException e) {
throw JMSExceptionHelper.convertFromActiveMQException(e);
}
}
}
ActiveMQMessage activeMQJmsMessage;
boolean foreign = false;
// First convert from foreign message if appropriate
if (!(jmsMessage instanceof ActiveMQMessage)) {
if (jmsMessage instanceof BytesMessage) {
activeMQJmsMessage = new ActiveMQBytesMessage((BytesMessage) jmsMessage, clientSession);
} else if (jmsMessage instanceof MapMessage) {
activeMQJmsMessage = new ActiveMQMapMessage((MapMessage) jmsMessage, clientSession);
} else if (jmsMessage instanceof ObjectMessage) {
activeMQJmsMessage = new ActiveMQObjectMessage((ObjectMessage) jmsMessage, clientSession, options);
} else if (jmsMessage instanceof StreamMessage) {
activeMQJmsMessage = new ActiveMQStreamMessage((StreamMessage) jmsMessage, clientSession);
} else if (jmsMessage instanceof TextMessage) {
activeMQJmsMessage = new ActiveMQTextMessage((TextMessage) jmsMessage, clientSession);
} else {
activeMQJmsMessage = new ActiveMQMessage(jmsMessage, clientSession);
}
// Set the destination on the original message
jmsMessage.setJMSDestination(destination);
foreign = true;
} else {
activeMQJmsMessage = (ActiveMQMessage) jmsMessage;
}
if (!disableMessageID) {
// Generate a JMS id
UUID uid = UUIDGenerator.getInstance().generateUUID();
activeMQJmsMessage.getCoreMessage().setUserID(uid);
activeMQJmsMessage.resetMessageID(null);
}
if (foreign) {
jmsMessage.setJMSMessageID(activeMQJmsMessage.getJMSMessageID());
}
activeMQJmsMessage.setJMSDestination(destination);
try {
activeMQJmsMessage.doBeforeSend();
} catch (Exception e) {
JMSException je = new JMSException(e.getMessage());
je.initCause(e);
throw je;
}
if (defaultDeliveryDelay > 0) {
activeMQJmsMessage.setJMSDeliveryTime(System.currentTimeMillis() + defaultDeliveryDelay);
}
ClientMessage coreMessage = activeMQJmsMessage.getCoreMessage();
coreMessage.putStringProperty(ActiveMQConnection.CONNECTION_ID_PROPERTY_NAME, connID);
coreMessage.setRoutingType(destination.isQueue() ? RoutingType.ANYCAST : RoutingType.MULTICAST);
try {
/**
* Using a completionListener requires wrapping using a {@link CompletionListenerWrapper},
* so we avoid it if we can.
*/
if (completionListener != null) {
clientProducer.send(address, coreMessage, new CompletionListenerWrapper(completionListener, jmsMessage, this));
} else {
clientProducer.send(address, coreMessage);
}
} catch (ActiveMQInterruptedException e) {
JMSException jmsException = new JMSException(e.getMessage());
jmsException.initCause(e);
throw jmsException;
} catch (ActiveMQException e) {
throw JMSExceptionHelper.convertFromActiveMQException(e);
} catch (java.lang.IllegalStateException e) {
JMSException je = new IllegalStateException(e.getMessage());
je.setStackTrace(e.getStackTrace());
je.initCause(e);
throw je;
}
}
use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.
the class CreateQueueIdempotentTest method testSequentialCreateQueueIdempotency.
@Test
public void testSequentialCreateQueueIdempotency() throws Exception {
final SimpleString QUEUE = new SimpleString("SequentialCreateQueueIdempotency");
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
ClientSession session = sf.createSession(false, true, true);
session.createQueue(QUEUE, QUEUE, null, true);
try {
session.createQueue(QUEUE, QUEUE, null, true);
fail("Expected exception, queue already exists");
} catch (ActiveMQQueueExistsException qee) {
// ok
} catch (ActiveMQException e) {
fail("Invalid Exception type:" + e.getType());
}
}
use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.
the class AcknowledgeTest method testAsyncConsumerAck.
@Test
public void testAsyncConsumerAck() throws Exception {
ActiveMQServer server = createServer(false);
server.start();
ServerLocator locator = createInVMNonHALocator().setBlockOnAcknowledge(true).setAckBatchSize(0);
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession sendSession = cf.createSession(false, true, true);
final ClientSession session = cf.createSession(false, true, true);
sendSession.createQueue(addressA, queueA, false);
ClientProducer cp = sendSession.createProducer(addressA);
ClientConsumer cc = session.createConsumer(queueA);
int numMessages = 100;
for (int i = 0; i < numMessages; i++) {
cp.send(sendSession.createMessage(false));
}
final CountDownLatch latch = new CountDownLatch(numMessages);
session.start();
cc.setMessageHandler(new MessageHandler() {
@Override
public void onMessage(final ClientMessage message) {
try {
message.acknowledge();
} catch (ActiveMQException e) {
try {
session.close();
} catch (ActiveMQException e1) {
e1.printStackTrace();
}
}
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
Assert.assertEquals(0, q.getDeliveringCount());
sendSession.close();
session.close();
}
use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.
the class AcknowledgeTest method testAsyncConsumerAckLastMessageOnly.
@Test
public void testAsyncConsumerAckLastMessageOnly() throws Exception {
ActiveMQServer server = createServer(false);
server.start();
ServerLocator locator = createInVMNonHALocator().setBlockOnAcknowledge(true).setAckBatchSize(0);
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession sendSession = cf.createSession(false, true, true);
final ClientSession session = cf.createSession(false, true, true);
sendSession.createQueue(addressA, queueA, false);
ClientProducer cp = sendSession.createProducer(addressA);
ClientConsumer cc = session.createConsumer(queueA);
int numMessages = 100;
for (int i = 0; i < numMessages; i++) {
cp.send(sendSession.createMessage(false));
}
final CountDownLatch latch = new CountDownLatch(numMessages);
session.start();
cc.setMessageHandler(new MessageHandler() {
@Override
public void onMessage(final ClientMessage message) {
if (latch.getCount() == 1) {
try {
message.acknowledge();
} catch (ActiveMQException e) {
try {
session.close();
} catch (ActiveMQException e1) {
e1.printStackTrace();
}
}
}
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
Assert.assertEquals(0, q.getDeliveringCount());
sendSession.close();
session.close();
}
use of org.apache.activemq.artemis.api.core.ActiveMQException in project activemq-artemis by apache.
the class CreateQueueTest method testUnsupportedRoutingType.
@Test
public void testUnsupportedRoutingType() throws Exception {
ClientSession sendSession = cf.createSession(false, true, true);
server.getAddressSettingsRepository().addMatch(addressA.toString(), new AddressSettings().setAutoCreateAddresses(false));
server.getAddressSettingsRepository().addMatch(addressB.toString(), new AddressSettings().setAutoCreateAddresses(false));
EnumSet<RoutingType> routingTypes = EnumSet.of(RoutingType.ANYCAST);
sendSession.createAddress(addressA, routingTypes, false);
try {
sendSession.createQueue(addressA, RoutingType.MULTICAST, queueA);
fail("Creating a queue here should fail since the queue routing type differs from what is supported on the address.");
} catch (Exception e) {
assertTrue(e instanceof ActiveMQException);
ActiveMQException ae = (ActiveMQException) e;
assertEquals(ActiveMQExceptionType.INTERNAL_ERROR, ae.getType());
}
routingTypes = EnumSet.of(RoutingType.MULTICAST);
sendSession.createAddress(addressB, routingTypes, false);
try {
sendSession.createQueue(addressB, RoutingType.ANYCAST, queueB);
fail("Creating a queue here should fail since the queue routing type differs from what is supported on the address.");
} catch (Exception e) {
assertTrue(e instanceof ActiveMQException);
ActiveMQException ae = (ActiveMQException) e;
assertEquals(ActiveMQExceptionType.INTERNAL_ERROR, ae.getType());
}
sendSession.close();
}
Aggregations