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.ivory.messaging.MessageProducer.java

/**
 * //from  ww  w.j  av a 2  s. c o m
 * @param arguments
 *            - 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
 */
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:" + DEFAULT_TTL + " milli-seconds");
    }
    producer.setTimeToLive(messageTTL);
    producer.send(new EntityInstanceMessageCreator(entityInstanceMessage).createMessage(session));

}

From source file:org.wso2.extension.siddhi.io.jms.sink.util.QueueConsumer.java

public void run() {
    // create queue connection
    QueueConnection queueConnection = null;
    try {//from  w  w  w . j  a v a2  s  . c om
        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) {
                resultContainer.eventReceived(message);
                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:nl.nn.adapterframework.extensions.tibco.TibcoMessagingSource.java

public Destination lookupDestination(String destinationName) throws JmsException {
    Session session = null;//  w w w .  j ava 2  s.co  m
    try {
        session = createSession(false, Session.AUTO_ACKNOWLEDGE);
        log.debug("Session class [" + session.getClass().getName() + "]");
        Destination destination;

        /* create the destination */
        if (session instanceof TopicSession) {
            destination = ((TopicSession) session).createTopic(destinationName);
        } else {
            destination = ((QueueSession) session).createQueue(destinationName);
        }

        return destination;
    } catch (Exception e) {
        throw new JmsException("cannot create destination", e);
    } finally {
        releaseSession(session);
    }
}

From source file:net.timewalker.ffmq4.common.session.AbstractMessageConsumer.java

/**
 * Constructor// w  ww . ja v  a 2 s .c om
 */
public AbstractMessageConsumer(AbstractSession session, Destination destination, String messageSelector,
        boolean noLocal, IntegerID consumerId) throws JMSException {
    super(session, destination, consumerId);
    this.messageSelector = messageSelector;
    this.noLocal = noLocal;
    this.autoAcknowledge = (session.getAcknowledgeMode() == Session.AUTO_ACKNOWLEDGE
            || session.getAcknowledgeMode() == Session.DUPS_OK_ACKNOWLEDGE);

    if (destination == null)
        throw new FFMQException("Message consumer destination cannot be null", "INVALID_DESTINATION");
}

From source file:org.wso2.extension.siddhi.io.jms.sink.util.TopicConsumer.java

public void run() {
    // create topic connection
    TopicConnection topicConnection = null;
    try {// w ww. j av a2s  . com
        topicConnection = topicConnectionFactory.createTopicConnection();
        topicConnection.start();
    } catch (JMSException e) {
        log.error("Can not create topic connection." + e.getMessage(), e);
        return;
    }
    Session session = null;
    try {
        session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic(topicName);
        MessageConsumer consumer = session.createConsumer(destination);
        log.info("Listening for messages");
        while (active) {
            Message message = consumer.receive(1000);
            if (message != null) {
                resultContainer.eventReceived(message);
                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();
        topicConnection.stop();
        topicConnection.close();
    } catch (JMSException e) {
        log.error("Can not subscribe." + e.getMessage(), e);
    }
}

From source file:io.fabric8.msg.gateway.TestGateway.java

/**
 * TODO - lets figure out a way of mocking kubernetes, running a ZK ensemble, an Artemis broker and testing it!
 * @throws Exception//  w  w w.j  a v  a  2  s . co  m
 */
@Ignore
public void simpleTest() throws Exception {
    int numberOfMessages = 10;
    String destinationName = "jms.queue.test.foo";
    String brokerURL = "tcp://0.0.0.0:61616";
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerURL);

    Connection consumerConnection = factory.createConnection();
    consumerConnection.start();
    Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination consumerDestination = consumerSession.createQueue(destinationName);
    MessageConsumer messageConsumer = consumerSession.createConsumer(consumerDestination);

    Connection producerConnection = factory.createConnection();
    producerConnection.start();
    Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = producerSession.createQueue(destinationName);
    MessageProducer producer = producerSession.createProducer(destination);

    for (int i = 0; i < numberOfMessages; i++) {
        Message message = producerSession.createTextMessage("test message: " + i);
        producer.send(message);
        //System.err.println("Sent message " + message);

    }

    Message message;

    for (int i = 0; i < numberOfMessages; i++) {
        message = messageConsumer.receive(5000);
        Assert.assertNotNull(message);
        //System.err.println("Got Message " + message);

    }
    messageConsumer.close();

}

From source file:com.tremolosecurity.provisioning.util.PooledMessageProducerFactory.java

@Override
public PooledObject<MessageProducerHolder> makeObject() throws Exception {
    Connection con = prov.getQueueConnection();
    Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue q = session.createQueue(taskQueueName);
    MessageProducer mp = session.createProducer(q);
    return new DefaultPooledObject(new MessageProducerHolder(con, mp, session));
}

From source file:de.adorsys.jmspojo.JMSMessageListenerServiceAdapterTest.java

@Before
public void setup() throws Exception {
    broker = new BrokerService();
    broker.setPersistent(false);/*  w  w  w.  j a  v a  2  s .c o m*/

    // configure the broker
    broker.addConnector("vm://test");
    broker.setBrokerName("test");
    broker.setUseShutdownHook(false);

    broker.start();

    cf = new ActiveMQConnectionFactory("vm://localhost?create=false");
    qc = cf.createQueueConnection();
    queueSession = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    reqlayQ = queueSession.createTemporaryQueue();
    qc.start();
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueSendReceiveTest.java

@Test
public void testReceive() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    connection.start();/*from  w  ww.j a v a 2s .  com*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(activeMQTestQueueA);
    Message rawMessage = consumer.receive();
    assertTrue(rawMessage instanceof TextMessage);

    TextMessage message = (TextMessage) rawMessage;
    assertEquals("test text", message.getText());
    connection.close();
}

From source file:org.dawnsci.commandserver.core.consumer.RemoteSubmission.java

/**
 * Submits the bean onto the server. From there events about this
 * bean are tacked by monitoring the status queue.
 * //from   w w  w . j av a 2s. c  o  m
 * @param uri
 * @param bean
 */
public synchronized TextMessage submit(StatusBean bean, boolean prepareBean) throws Exception {

    if (getQueueName() == null || "".equals(getQueueName()))
        throw new Exception("Please specify a queue name!");

    Connection send = null;
    Session session = null;
    MessageProducer producer = null;

    try {
        QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri);
        send = connectionFactory.createConnection();

        session = send.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(queueName);

        producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        ObjectMapper mapper = new ObjectMapper();

        if (getTimestamp() < 1)
            setTimestamp(System.currentTimeMillis());
        if (getPriority() < 1)
            setPriority(1);
        if (getLifeTime() < 1)
            setLifeTime(7 * 24 * 60 * 60 * 1000); // 7 days in ms

        if (prepareBean) {
            if (bean.getUserName() == null)
                bean.setUserName(System.getProperty("user.name"));
            bean.setUniqueId(uniqueId);
            bean.setSubmissionTime(getTimestamp());
        }
        String jsonString = mapper.writeValueAsString(bean);

        TextMessage message = session.createTextMessage(jsonString);

        message.setJMSMessageID(uniqueId);
        message.setJMSExpiration(getLifeTime());
        message.setJMSTimestamp(getTimestamp());
        message.setJMSPriority(getPriority());

        producer.send(message);

        return message;

    } finally {
        if (send != null)
            send.close();
        if (session != null)
            session.close();
        if (producer != null)
            producer.close();
    }

}