Search in sources :

Example 1 with ClientConsumer

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);
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer)

Example 2 with ClientConsumer

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);
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Test(org.junit.Test)

Example 3 with ClientConsumer

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);
    }
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer)

Example 4 with ClientConsumer

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) {
        }
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

Example 5 with ClientConsumer

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);
    }
}
Also used : AddressQuery(org.apache.activemq.artemis.api.core.client.ClientSession.AddressQuery) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InvalidDestinationException(javax.jms.InvalidDestinationException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer)

Aggregations

ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)720 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)642 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)624 Test (org.junit.Test)584 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)569 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)409 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)348 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)154 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)132 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)88 Configuration (org.apache.activemq.artemis.core.config.Configuration)81 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)79 CountDownLatch (java.util.concurrent.CountDownLatch)72 Queue (org.apache.activemq.artemis.core.server.Queue)63 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)62 Xid (javax.transaction.xa.Xid)55 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)52 HashMap (java.util.HashMap)48 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)46 ArrayList (java.util.ArrayList)44