use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project candlepin by candlepin.
the class EventSource method registerListener.
void registerListener(EventListener listener) {
String queueName = QUEUE_ADDRESS + "." + listener.getClass().getCanonicalName();
log.debug("registering listener for {}", queueName);
try {
try {
// Create a durable queue that will be persisted to disk:
session.createQueue(QUEUE_ADDRESS, queueName, true);
log.debug("created new event queue " + queueName);
} catch (ActiveMQException e) {
// so that's fine.
if (e.getType() != ActiveMQExceptionType.QUEUE_EXISTS) {
throw e;
}
}
ClientConsumer consumer = session.createConsumer(queueName);
consumer.setMessageHandler(new ListenerWrapper(listener, mapper));
} catch (ActiveMQException e) {
log.error("Unable to register listener :" + listener, e);
}
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class RawAckTest method testAck.
@Test
public void testAck() throws Exception {
ClientMessage message;
message = session.createMessage(Message.OBJECT_TYPE, false);
message.getBodyBuffer().writeInt("hello".getBytes().length);
message.getBodyBuffer().writeBytes("hello".getBytes());
producer.send(message);
Thread.sleep(100);
ClientSession sessionConsumer = sessionFactory.createSession(true, true);
ClientConsumer consumer = sessionConsumer.createConsumer("testQueue");
sessionConsumer.start();
MyThread t = new MyThread(consumer);
t.start();
t.join();
Assert.assertTrue(passed);
passed = false;
message = session.createMessage(false);
message.getBodyBuffer().writeInt("hello2".getBytes().length);
message.getBodyBuffer().writeBytes("hello2".getBytes());
producer.send(message);
Thread.sleep(100);
t = new MyThread(consumer);
t.start();
t.join();
Assert.assertTrue(passed);
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class PushConsumer method start.
public void start() throws Exception {
if (registration.getTarget().getClassName() != null) {
Class clazz = Thread.currentThread().getContextClassLoader().loadClass(registration.getTarget().getClassName());
strategy = (PushStrategy) clazz.newInstance();
} else if (registration.getTarget().getRelationship() != null) {
if (registration.getTarget().getRelationship().equals("destination")) {
strategy = new ActiveMQPushStrategy();
} else if (registration.getTarget().getRelationship().equals("template")) {
strategy = new UriTemplateStrategy();
}
}
if (strategy == null) {
strategy = new UriStrategy();
}
strategy.setRegistration(registration);
strategy.setJmsOptions(jmsOptions);
strategy.start();
sessions = new ArrayList<>();
consumers = new ArrayList<>();
for (int i = 0; i < registration.getSessionCount(); i++) {
ClientSession session = factory.createSession(false, false, 0);
ClientConsumer consumer;
if (registration.getSelector() != null) {
consumer = session.createConsumer(destination, SelectorTranslator.convertToActiveMQFilterString(registration.getSelector()));
} else {
consumer = session.createConsumer(destination);
}
consumer.setMessageHandler(new PushConsumerMessageHandler(this, session));
session.start();
ActiveMQRestLogger.LOGGER.startingPushConsumer(registration.getTarget());
consumers.add(consumer);
sessions.add(session);
}
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class AcknowledgedQueueConsumer method unacknowledge.
protected void unacknowledge() {
// we close current session so that message is redelivered
// for temporary queues/topics, create a new session before closing old so we don't lose the temporary topic/queue
ClientConsumer old = consumer;
ClientSession oldSession = session;
try {
createSession();
} catch (Exception e) {
shutdown();
throw new RuntimeException(e);
} finally {
try {
old.close();
} catch (ActiveMQException e) {
}
try {
oldSession.close();
} catch (ActiveMQException e) {
}
}
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ActiveMQSession method internalCreateSharedConsumer.
/**
* This is an internal method for shared consumers
*/
private ActiveMQMessageConsumer internalCreateSharedConsumer(final ActiveMQDestination dest, final String subscriptionName, String selectorString, ConsumerDurability durability) throws JMSException {
try {
if (dest.isQueue()) {
// createSharedConsumer only accepts Topics by declaration
throw new RuntimeException("Internal error: createSharedConsumer is only meant for Topics");
}
if (subscriptionName == null) {
throw ActiveMQJMSClientBundle.BUNDLE.invalidSubscriptionName();
}
selectorString = "".equals(selectorString) ? null : selectorString;
SimpleString coreFilterString = null;
if (selectorString != null) {
coreFilterString = new SimpleString(SelectorTranslator.convertToActiveMQFilterString(selectorString));
}
ClientConsumer consumer;
SimpleString autoDeleteQueueName = null;
AddressQuery response = session.addressQuery(dest.getSimpleAddress());
if (!response.isExists() && !response.isAutoCreateAddresses()) {
throw ActiveMQJMSClientBundle.BUNDLE.destinationDoesNotExist(dest.getSimpleAddress());
}
SimpleString queueName;
if (dest.isTemporary() && durability == ConsumerDurability.DURABLE) {
throw new InvalidDestinationException("Cannot create a durable subscription on a temporary topic");
}
queueName = ActiveMQDestination.createQueueNameForSubscription(durability == ConsumerDurability.DURABLE, connection.getClientID(), subscriptionName);
try {
if (durability == ConsumerDurability.DURABLE) {
createSharedQueue(dest, RoutingType.MULTICAST, queueName, coreFilterString, true, response.getDefaultMaxConsumers(), response.isDefaultPurgeOnNoConsumers(), response.isDefaultExclusive(), response.isDefaultLastValueQueue());
} else {
createSharedQueue(dest, RoutingType.MULTICAST, queueName, coreFilterString, false, response.getDefaultMaxConsumers(), response.isDefaultPurgeOnNoConsumers(), response.isDefaultExclusive(), response.isDefaultLastValueQueue());
}
} catch (ActiveMQQueueExistsException ignored) {
// We ignore this because querying and then creating the queue wouldn't be idempotent
// we could also add a parameter to ignore existence what would require a bigger work around to avoid
// compatibility.
}
consumer = session.createConsumer(queueName, null, false);
ActiveMQMessageConsumer jbc = new ActiveMQMessageConsumer(options, connection, this, consumer, false, dest, selectorString, autoDeleteQueueName);
consumers.add(jbc);
return jbc;
} catch (ActiveMQException e) {
throw JMSExceptionHelper.convertFromActiveMQException(e);
}
}
Aggregations