Example usage for javax.jms Session AUTO_ACKNOWLEDGE

List of usage examples for javax.jms Session AUTO_ACKNOWLEDGE

Introduction

In this page you can find the example usage for javax.jms Session AUTO_ACKNOWLEDGE.

Prototype

int AUTO_ACKNOWLEDGE

To view the source code for javax.jms Session AUTO_ACKNOWLEDGE.

Click Source Link

Document

With this acknowledgment mode, the session automatically acknowledges a client's receipt of a message either when the session has successfully returned from a call to receive or when the message listener the session has called to process the message successfully returns.

Usage

From source file:org.apache.james.queue.activemq.ActiveMQMailQueue.java

/**
 * Try to use ActiveMQ StatisticsPlugin to get size and if that fails
 * fallback to {@link JMSMailQueue#getSize()}
 *//*from w w  w . j  a v  a2  s.c  o m*/
@Override
public long getSize() throws MailQueueException {

    Connection connection = null;
    Session session = null;
    MessageConsumer consumer = null;
    MessageProducer producer = null;
    TemporaryQueue replyTo = null;
    long size = -1;

    try {
        connection = connectionFactory.createConnection();
        connection.start();

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        replyTo = session.createTemporaryQueue();
        consumer = session.createConsumer(replyTo);

        Queue myQueue = session.createQueue(queuename);
        producer = session.createProducer(null);

        String queueName = "ActiveMQ.Statistics.Destination." + myQueue.getQueueName();
        Queue query = session.createQueue(queueName);

        Message msg = session.createMessage();
        msg.setJMSReplyTo(replyTo);
        producer.send(query, msg);
        MapMessage reply = (MapMessage) consumer.receive(2000);
        if (reply != null && reply.itemExists("size")) {
            try {
                size = reply.getLong("size");
                return size;
            } catch (NumberFormatException e) {
                // if we hit this we can't calculate the size so just catch
                // it
            }
        }

    } catch (Exception e) {
        throw new MailQueueException("Unable to remove mails", e);

    } finally {

        if (consumer != null) {

            try {
                consumer.close();
            } catch (JMSException e1) {
                e1.printStackTrace();
                // ignore on rollback
            }
        }

        if (producer != null) {

            try {
                producer.close();
            } catch (JMSException e1) {
                // ignore on rollback
            }
        }

        if (replyTo != null) {
            try {

                // we need to delete the temporary queue to be sure we will
                // free up memory if thats not done and a pool is used
                // its possible that we will register a new mbean in jmx for
                // every TemporaryQueue which will never get unregistered
                replyTo.delete();
            } catch (JMSException e) {
            }
        }
        try {
            if (session != null)
                session.close();
        } catch (JMSException e1) {
            // ignore here
        }

        try {
            if (connection != null)
                connection.close();
        } catch (JMSException e1) {
            // ignore here
        }
    }

    // if we came to this point we should just fallback to super method
    return super.getSize();
}

From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java

@Test
public void testTopicMessageSize() throws Exception {
    AtomicLong publishedMessageSize = new AtomicLong();

    Connection connection = cf.createConnection();
    connection.setClientID("clientId");
    connection.start();/*w  w  w. j  a va  2s.c o m*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createTopic(defaultTopicName));

    publishTestTopicMessages(200, publishedMessageSize);

    verifyPendingStats(defaultTopicName, 200, publishedMessageSize.get());
    verifyPendingDurableStats(defaultQueueName, 0, 0);

    // consume all messages
    consumeTestMessages(consumer, 200);

    // All messages should now be gone
    verifyPendingStats(defaultTopicName, 0, 0);
    verifyPendingDurableStats(defaultQueueName, 0, 0);

    connection.close();
}

From source file:org.apache.activemq.store.kahadb.SubscriptionRecoveryTest.java

private void tryConsumeExpectNone(Topic topic) throws Exception {
    Connection connection = cf.createConnection();
    connection.setClientID("Inactive");
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createDurableSubscriber(topic, "Inactive");
    connection.start();// w w  w  .j  a va2s .c  om
    if (consumer.receive(TimeUnit.SECONDS.toMillis(10)) != null) {
        fail("Should be no messages for this durable.");
    }
    consumer.close();
    connection.close();
}

From source file:org.aludratest.service.jms.impl.JmsActionImpl.java

@Override
public void subscribeTopic(MessageListener listener, @TechnicalLocator String destinationName,
        @TechnicalArgument String messageSelector, @TechnicalArgument String subscriptionName,
        @TechnicalArgument boolean durable) throws JMSException {
    if (StringUtils.isEmpty(subscriptionName)) {
        throw new IllegalArgumentException("subscriptionName must be provided to subscribe!");
    }//from  ww w  . j  a v a 2s. c o  m
    Topic topic;
    try {
        topic = (Topic) context.lookup(destinationName);
    } catch (NamingException e) {
        throw new AutomationException("Could not lookup destination " + destinationName, e);
    }

    LOGGER.debug("Creating topic-subscriber for topic " + destinationName + " and subscriptionname "
            + subscriptionName);
    Connection c = getDynamicConnection(subscriptionName);

    TopicSession ts = (TopicSession) c.createSession(false, Session.AUTO_ACKNOWLEDGE);
    if (durable) {
        TopicSubscriber subscriber = ts.createDurableSubscriber(topic, subscriptionName, messageSelector,
                false);
        subscriber.setMessageListener(listener);
        this.durableConsumers.put(subscriptionName, subscriber);
    } else {
        ts.createSubscriber(topic, messageSelector, true).setMessageListener(listener);
    }

}

From source file:org.apache.activemq.store.kahadb.SubscriptionRecoveryTest.java

private int consumeFromInactiveDurableSub(Topic topic) throws Exception {
    Connection connection = cf.createConnection();
    connection.setClientID("Inactive");
    connection.start();// w w  w.j  a va2 s .co m
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createDurableSubscriber(topic, "Inactive");

    int count = 0;

    while (consumer.receive(10000) != null) {
        count++;
    }

    consumer.close();
    connection.close();

    return count;
}

From source file:edu.psu.citeseerx.messaging.MsgService.java

/**
 * Utility for translating String representations of acknowledgement
 * modes into their int encoding.  Default is CLIENT_ACKNOWLEDGE, so
 * any string that is not understood will result in a CLIENT_ACKNOWLEDGE
 * mode./*from   w  w  w .ja  v a 2s  . com*/
 * @param modeStr
 * @return int representation of string acknowledgement
 */
protected int getAckMode(String modeStr) {
    int mode = Session.CLIENT_ACKNOWLEDGE;
    if (modeStr.equalsIgnoreCase("AUTO_ACKNOWLEDGE")) {
        mode = Session.AUTO_ACKNOWLEDGE;
    }
    if (modeStr.equalsIgnoreCase("DUPS_OK_ACKNOWLEDGE")) {
        mode = Session.DUPS_OK_ACKNOWLEDGE;
    }
    return mode;

}

From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java

@Test
public void testTopicMessageSizeShared() throws Exception {
    AtomicLong publishedMessageSize = new AtomicLong();

    Connection connection = cf.createConnection();
    connection.setClientID("clientId");
    connection.start();/*from www .j  ava2  s  . co  m*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createSharedConsumer(session.createTopic(defaultTopicName), "sub1");
    MessageConsumer consumer2 = session.createSharedConsumer(session.createTopic(defaultTopicName), "sub1");

    publishTestTopicMessages(200, publishedMessageSize);

    verifyPendingStats(defaultTopicName, 200, publishedMessageSize.get());
    verifyPendingDurableStats(defaultTopicName, 0, 0);
    consumer2.close();

    // consume all messages
    consumeTestMessages(consumer, 200);

    // All messages should now be gone
    verifyPendingStats(defaultTopicName, 0, 0);
    verifyPendingDurableStats(defaultTopicName, 0, 0);

    connection.close();
}

From source file:org.apache.synapse.transport.jms.JMSConnectionFactory.java

/**
 * Create a session for sending to the given destination and save it on the jmsSessions Map
 * keyed by the destinatin JNDI name/*from www.  j av a2  s  .com*/
 * @param destinationJNDIname the destination JNDI name
 * @return a JMS Session to send messages to the destination using this connection factory
 */
public Session getSessionForDestination(String destinationJNDIname) {

    Session session = (Session) jmsSessions.get(destinationJNDIname);

    if (session == null) {
        try {
            Destination dest = (Destination) getPhysicalDestination(destinationJNDIname);

            if (dest instanceof Topic) {
                session = ((TopicConnection) connection).createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            } else {
                session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            }

            jmsSessions.put(destinationJNDIname, session);

        } catch (JMSException e) {
            handleException("Unable to create a session using connection factory : " + name, e);
        }
    }
    return session;
}

From source file:org.wso2.carbon.event.output.adaptor.jms.JMSEventAdaptorType.java

@Override
public void testConnection(OutputEventAdaptorConfiguration outputEventAdaptorConfiguration, int tenantId) {
    try {/*from w  ww.  j a  v a 2  s .  c  o m*/
        Hashtable<String, String> adaptorProperties = new Hashtable<String, String>();
        adaptorProperties.putAll(outputEventAdaptorConfiguration.getOutputProperties());

        JMSConnectionFactory jmsConnectionFactory = new JMSConnectionFactory(adaptorProperties,
                outputEventAdaptorConfiguration.getName());
        Connection connection = jmsConnectionFactory.getConnection();
        connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        connection.close();
        jmsConnectionFactory.stop();
    } catch (Exception e) {
        throw new OutputEventAdaptorEventProcessingException(e);
    }
}

From source file:org.apache.activemq.store.kahadb.SubscriptionRecoveryTest.java

private void sendMessages(Destination destination, int count) throws Exception {
    Connection connection = cf.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    for (int i = 0; i < count; ++i) {
        TextMessage message = session.createTextMessage("Message #" + i + " for destination: " + destination);
        producer.send(message);//ww w.  ja v a  2s.  c o m
    }
    connection.close();
}