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.activemq.apollo.JmsQueueBrowserTest.java

/**
 * Tests the queue browser. Browses the messages then the consumer tries to receive them. The messages should still
 * be in the queue even when it was browsed.
 *
 * Re-enable once https://issues.apache.org/jira/browse/APLO-226 is fixed.
 *
 * @throws Exception/*from  ww w .j  a v  a 2 s .com*/
 */
public void testReceiveBrowseReceive() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");
    MessageProducer producer = session.createProducer(destination);
    MessageConsumer consumer = session.createConsumer(destination);
    connection.start();

    Message[] outbound = new Message[] { session.createTextMessage("First Message"),
            session.createTextMessage("Second Message"), session.createTextMessage("Third Message") };

    // lets consume any outstanding messages from previous test runs
    while (consumer.receive(1000) != null) {
    }

    producer.send(outbound[0]);
    producer.send(outbound[1]);
    producer.send(outbound[2]);

    // Get the first.
    assertEquals(outbound[0], consumer.receive(1000));
    consumer.close();
    //Thread.sleep(200);

    QueueBrowser browser = session.createBrowser((Queue) destination);
    Enumeration enumeration = browser.getEnumeration();

    // browse the second
    assertTrue("should have received the second message", enumeration.hasMoreElements());
    assertEquals(outbound[1], (Message) enumeration.nextElement());

    // browse the third.
    assertTrue("Should have received the third message", enumeration.hasMoreElements());
    assertEquals(outbound[2], (Message) enumeration.nextElement());

    // There should be no more.
    boolean tooMany = false;
    while (enumeration.hasMoreElements()) {
        LOG.info("Got extra message: " + ((TextMessage) enumeration.nextElement()).getText());
        tooMany = true;
    }
    assertFalse(tooMany);
    browser.close();

    // Re-open the consumer.
    consumer = session.createConsumer(destination);
    // Receive the second.
    assertEquals(outbound[1], consumer.receive(1000));
    // Receive the third.
    assertEquals(outbound[2], consumer.receive(1000));
    consumer.close();

}

From source file:org.apache.falcon.messaging.MessageProducer.java

/**
 * @param entityInstanceMessage - Accepts a Message to be send to JMS topic, creates a new
 *                  Topic based on topic name if it does not exist or else
 *                  existing topic with the same name is used to send the message.
 * @throws JMSException/* w w w  .j av a2s  .  c o m*/
 */
protected void sendMessage(EntityInstanceMessage entityInstanceMessage) throws JMSException {

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic entityTopic = session.createTopic(entityInstanceMessage.getTopicName());
    javax.jms.MessageProducer producer = session.createProducer(entityTopic);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    long messageTTL = DEFAULT_TTL;
    try {
        long messageTTLinMins = Long.parseLong(entityInstanceMessage.getBrokerTTL());
        messageTTL = messageTTLinMins * 60 * 1000;
    } catch (NumberFormatException e) {
        LOG.error("Error in parsing broker.ttl, setting TTL to: {} milli-seconds", DEFAULT_TTL);
    }
    producer.setTimeToLive(messageTTL);
    producer.send(new EntityInstanceMessageCreator(entityInstanceMessage).createMessage(session));
}

From source file:org.apache.servicemix.samples.bridge.BridgeTest.java

public void testSimpleCall() throws Exception {

    ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Destination outQueue = new ActiveMQQueue("bridge.output");
    Connection connection = factory.createConnection();
    connection.start();/*  w ww. j  av  a2s.c  om*/
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer createConsumer = session.createConsumer(outQueue);
    createConsumer.setMessageListener(new MessageListener() {

        public void onMessage(Message arg0) {
            BridgeTest.recieved = true;
            ActiveMQTextMessage msg = (ActiveMQTextMessage) arg0;
            assertNotNull(msg);
        }

    });

    HttpClient client = new HttpClient();

    PostMethod post = new PostMethod(url);
    post.setRequestBody(new FileInputStream("src/test/resources/request.xml"));

    int executeMethod = client.executeMethod(post);

    assertEquals(202, executeMethod);

    int maxTry = 100;
    while (!recieved || maxTry == 0) {
        Thread.sleep(200);
        maxTry--;
    }

    session.close();
    connection.close();

}

From source file:com.adaptris.core.jms.activemq.JmsReplyToDestinationTest.java

private Queue createTempQueue(EmbeddedActiveMq broker) throws Exception {
    ActiveMQConnection conn = broker.createConnection();
    ActiveMQSession session = (ActiveMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    return session.createTemporaryQueue();
}

From source file:org.symplify.runtime.jms.JMSContextImpl.java

protected void initialize() {
    try {//from  w ww  . jav a2 s  . c  o m
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(m_user, m_password, m_url);
        m_connection = connectionFactory.createConnection();
        m_connection.setExceptionListener(this);
        m_connection.start();

        m_session = m_connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.ahp.core.messaging.AhpJmsProducer.java

public void sendTextMessage(String pTextMessage, AhpJmsDestinationTypes pAhpJmsDestinationTypes,
        AhpJmsDestinationNames pAhpJmsDestinationNames) {
    Connection lConnection = null;
    Session lSession = null;//  w ww .  j a v a 2s  . c om
    try {
        lConnection = this.mJmsConnectionFactory.createConnection();
        lSession = lConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        Destination lDestination = null;
        if (pAhpJmsDestinationTypes.equals(AhpJmsDestinationTypes.Queue))
            lDestination = lSession.createQueue(pAhpJmsDestinationNames.toString());
        else
            lDestination = lSession.createTopic(pAhpJmsDestinationNames.toString());
        MessageProducer lMessageProducer = lSession.createProducer(lDestination);
        lMessageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        //lMessageProducer.setTimeToLive( timeToLive );
        Message lTextMessage = lSession.createTextMessage(pTextMessage);
        LOGGER.debug("AhpJmsProducer sending message to Destination " + pAhpJmsDestinationNames.toString()
                + " TextMessage :: \n" + pTextMessage);
        lMessageProducer.send(lTextMessage);
        lSession.commit();
    } catch (JMSException exJms) {
        LOGGER.error("", exJms);
    } finally {
        try {
            lConnection.close();
        } catch (JMSException exJms) {
            LOGGER.error("", exJms);
        }
    }
}

From source file:org.wso2.carbon.integration.test.client.JMSPublisherClient.java

/**
 * This method will publish the data in the test data file to the given topic via ActiveMQ message broker
 *
 * @param topicName             the topic which the messages should be published under
 * @param format                format of the test data file (csv or text)
 * @param testCaseFolderName    Testcase folder name which is in the test artifacts folder
 * @param dataFileName          data file name with the extension to be read
 *
 *//*from   w w  w. j a v  a2  s  .  c o  m*/
public static void publish(String topicName, String format, String testCaseFolderName, String dataFileName) {

    if (format == null || "map".equals(format)) {
        format = "csv";
    }

    try {
        Properties properties = new Properties();

        String filePath = getTestDataFileLocation(testCaseFolderName, dataFileName);
        properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties"));
        Context context = new InitialContext(properties);
        TopicConnectionFactory connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
        TopicConnection topicConnection = connFactory.createTopicConnection();
        topicConnection.start();
        Session session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(topicName);
        MessageProducer producer = session.createProducer(topic);

        List<String> messagesList = readFile(filePath);
        try {

            if (format.equalsIgnoreCase("csv")) {
                log.info("Sending Map messages on '" + topicName + "' topic");
                publishMapMessages(producer, session, messagesList);

            } else {
                log.info("Sending  " + format + " messages on '" + topicName + "' topic");
                publishTextMessage(producer, session, messagesList);
            }
        } catch (JMSException e) {
            log.error("Can not subscribe." + e.getMessage(), e);
        } finally {
            producer.close();
            session.close();
            topicConnection.stop();
            topicConnection.close();
        }
    } catch (Exception e) {
        log.error("Error when publishing messages" + e.getMessage(), e);
    }

    log.info("All Order Messages sent");
}

From source file:TopicListener.java

public void run() throws JMSException {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
    connection = factory.createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    topic = session.createTopic("topictest.messages");
    control = session.createTopic("topictest.control");

    MessageConsumer consumer = session.createConsumer(topic);
    consumer.setMessageListener(this);

    connection.start();//ww  w  .ja  v a2 s. c  o  m

    producer = session.createProducer(control);
    System.out.println("Waiting for messages...");
}

From source file:org.wso2.siddhi.extension.output.transport.jms.util.QueueConsumer.java

public void run() {
    // create queue connection
    QueueConnection queueConnection = null;
    try {/*  w ww.ja  v  a2  s.  c o  m*/
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueConnection.start();
    } catch (JMSException e) {
        log.error("Can not create queue connection." + e.getMessage(), e);
        return;
    }
    Session session;
    try {
        session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(queueName);
        MessageConsumer consumer = session.createConsumer(destination);
        log.info("Listening for messages at " + queueName);
        while (active) {
            Message message = consumer.receive(1000);
            if (message != null) {
                if (message instanceof MapMessage) {
                    MapMessage mapMessage = (MapMessage) message;
                    Map<String, Object> map = new HashMap<String, Object>();
                    Enumeration enumeration = mapMessage.getMapNames();
                    while (enumeration.hasMoreElements()) {
                        String key = (String) enumeration.nextElement();
                        map.put(key, mapMessage.getObject(key));
                    }
                    log.info("Received Map Message: " + map);
                } else if (message instanceof TextMessage) {
                    log.info("Received Text Message: " + ((TextMessage) message).getText());
                } else {
                    log.info("Received message: " + message.toString());
                }
            }
        }
        log.info("Finished listening for messages.");
        session.close();
        queueConnection.stop();
        queueConnection.close();
    } catch (JMSException e) {
        log.error("Can not subscribe." + e.getMessage(), e);
    }
}

From source file:org.apache.camel.component.sjms.it.ConnectionResourceIT.java

/**
 * Test method for/*from w  w  w  . ja va 2 s.  c  o  m*/
 * {@link org.apache.commons.pool.ObjectPool#returnObject(java.lang.Object)}
 * .
 * 
 * @throws Exception
 */
@Test
public void testCreateConnections() throws Exception {
    ConnectionResource pool = new AMQConnectionResource("tcp://localhost:33333", 1);
    assertNotNull(pool);
    Connection connection = pool.borrowConnection();
    assertNotNull(connection);
    assertNotNull(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
    pool.returnConnection(connection);
    Connection connection2 = pool.borrowConnection();
    assertNotNull(connection2);
}