Example usage for javax.jms Session CLIENT_ACKNOWLEDGE

List of usage examples for javax.jms Session CLIENT_ACKNOWLEDGE

Introduction

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

Prototype

int CLIENT_ACKNOWLEDGE

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

Click Source Link

Document

With this acknowledgment mode, the client acknowledges a consumed message by calling the message's acknowledge method.

Usage

From source file:com.zotoh.maedr.device.JmsIO.java

private void inizFac(Context ctx, Object obj) throws Exception {

    ConnectionFactory f = (ConnectionFactory) obj;
    final JmsIO me = this;
    Connection conn;//  ww w . java  2 s.  com
    Object c = ctx.lookup(_dest);

    if (!isEmpty(_jmsUser)) {
        conn = f.createConnection(_jmsUser, _jmsPwd);
    } else {
        conn = f.createConnection();
    }

    _conn = conn;

    if (c instanceof Destination) {
        //TODO ? ack always ?
        Session s = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        MessageConsumer u = s.createConsumer((Destination) c);
        u.setMessageListener(new MessageListener() {
            public void onMessage(Message msg) {
                me.onMessage(msg);
            }
        });
    } else {
        throw new Exception("JmsIO: Object not of Destination type");
    }
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.factory.JMSConnectionFactory.java

public JMSConnectionFactory(Properties properties) {
    try {/*  w w w  . ja v a  2 s  .  c  o m*/
        ctx = new InitialContext(properties);
    } catch (NamingException e) {
        logger.error("NamingException while obtaining initial context. " + e.getMessage(), e);
    }

    String connectionFactoryType = properties.getProperty(JMSConstants.CONNECTION_FACTORY_TYPE);
    if ("topic".equals(connectionFactoryType)) {
        this.destinationType = JMSConstants.JMSDestinationType.TOPIC;
    } else {
        this.destinationType = JMSConstants.JMSDestinationType.QUEUE;
    }

    if (properties.getProperty(JMSConstants.PARAM_JMS_SPEC_VER) == null || JMSConstants.JMS_SPEC_VERSION_1_1
            .equals(properties.getProperty(JMSConstants.PARAM_JMS_SPEC_VER))) {
        jmsSpec = JMSConstants.JMS_SPEC_VERSION_1_1;
    } else if (JMSConstants.JMS_SPEC_VERSION_2_0
            .equals(properties.getProperty(JMSConstants.PARAM_JMS_SPEC_VER))) {
        jmsSpec = JMSConstants.JMS_SPEC_VERSION_2_0;
    } else {
        jmsSpec = JMSConstants.JMS_SPEC_VERSION_1_0;
    }

    if ("true".equalsIgnoreCase(properties.getProperty(JMSConstants.PARAM_IS_SHARED_SUBSCRIPTION))) {
        isSharedSubscription = true;
    } else {
        isSharedSubscription = false;
    }

    noPubSubLocal = Boolean.valueOf(properties.getProperty(JMSConstants.PARAM_PUBSUB_NO_LOCAL));

    clientId = properties.getProperty(JMSConstants.PARAM_DURABLE_SUB_CLIENT_ID);
    subscriptionName = properties.getProperty(JMSConstants.PARAM_DURABLE_SUB_NAME);

    if (isSharedSubscription) {
        if (subscriptionName == null) {
            logger.info("Subscription name is not given. Therefor declaring a non-shared subscription");
            isSharedSubscription = false;
        }
    }

    String subDurable = properties.getProperty(JMSConstants.PARAM_SUB_DURABLE);
    if (subDurable != null) {
        isDurable = Boolean.parseBoolean(subDurable);
    }
    String msgSelector = properties.getProperty(JMSConstants.PARAM_MSG_SELECTOR);
    if (msgSelector != null) {
        messageSelector = msgSelector;
    }
    this.connectionFactoryString = properties.getProperty(JMSConstants.CONNECTION_FACTORY_JNDI_NAME);
    if (connectionFactoryString == null || "".equals(connectionFactoryString)) {
        connectionFactoryString = "QueueConnectionFactory";
    }

    this.destinationName = properties.getProperty(JMSConstants.DESTINATION_NAME);
    if (destinationName == null || "".equals(destinationName)) {
        destinationName = "QUEUE_" + System.currentTimeMillis();
    }

    String strTransactedSession = properties.getProperty(JMSConstants.SESSION_TRANSACTED);
    if (strTransactedSession == null || "".equals(strTransactedSession)
            || !strTransactedSession.equals("true")) {
        transactedSession = false;
    } else if ("true".equals(strTransactedSession)) {
        transactedSession = true;
    }

    String strSessionAck = properties.getProperty(JMSConstants.SESSION_ACK);
    if (null == strSessionAck) {
        sessionAckMode = 1;
    } else if (strSessionAck.equals("AUTO_ACKNOWLEDGE")) {
        sessionAckMode = Session.AUTO_ACKNOWLEDGE;
    } else if (strSessionAck.equals("CLIENT_ACKNOWLEDGE")) {
        sessionAckMode = Session.CLIENT_ACKNOWLEDGE;
    } else if (strSessionAck.equals("DUPS_OK_ACKNOWLEDGE")) {
        sessionAckMode = Session.DUPS_OK_ACKNOWLEDGE;
    } else if (strSessionAck.equals("SESSION_TRANSACTED")) {
        sessionAckMode = Session.SESSION_TRANSACTED;
    } else {
        sessionAckMode = 1;
    }

    createConnectionFactory();
}

From source file:com.adaptris.core.jms.MetadataCorrelationIdSourceTest.java

public void testJmsCorrelationIdToAdaptrisMessageMetadata_NoValue() throws Exception {
    EmbeddedActiveMq broker = new EmbeddedActiveMq();
    JmsConnection conn = broker.getJmsConnection();
    try {/*from w  w  w . j a  v  a 2  s .  com*/
        broker.start();
        start(conn);
        Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        AdaptrisMessage adpMsg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
        TextMessage jmsMsg = session.createTextMessage();
        MetadataCorrelationIdSource mcs = new MetadataCorrelationIdSource(CORRELATIONID_KEY);
        mcs.processCorrelationId(jmsMsg, adpMsg);
        assertFalse(adpMsg.containsKey(CORRELATIONID_KEY));
        session.close();
    } finally {
        stop(conn);
        broker.destroy();
    }
}

From source file:com.adaptris.core.jms.MessageTypeTranslatorCase.java

public void testMoveMetadataJmsMessageToAdaptrisMessage() throws Exception {
    EmbeddedActiveMq broker = new EmbeddedActiveMq();
    MessageTypeTranslatorImp trans = createTranslator();
    try {//from   w  w  w  . j a  v  a  2s . co m
        broker.start();
        Session session = broker.createConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE);
        Message jmsMsg = createMessage(session);
        addProperties(jmsMsg);
        start(trans, session);
        AdaptrisMessage msg = trans.translate(jmsMsg);
        assertMetadata(msg);
    } finally {
        stop(trans);
        broker.destroy();
    }

}

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

public void testBug895() throws Exception {
    MessageTypeTranslatorImp trans = new BlobMessageTranslator();
    EmbeddedActiveMq activeMqBroker = new EmbeddedActiveMq();
    JmsConnection conn = null;//from  ww  w  .  j av  a 2s  .co m
    try {
        activeMqBroker.start();
        conn = activeMqBroker.getJmsConnection(new BasicActiveMqImplementation());
        start(conn);

        AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage();

        msg.addMetadata(JmsConstants.JMS_PRIORITY, "9");
        msg.addMetadata(JmsConstants.JMS_TYPE, "idaho");
        Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        trans.setMoveJmsHeaders(true);
        trans.registerSession(session);
        trans.registerMessageFactory(new DefaultMessageFactory());
        start(trans);

        Message jmsMsg = trans.translate(msg);

        assertNotSame("JMS Priorities should be different", jmsMsg.getJMSPriority(), 9);
        assertEquals("JMSType should be equal", "idaho", jmsMsg.getJMSType());
    } finally {
        stop(trans);
        stop(conn);
        activeMqBroker.destroy();
    }

}

From source file:com.zotoh.maedr.device.JmsIO.java

private void inizTopic(Context ctx, Object obj) throws Exception {

    TopicConnectionFactory f = (TopicConnectionFactory) obj;
    final JmsIO me = this;
    TopicConnection conn;//from   www  .j a  v a 2s.  co m
    Topic t = (Topic) ctx.lookup(_dest);

    if (!isEmpty(_jmsUser)) {
        conn = f.createTopicConnection(_jmsUser, _jmsPwd);
    } else {
        conn = f.createTopicConnection();
    }

    _conn = conn;

    TopicSession s = conn.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
    TopicSubscriber b;

    if (_durable) {
        b = s.createDurableSubscriber(t, GUID.generate());
    } else {
        b = s.createSubscriber(t);
    }

    b.setMessageListener(new MessageListener() {
        public void onMessage(Message msg) {
            me.onMessage(msg);
        }
    });
}

From source file:com.adaptris.core.jms.MessageTypeTranslatorCase.java

public void testMoveMetadataJmsMessageToAdaptrisMessage_RemoveAllFilter() throws Exception {
    EmbeddedActiveMq broker = new EmbeddedActiveMq();
    MessageTypeTranslatorImp trans = createTranslator();
    trans.setMetadataFilter(new RemoveAllMetadataFilter());
    try {/*from  w ww  .ja  v  a 2 s .  c  o  m*/
        broker.start();
        Session session = broker.createConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE);
        Message jmsMsg = createMessage(session);
        addProperties(jmsMsg);
        start(trans, session);
        AdaptrisMessage msg = trans.translate(jmsMsg);
        assertFalse(msg.containsKey(INTEGER_METADATA));
        assertFalse(msg.containsKey(STRING_METADATA));
        assertFalse(msg.containsKey(BOOLEAN_METADATA));
    } finally {
        stop(trans);
        broker.destroy();
    }
}

From source file:ConsumerTool.java

public void onMessage(Message message) {

    messagesReceived++;//  w  w w. ja  va2  s .  com

    try {

        if (message instanceof TextMessage) {
            TextMessage txtMsg = (TextMessage) message;
            if (verbose) {

                String msg = txtMsg.getText();
                int length = msg.length();
                if (length > 50) {
                    msg = msg.substring(0, 50) + "...";
                }
                System.out.println("[" + this.getName() + "] Received: '" + msg + "' (length " + length + ")");
            }
        } else {
            if (verbose) {
                System.out.println("[" + this.getName() + "] Received: '" + message + "'");
            }
        }

        if (message.getJMSReplyTo() != null) {
            replyProducer.send(message.getJMSReplyTo(),
                    session.createTextMessage("Reply: " + message.getJMSMessageID()));
        }

        if (transacted) {
            if ((messagesReceived % batch) == 0) {
                System.out.println("Commiting transaction for last " + batch + " messages; messages so far = "
                        + messagesReceived);
                session.commit();
            }
        } else if (ackMode == Session.CLIENT_ACKNOWLEDGE) {
            if ((messagesReceived % batch) == 0) {
                System.out.println(
                        "Acknowledging last " + batch + " messages; messages so far = " + messagesReceived);
                message.acknowledge();
            }
        }

    } catch (JMSException e) {
        System.out.println("[" + this.getName() + "] Caught: " + e);
        e.printStackTrace();
    } finally {
        if (sleepTime > 0) {
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
            }
        }
    }
}

From source file:com.zotoh.maedr.device.JmsIO.java

private void inizQueue(Context ctx, Object obj) throws Exception {

    QueueConnectionFactory f = (QueueConnectionFactory) obj;
    final JmsIO me = this;
    QueueConnection conn;/*from  w w w. j a v a 2s.c om*/
    Queue q = (Queue) ctx.lookup(_dest);

    if (!isEmpty(_jmsUser)) {
        conn = f.createQueueConnection(_jmsUser, _jmsPwd);
    } else {
        conn = f.createQueueConnection();
    }

    _conn = conn;

    QueueSession s = conn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    QueueReceiver r;

    r = s.createReceiver(q);
    r.setMessageListener(new MessageListener() {
        public void onMessage(Message msg) {
            me.onMessage(msg);
        }
    });
}

From source file:org.apache.synapse.message.store.impl.jms.JmsStore.java

public MessageConsumer getConsumer() {
    JmsConsumer consumer = new JmsConsumer(this);
    consumer.setId(nextConsumerId());/*from  w w w  .  j av a  2 s  . c  o m*/
    Connection connection = null;
    try {
        // Had to add a condition to avoid piling up log files with the error message and throttle retries.
        // need to improve this to allow the client to configure it.
        if ((System.currentTimeMillis() - retryTime) >= 3000) {
            connection = newConnection();
            retryTime = -1;
        }
    } catch (JMSException e) {

        retryTime = System.currentTimeMillis();

        if (logger.isDebugEnabled()) {
            logger.error("Could not create a Message Consumer for " + nameString()
                    + ". Could not create connection.");
        }
        return consumer;
    }
    if (connection == null) {
        return consumer;
    }
    Session session;
    try {
        session = newSession(connection, Session.CLIENT_ACKNOWLEDGE, false);
    } catch (JMSException e) {
        if (logger.isDebugEnabled()) {
            logger.error(
                    "Could not create a Message Consumer for " + nameString() + ". Could not create session.");
        }
        return consumer;
    }
    if (session == null) {
        return consumer;
    }
    javax.jms.MessageConsumer c;
    try {
        c = newConsumer(session);
    } catch (JMSException e) {
        if (logger.isDebugEnabled()) {
            logger.error(
                    "Could not create a Message Consumer for " + nameString() + ". Could not create consumer.");
        }
        return consumer;
    }
    consumer.setConnection(connection).setSession(session).setConsumer(c);
    if (logger.isDebugEnabled()) {
        logger.debug(nameString() + " created message consumer " + consumer.getId());
    }
    return consumer;
}