Example usage for javax.jms Message acknowledge

List of usage examples for javax.jms Message acknowledge

Introduction

In this page you can find the example usage for javax.jms Message acknowledge.

Prototype


void acknowledge() throws JMSException;

Source Link

Document

Acknowledges all consumed messages of the session of this consumed message.

Usage

From source file:com.chinamobile.bcbsp.comm.ConsumerTool.java

/**
 * Put message into messageQueue with serialize method.
 *///w w  w. ja  va2 s.  com
public void onMessageOptimistic(Message message) throws IOException {
    messagesReceived++;
    try {
        if (message instanceof BytesMessage) {
            BytesMessage mapMsg = (BytesMessage) message;
            BSPMessage bspMsg;
            int packSize = mapMsg.readInt();
            int count = 0;
            int partitionID;
            String srcVertexID;
            String dstVertexID;
            byte[] tag;
            byte[] data;
            int tagLen;
            int dataLen;
            while (count < packSize) {
                partitionID = mapMsg.readInt();
                dstVertexID = mapMsg.readUTF();
                tagLen = mapMsg.readInt();
                tag = new byte[tagLen];
                mapMsg.readBytes(tag);
                dataLen = mapMsg.readInt();
                data = new byte[dataLen];
                mapMsg.readBytes(data);
                // bspMsg = new BSPMessage(partitionID, dstVertexID, tag, data);
                // dst is message if it is not null.
                bspMsg = new BSPMessage(partitionID, dstVertexID, tag, data);
                this.messageQueues.incomeAMessage(dstVertexID, bspMsg);
                this.messageCount++;
                this.messageBytesCount += bspMsg.size();
                count++;
            }
        } else {
            // Message received is not ObjectMessage.
            LOG.error("[ConsumerTool] Message received is not BytesMessage!");
        }
        if (message.getJMSReplyTo() != null) {
            replyProducer.send(message.getJMSReplyTo(),
                    session.createTextMessage("Reply: " + message.getJMSMessageID()));
        }
        if (transacted) {
            if ((messagesReceived % batch) == 0) {
                LOG.info("Commiting transaction for last " + batch + " messages; messages so far = "
                        + messagesReceived);
                session.commit();
            }
        } else if (ackMode == Session.CLIENT_ACKNOWLEDGE) {
            if ((messagesReceived % batch) == 0) {
                LOG.info("Acknowledging last " + batch + " messages; messages so far = " + messagesReceived);
                message.acknowledge();
            }
        }
    } catch (JMSException e) {
        LOG.error("[ConsumerTool] caught: ", e);
    } finally {
        if (sleepTime > 0) {
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
                LOG.error("[ConsumerTool] caught: ", e);
            }
        }
    }
}

From source file:nl.nn.adapterframework.jms.JmsListenerBase.java

protected String retrieveIdFromMessage(Message message, Map threadContext) throws ListenerException {
    String cid = "unset";
    String mode = "unknown";
    String id = "unset";
    Date tsSent = null;//  w  ww .  j a va  2s . co  m
    Destination replyTo = null;
    try {
        mode = deliveryModeToString(message.getJMSDeliveryMode());
    } catch (JMSException ignore) {
        log.debug("ignoring JMSException in getJMSDeliveryMode()", ignore);
    }
    // --------------------------
    // retrieve MessageID
    // --------------------------
    try {
        id = message.getJMSMessageID();
    } catch (JMSException ignore) {
        log.debug("ignoring JMSException in getJMSMessageID()", ignore);
    }
    // --------------------------
    // retrieve CorrelationID
    // --------------------------
    try {
        if (isForceMessageIdAsCorrelationId()) {
            if (log.isDebugEnabled())
                log.debug("forcing the messageID to be the correlationID");
            cid = id;
        } else {
            cid = message.getJMSCorrelationID();
            if (cid == null) {
                cid = id;
                log.debug("Setting correlation ID to MessageId");
            }
        }
    } catch (JMSException ignore) {
        log.debug("ignoring JMSException in getJMSCorrelationID()", ignore);
    }
    // --------------------------
    // retrieve TimeStamp
    // --------------------------
    try {
        long lTimeStamp = message.getJMSTimestamp();
        tsSent = new Date(lTimeStamp);

    } catch (JMSException ignore) {
        log.debug("ignoring JMSException in getJMSTimestamp()", ignore);
    }
    // --------------------------
    // retrieve ReplyTo address
    // --------------------------
    try {
        replyTo = message.getJMSReplyTo();

    } catch (JMSException ignore) {
        log.debug("ignoring JMSException in getJMSReplyTo()", ignore);
    }

    if (log.isDebugEnabled()) {
        log.debug(getLogPrefix() + "listener on [" + getDestinationName()
                + "] got message with JMSDeliveryMode=[" + mode + "] \n  JMSMessageID=[" + id
                + "] \n  JMSCorrelationID=[" + cid + "] \n  Timestamp Sent=[" + DateUtils.format(tsSent)
                + "] \n  ReplyTo=[" + ((replyTo == null) ? "none" : replyTo.toString()) + "] \n Message=["
                + message.toString() + "]");
    }
    PipeLineSessionBase.setListenerParameters(threadContext, id, cid, null, tsSent);
    threadContext.put("timestamp", tsSent);
    threadContext.put("replyTo", replyTo);
    try {
        if (getAckMode() == Session.CLIENT_ACKNOWLEDGE) {
            message.acknowledge();
            log.debug("Listener on [" + getDestinationName() + "] acknowledged message");
        }
    } catch (JMSException e) {
        log.error("Warning in ack", e);
    }
    return cid;
}

From source file:org.sakaiproject.nakamura.grouper.event.SyncJMSMessageConsumer.java

@SuppressWarnings("unchecked")
public void onMessage(Message message) {
    log.debug("Receiving a message on {} : {}", SyncJMSMessageProducer.QUEUE_NAME, message);
    try {/*from ww  w  . j a v  a 2 s  .co  m*/

        String topic = message.getJMSType();
        String groupId = (String) message.getStringProperty("path");

        String operation = "UNKNOWN";

        // A group was DELETED
        if ("org/sakaiproject/nakamura/lite/authorizables/DELETE".equals(topic) && config.getDeletesEnabled()) {
            Map<String, Object> attributes = (Map<String, Object>) message
                    .getObjectProperty(StoreListener.BEFORE_EVENT_PROPERTY);
            grouperManager.deleteGroup(groupId, attributes);
            operation = "DELETED";
        }

        // A new group was ADDED or an existing group was UPDATED
        if ("org/sakaiproject/nakamura/lite/authorizables/ADDED".equals(topic)
                || "org/sakaiproject/nakamura/lite/authorizables/UPDATED".equals(topic)) {
            // These events should be under org/sakaiproject/nakamura/lite/authorizables/UPDATED
            // http://jira.sakaiproject.org/browse/KERN-1795
            String membersAdded = (String) message.getStringProperty(GrouperEventUtils.MEMBERS_ADDED_PROP);
            if (membersAdded != null) {
                // membership adds can be attached to the same event for the group add.
                grouperManager.createGroup(groupId, config.getGroupTypes());
                grouperManager.addMemberships(groupId, Arrays.asList(StringUtils.split(membersAdded, ",")));
                operation = "ADD_MEMBERS";
            }

            String membersRemoved = (String) message.getStringProperty(GrouperEventUtils.MEMBERS_REMOVED_PROP);
            if (membersRemoved != null) {
                grouperManager.removeMemberships(groupId,
                        Arrays.asList(StringUtils.split(membersRemoved, ",")));
                operation = "REMOVE_MEMBERS";
            }

            if (membersAdded == null && membersRemoved == null) {
                org.sakaiproject.nakamura.api.lite.Session repositorySession = repository.loginAdministrative();
                AuthorizableManager am = repositorySession.getAuthorizableManager();
                Group group = (Group) am.findAuthorizable(groupId);
                repositorySession.logout();

                if (groupId.startsWith(ContactsGrouperNameProviderImpl.CONTACTS_GROUPID_PREFIX)) {
                    // TODO Why are we not getting added and removed properties on the Message
                    grouperManager.createGroup(groupId, null);
                    grouperManager.addMemberships(groupId, Arrays.asList(group.getMembers()));
                    operation = "UPDATE CONTACTS";
                } else {
                    grouperManager.createGroup(groupId, config.getGroupTypes());
                    grouperManager.addMemberships(groupId, Arrays.asList(group.getMembers()));
                    operation = "CREATE";
                }
            }
        }

        // The message was processed successfully. No exceptions were thrown.
        // We acknowledge the message and its removed from the queue
        message.acknowledge();

        // We got a message that we didn't know what to do with.
        if (operation.equals("UNKNOWN")) {
            log.error("I don't know what to do with this topic: {}. Turn on debug logs to see the message.",
                    topic);
            log.debug(message.toString());
        } else {
            log.info("Successfully processed and acknowledged. {}, {}", operation, groupId);
            log.debug(message.toString());
        }

    } catch (JMSException jmse) {
        log.error("JMSException while processing message.", jmse);
    } catch (Exception e) {
        log.error("Exception while processing message.", e);
    }
}