Example usage for javax.jms Destination toString

List of usage examples for javax.jms Destination toString

Introduction

In this page you can find the example usage for javax.jms Destination toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

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

public String sendMessage(String correlationID, String message, ParameterResolutionContext prc,
        String soapHeader) throws SenderException, TimeOutException {
    Session s = null;//from  w  w  w .jav a 2  s.co  m
    MessageProducer mp = null;

    ParameterValueList pvl = null;
    if (prc != null && paramList != null) {
        try {
            pvl = prc.getValues(paramList);
        } catch (ParameterException e) {
            throw new SenderException(getLogPrefix() + "cannot extract parameters", e);
        }
    }

    if (isSoap()) {
        if (soapHeader == null) {
            if (pvl != null && StringUtils.isNotEmpty(getSoapHeaderParam())) {
                ParameterValue soapHeaderParamValue = pvl.getParameterValue(getSoapHeaderParam());
                if (soapHeaderParamValue == null) {
                    log.warn("no SoapHeader found using parameter [" + getSoapHeaderParam() + "]");
                } else {
                    soapHeader = soapHeaderParamValue.asStringValue("");
                }
            }
        }
        message = soapWrapper.putInEnvelope(message, getEncodingStyleURI(), getServiceNamespaceURI(),
                soapHeader);
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "correlationId [" + correlationID + "] soap message [" + message + "]");
    }
    try {
        s = createSession();
        mp = getMessageProducer(s, getDestination(prc));
        Destination replyQueue = null;

        // create message
        Message msg = createTextMessage(s, correlationID, message);

        if (getMessageType() != null) {
            msg.setJMSType(getMessageType());
        }
        if (getDeliveryModeInt() > 0) {
            msg.setJMSDeliveryMode(getDeliveryModeInt());
            mp.setDeliveryMode(getDeliveryModeInt());
        }
        if (getPriority() >= 0) {
            msg.setJMSPriority(getPriority());
            mp.setPriority(getPriority());
        }

        // set properties
        if (pvl != null) {
            setProperties(msg, pvl);
        }
        if (replyToName != null) {
            replyQueue = getDestination(replyToName);
        } else {
            if (isSynchronous()) {
                replyQueue = getMessagingSource().getDynamicReplyQueue(s);
            }
        }
        if (replyQueue != null) {
            msg.setJMSReplyTo(replyQueue);
            if (log.isDebugEnabled())
                log.debug("replyTo set to queue [" + replyQueue.toString() + "]");
        }

        // send message   
        send(mp, msg);
        if (log.isDebugEnabled()) {
            log.debug("[" + getName() + "] " + "sent message [" + message + "] " + "to [" + mp.getDestination()
                    + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID ["
                    + msg.getJMSCorrelationID() + "] " + "using deliveryMode [" + getDeliveryMode() + "] "
                    + ((replyToName != null) ? "replyTo [" + replyToName + "]" : ""));
        } else {
            if (log.isInfoEnabled()) {
                log.info("[" + getName() + "] " + "sent message to [" + mp.getDestination() + "] " + "msgID ["
                        + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] "
                        + "using deliveryMode [" + getDeliveryMode() + "] "
                        + ((replyToName != null) ? "replyTo [" + replyToName + "]" : ""));
            }
        }
        if (isSynchronous()) {
            String replyCorrelationId = null;
            if (replyToName != null) {
                if ("CORRELATIONID".equalsIgnoreCase(getLinkMethod())) {
                    replyCorrelationId = correlationID;
                } else if ("CORRELATIONID_FROM_MESSAGE".equalsIgnoreCase(getLinkMethod())) {
                    replyCorrelationId = msg.getJMSCorrelationID();
                } else {
                    replyCorrelationId = msg.getJMSMessageID();
                }
            }
            if (log.isDebugEnabled())
                log.debug("[" + getName() + "] start waiting for reply on [" + replyQueue + "] requestMsgId ["
                        + msg.getJMSMessageID() + "] replyCorrelationId [" + replyCorrelationId + "] for ["
                        + getReplyTimeout() + "] ms");
            MessageConsumer mc = getMessageConsumerForCorrelationId(s, replyQueue, replyCorrelationId);
            try {
                Message rawReplyMsg = mc.receive(getReplyTimeout());
                if (rawReplyMsg == null) {
                    throw new TimeOutException("did not receive reply on [" + replyQueue + "] requestMsgId ["
                            + msg.getJMSMessageID() + "] replyCorrelationId [" + replyCorrelationId
                            + "] within [" + getReplyTimeout() + "] ms");
                }
                return getStringFromRawMessage(rawReplyMsg, prc != null ? prc.getSession() : null, isSoap(),
                        getReplySoapHeaderSessionKey(), soapWrapper);
            } finally {
                if (mc != null) {
                    try {
                        mc.close();
                    } catch (JMSException e) {
                        log.warn("JmsSender [" + getName()
                                + "] got exception closing message consumer for reply", e);
                    }
                }
            }
        }
        return msg.getJMSMessageID();
    } catch (JMSException e) {
        throw new SenderException(e);
    } catch (IOException e) {
        throw new SenderException(e);
    } catch (NamingException e) {
        throw new SenderException(e);
    } catch (DomBuilderException e) {
        throw new SenderException(e);
    } catch (TransformerException e) {
        throw new SenderException(e);
    } catch (JmsException e) {
        throw new SenderException(e);
    } finally {
        if (mp != null) {
            try {
                mp.close();
            } catch (JMSException e) {
                log.warn("JmsSender [" + getName() + "] got exception closing message producer", e);
            }
        }
        closeSession(s);
    }
}

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

public void afterMessageProcessed(PipeLineResult plr, Object rawMessage, Map threadContext)
        throws ListenerException {
    String cid = (String) threadContext.get(IPipeLineSession.technicalCorrelationIdKey);

    if (log.isDebugEnabled())
        log.debug(getLogPrefix() + "in PullingJmsListener.afterMessageProcessed()");

    try {//from   ww  w  . ja  v a 2 s  .c  o m
        Destination replyTo = (Destination) threadContext.get("replyTo");

        // handle reply
        if (isUseReplyTo() && (replyTo != null)) {
            Session session = null;

            log.debug(getLogPrefix() + "sending reply message with correlationID [" + cid + "], replyTo ["
                    + replyTo.toString() + "]");
            long timeToLive = getReplyMessageTimeToLive();
            boolean ignoreInvalidDestinationException = false;
            if (timeToLive == 0) {
                Message messageSent = (Message) rawMessage;
                long expiration = messageSent.getJMSExpiration();
                if (expiration != 0) {
                    timeToLive = expiration - new Date().getTime();
                    if (timeToLive <= 0) {
                        log.warn(getLogPrefix() + "message [" + cid + "] expired [" + timeToLive
                                + "]ms, sending response with 1 second time to live");
                        timeToLive = 1000;
                        // In case of a temporary queue it might already
                        // have disappeared.
                        ignoreInvalidDestinationException = true;
                    }
                }
            }
            if (threadContext != null) {
                session = (Session) threadContext.get(THREAD_CONTEXT_SESSION_KEY);
            }
            if (session == null) {
                try {
                    session = getSession(threadContext);
                    send(session, replyTo, cid, prepareReply(plr.getResult(), threadContext),
                            getReplyMessageType(), timeToLive, stringToDeliveryMode(getReplyDeliveryMode()),
                            getReplyPriority(), ignoreInvalidDestinationException);
                } finally {
                    releaseSession(session);
                }
            } else {
                send(session, replyTo, cid, plr.getResult(), getReplyMessageType(), timeToLive,
                        stringToDeliveryMode(getReplyDeliveryMode()), getReplyPriority(),
                        ignoreInvalidDestinationException);
            }
        } else {
            if (getSender() == null) {
                log.debug(getLogPrefix()
                        + "itself has no sender to send the result (An enclosing Receiver might still have one).");
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(getLogPrefix()
                            + "no replyTo address found or not configured to use replyTo, using default destination"
                            + "sending message with correlationID[" + cid + "] [" + plr.getResult() + "]");
                }
                getSender().sendMessage(cid, plr.getResult());
            }
        }

        // TODO Do we still need this? Should we rollback too? See
        // PushingJmsListener.afterMessageProcessed() too (which does a
        // rollback, but no commit).
        if (!isTransacted()) {
            if (isJmsTransacted()) {
                // the following if transacted using transacted sessions, instead of XA-enabled sessions.
                Session session = (Session) threadContext.get(THREAD_CONTEXT_SESSION_KEY);
                if (session == null) {
                    log.warn("Listener [" + getName() + "] message [" + (String) threadContext.get("id")
                            + "] has no session to commit or rollback");
                } else {
                    String successState = getCommitOnState();
                    if (successState != null && successState.equals(plr.getState())) {
                        session.commit();
                    } else {
                        log.warn("Listener [" + getName() + "] message [" + (String) threadContext.get("id")
                                + "] not committed nor rolled back either");
                        //TODO: enable rollback, or remove support for JmsTransacted altogether (XA-transactions should do it all)
                        // session.rollback();
                    }
                    if (isSessionsArePooled()) {
                        threadContext.remove(THREAD_CONTEXT_SESSION_KEY);
                        releaseSession(session);
                    }
                }
            } else {
                // TODO: dit weghalen. Het hoort hier niet, en zit ook al in getIdFromRawMessage. Daar hoort het ook niet, overigens...
                if (getAckMode() == Session.CLIENT_ACKNOWLEDGE) {
                    log.debug("[" + getName() + "] acknowledges message with id [" + cid + "]");
                    ((TextMessage) rawMessage).acknowledge();
                }
            }
        }
    } catch (Exception e) {
        throw new ListenerException(e);
    }
}

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

public void afterMessageProcessed(PipeLineResult plr, Object rawMessage, Map threadContext)
        throws ListenerException {
    String cid = (String) threadContext.get(IPipeLineSession.technicalCorrelationIdKey);
    Session session = (Session) threadContext.get(jmsConnector.THREAD_CONTEXT_SESSION_KEY); // session is/must be saved in threadcontext by JmsConnector

    if (log.isDebugEnabled())
        log.debug(getLogPrefix() + "in PushingJmsListener.afterMessageProcessed()");
    try {/*  w ww.  ja v  a  2  s .com*/
        Destination replyTo = (Destination) threadContext.get("replyTo");

        // handle reply
        if (isUseReplyTo() && (replyTo != null)) {

            log.debug("sending reply message with correlationID[" + cid + "], replyTo [" + replyTo.toString()
                    + "]");
            long timeToLive = getReplyMessageTimeToLive();
            boolean ignoreInvalidDestinationException = false;
            if (timeToLive == 0) {
                Message messageReceived = (Message) rawMessage;
                long expiration = messageReceived.getJMSExpiration();
                if (expiration != 0) {
                    timeToLive = expiration - new Date().getTime();
                    if (timeToLive <= 0) {
                        log.warn("message [" + cid + "] expired [" + timeToLive
                                + "]ms, sending response with 1 second time to live");
                        timeToLive = 1000;
                        // In case of a temporary queue it might already
                        // have disappeared.
                        ignoreInvalidDestinationException = true;
                    }
                }
            }
            Map properties = getMessagePropertiesToSet(threadContext);
            send(session, replyTo, cid, prepareReply(plr.getResult(), threadContext), getReplyMessageType(),
                    timeToLive, stringToDeliveryMode(getReplyDeliveryMode()), getReplyPriority(),
                    ignoreInvalidDestinationException, properties);
        } else {
            if (getSender() == null) {
                log.info("[" + getName() + "] has no sender, not sending the result.");
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("[" + getName()
                            + "] no replyTo address found or not configured to use replyTo, using default destination"
                            + "sending message with correlationID[" + cid + "] [" + plr.getResult() + "]");
                }
                getSender().sendMessage(cid, plr.getResult());
            }
        }

        // TODO Do we still need this? Should we commit too? See
        // PullingJmsListener.afterMessageProcessed() too (which does a
        // commit, but no rollback).
        if (plr != null && !isTransacted() && isJmsTransacted() && StringUtils.isNotEmpty(getCommitOnState())
                && !getCommitOnState().equals(plr.getState())) {
            if (session == null) {
                log.error(getLogPrefix() + "session is null, cannot roll back session");
            } else {
                log.warn(getLogPrefix() + "got exit state [" + plr.getState() + "], rolling back session");
                session.rollback();
            }
        }
    } catch (Exception e) {
        if (e instanceof ListenerException) {
            throw (ListenerException) e;
        } else {
            throw new ListenerException(e);
        }
    }
}

From source file:org.openanzo.combus.realtime.RealtimeUpdatePublisher.java

/**
 * Register a User with its destination and a session
 * //from  w w  w.j  av  a  2  s.  c  o  m
 * @param userUri
 *            UserURI for client
 * @param destination
 *            Destination for client
 * @param session
 *            Session for the destination
 * @throws AnzoException
 */
void registerClient(AnzoPrincipal principal, Destination destination) throws AnzoException {
    if (log.isDebugEnabled()) {
        log.debug("Registering client - user:{} dest:{}", (principal != null ? principal.getUserURI() : null),
                (destination != null ? destination.toString() : null));
    }
    if (principal == null) {
        throw new AnzoException(ExceptionConstants.CORE.NULL_PARAMETER, "principal");
    }
    userDestinations.put(principal.getUserURI(), destination);
    if (principal.isSysadmin()) {
        registeredSysadmins.add(principal.getUserURI());
    }
    for (URI role : principal.getRoles()) {
        userRolesCache.put(role, principal.getUserURI());
    }
}