Example usage for javax.jms ObjectMessage acknowledge

List of usage examples for javax.jms ObjectMessage acknowledge

Introduction

In this page you can find the example usage for javax.jms ObjectMessage 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.cws.esolutions.core.utils.MQUtils.java

/**
 * Gets an MQ message off a specified queue and returns it as an
 * <code>Object</code> to the requestor for further processing.
 *
 * @param connName - The connection name to utilize
 * @param authData - The authentication data to utilize, if required
 * @param responseQueue - The request queue name to put the message on
 * @param timeout - How long to wait for a connection or response
 * @param messageId - The JMS correlation ID of the message the response is associated with
 * @return <code>Object</code> - The serializable data returned by the MQ request
 * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing
 *//*from   w w w  .  ja va2 s . c  o  m*/
public static final synchronized Object getMqMessage(final String connName, final List<String> authData,
        final String responseQueue, final long timeout, final String messageId) throws UtilityException {
    final String methodName = MQUtils.CNAME
            + "getMqMessage(final String connName, final List<String> authData, final String responseQueue, final long timeout, final String messageId) throws UtilityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", connName);
        DEBUGGER.debug("Value: {}", responseQueue);
        DEBUGGER.debug("Value: {}", timeout);
        DEBUGGER.debug("Value: {}", messageId);
    }

    Connection conn = null;
    Session session = null;
    Object response = null;
    Context envContext = null;
    MessageConsumer consumer = null;
    ConnectionFactory connFactory = null;

    try {
        try {
            InitialContext initCtx = new InitialContext();
            envContext = (Context) initCtx.lookup(MQUtils.INIT_CONTEXT);

            connFactory = (ConnectionFactory) envContext.lookup(connName);
        } catch (NamingException nx) {
            // we're probably not in a container
            connFactory = new ActiveMQConnectionFactory(connName);
        }

        if (DEBUG) {
            DEBUGGER.debug("ConnectionFactory: {}", connFactory);
        }

        if (connFactory == null) {
            throw new UtilityException("Unable to create connection factory for provided name");
        }

        // Create a Connection
        conn = connFactory.createConnection(authData.get(0),
                PasswordUtils.decryptText(authData.get(1), authData.get(2), authData.get(3),
                        Integer.parseInt(authData.get(4)), Integer.parseInt(authData.get(5)), authData.get(6),
                        authData.get(7), authData.get(8)));
        conn.start();

        if (DEBUG) {
            DEBUGGER.debug("Connection: {}", conn);
        }

        // Create a Session
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        if (DEBUG) {
            DEBUGGER.debug("Session: {}", session);
        }

        if (envContext != null) {
            try {
                consumer = session.createConsumer((Destination) envContext.lookup(responseQueue),
                        "JMSCorrelationID='" + messageId + "'");
            } catch (NamingException nx) {
                throw new UtilityException(nx.getMessage(), nx);
            }
        } else {
            Destination destination = session.createQueue(responseQueue);

            if (DEBUG) {
                DEBUGGER.debug("Destination: {}", destination);
            }

            consumer = session.createConsumer(destination, "JMSCorrelationID='" + messageId + "'");
        }

        if (DEBUG) {
            DEBUGGER.debug("MessageConsumer: {}", consumer);
        }

        ObjectMessage message = (ObjectMessage) consumer.receive(timeout);

        if (DEBUG) {
            DEBUGGER.debug("ObjectMessage: {}", message);
        }

        if (message == null) {
            throw new UtilityException("Failed to retrieve message within the timeout specified.");
        }

        response = message.getObject();
        message.acknowledge();

        if (DEBUG) {
            DEBUGGER.debug("Object: {}", response);
        }
    } catch (JMSException jx) {
        throw new UtilityException(jx.getMessage(), jx);
    } finally {
        try {
            // Clean up
            if (!(session == null)) {
                session.close();
            }

            if (!(conn == null)) {
                conn.close();
                conn.stop();
            }
        } catch (JMSException jx) {
            ERROR_RECORDER.error(jx.getMessage(), jx);
        }
    }

    return response;
}

From source file:com.cws.esolutions.agent.mq.MQMessageHandler.java

public void onMessage(final Message message) {
    final String methodName = MQMessageHandler.CNAME + "#onMessage(final Message message)";

    if (DEBUG) {//from   ww  w  . j a  v  a 2s  . c  om
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Message: {}", message);
    }

    final Session session = agentBean.getSession();
    final MessageProducer producer = agentBean.getProducer();
    final Destination destination = MQMessageHandler.agentBean.getResponseQueue();

    if (DEBUG) {
        DEBUGGER.debug("Session: {}", session);
        DEBUGGER.debug("MessageProducer: {}", producer);
        DEBUGGER.debug("Destination: {}", destination);
    }

    try {
        ObjectMessage mqMessage = (ObjectMessage) message;

        if (DEBUG) {
            DEBUGGER.debug("mqMessage: {}", mqMessage);
        }

        if ((StringUtils.equals(MQMessageHandler.serverConfig.getRequestQueue(),
                MQMessageHandler.serverConfig.getResponseQueue()))
                && (mqMessage.getObject() instanceof AgentResponse)) {
            return;
        }

        AgentRequest agentRequest = (AgentRequest) mqMessage.getObject();

        if (DEBUG) {
            DEBUGGER.debug("agentRequest: {}", agentRequest);
        }

        mqMessage.acknowledge();

        AgentResponse agentResponse = MQMessageHandler.processor.processRequest(agentRequest);

        if (DEBUG) {
            DEBUGGER.debug("AgentResponse: {}", agentResponse);
        }

        ObjectMessage oMessage = session.createObjectMessage(true);
        oMessage.setObject(agentResponse);
        oMessage.setJMSCorrelationID(message.getJMSCorrelationID());

        if (DEBUG) {
            DEBUGGER.debug("ObjectMessage: {}", oMessage);
        }

        producer.send(destination, oMessage);
    } catch (JMSException jx) {
        ERROR_RECORDER.error(jx.getMessage(), jx);
    } catch (AgentException ax) {
        ERROR_RECORDER.error(ax.getMessage(), ax);
    }
}

From source file:org.jbpm.bpel.integration.jms.RequestListener.java

public void onMessage(Message message) {
    if (!(message instanceof ObjectMessage)) {
        log.error("received non-object message: " + message);
        return;//from   ww w.ja v  a  2 s  . c o  m
    }
    try {
        ObjectMessage request = (ObjectMessage) message;
        log.debug("delivering request: " + RequestListener.messageToString(request));
        /*
         * LEAK WARNING. This listener must be removed from the integration control before passing
         * control to the inbound message activity, because loop structures could execute the same
         * activity again and overwrite the entry in the integration control with a new listener. If
         * removeRequestListener() was invoked after passing control to the activity, the new listener
         * would get removed instead of this, leaving the listener open but unreachable from
         * application code. CODE ORDER NOTE. Removing this listener early in the process prevents any
         * other thread from closing it. This effect is desirable because the orderly shutdown
         * mechanism of JMS never stops a running listener anyway. Furthermore, the mechanism is
         * specified to *block* the other thread until this listener returns.
         */
        if (oneShot)
            integrationControl.removeRequestListener(this);

        deliverRequest((Map) request.getObject(), request.getJMSReplyTo(), request.getJMSMessageID());
        request.acknowledge();

        if (oneShot)
            close();
    } catch (JMSException e) {
        log.error("request delivery failed due to jms exception, giving up", e);
    } catch (RuntimeException e) {
        if (isRecoverable(e)) {
            log.warn("request delivery failed due to recoverable exception, attempting recovery");
            try {
                // recover the session manually
                jmsSession.recover();
            } catch (JMSException je) {
                log.error("request recovery failed, giving up", je);
            }
        } else
            log.error("request delivery failed due to non-recoverable exception, giving up", e);
    }
}

From source file:org.jbpm.bpel.integration.jms.StartListener.java

public void onMessage(Message message) {
    if (!(message instanceof ObjectMessage)) {
        log.error("received non-object jms message: " + message);
        return;/*from  w  ww  .j  av a 2 s.c o m*/
    }

    try {
        ObjectMessage request = (ObjectMessage) message;
        log.debug("delivering request: " + RequestListener.messageToString(request));

        /*
         * CODE ORDER NOTE. Removing this listener early in the process prevents any other thread from
         * closing it. This effect is desirable because the orderly shutdown mechanism of JMS never
         * stops a running listener anyway. Furthermore, the mechanism is specified to *block* the
         * other thread until this listener returns.
         */
        integrationControl.removeStartListener(this);

        // BPEL-282 create new start listener to improve concurrency
        StartListener startListener = new StartListener(this);
        startListener.open();

        deliverRequest((Map) request.getObject(), request.getJMSReplyTo(), request.getJMSMessageID());
        request.acknowledge();

        close();
    } catch (JMSException e) {
        log.error("request delivery failed due to jms exception, giving up", e);
    } catch (RuntimeException e) {
        if (RequestListener.isRecoverable(e)) {
            log.warn("request delivery failed due to recoverable exception, attempting recovery");
            try {
                // recover the session manually
                jmsSession.recover();
            } catch (JMSException je) {
                log.error("request recovery failed, giving up", je);
            }
        } else
            log.error("request delivery failed due to non-recoverable exception, giving up", e);
    }
}

From source file:org.jbpm.bpel.integration.server.SoapHandler.java

protected ObjectMessage receiveResponse(Session jmsSession, Destination replyTo, String requestId,
        JbpmContext jbpmContext) throws JMSException, SOAPFaultException {
    // set up consumer
    String selector = "JMSCorrelationID='" + requestId + '\'';
    MessageConsumer consumer = jmsSession.createConsumer(replyTo, selector);
    try {/*from   w  w w. ja  v  a 2  s  .c o  m*/
        // receive response message
        log.debug("listening for response: destination=" + replyTo + ", requestId=" + requestId);
        Number responseTimeout = getResponseTimeout(jbpmContext);
        ObjectMessage jmsResponse = (ObjectMessage) (responseTimeout != null
                ? consumer.receive(responseTimeout.longValue())
                : consumer.receive());
        // did a message arrive in time?
        if (jmsResponse == null) {
            log.debug("response timeout expired: destination=" + replyTo + ", requestId" + requestId);
            throw new SOAPFaultException(SoapBindConstants.SERVER_FAULTCODE,
                    SoapBindConstants.TIMEOUT_FAULTSTRING, null, null);
        }
        jmsResponse.acknowledge();
        log.debug("received response: " + RequestListener.messageToString(jmsResponse));
        return jmsResponse;
    } finally {
        // release consumer resources
        consumer.close();
    }
}