Example usage for javax.jms TextMessage setJMSCorrelationID

List of usage examples for javax.jms TextMessage setJMSCorrelationID

Introduction

In this page you can find the example usage for javax.jms TextMessage setJMSCorrelationID.

Prototype


void setJMSCorrelationID(String correlationID) throws JMSException;

Source Link

Document

Sets the correlation ID for the message.

Usage

From source file:org.openanzo.combus.bayeux.BridgeConnectionManager.java

/**
 * Marks a temporary topic for deletion. Before deleting the topic, it will be unregistered via the notification registration service. The topic will be
 * deleted once that operation completes.
 * //ww  w  .j a  v a 2s . co m
 * @param topicToClose
 *            the temporary topic to close.
 * @param username
 *            the username of the user to which the temporary topic belongs
 * @param clientId
 *            the clientId of the specific Bayeux connection to which the temporary topic applies.
 */
private void cleanupTemporaryTopic(TemporaryTopic topicToClose, MessageConsumer tempTopicConsumerToClose,
        String username, String clientId) {

    String correlationId = UUID.randomUUID().toString();
    mapLock.lock();
    try {
        topicsToDelete.put(correlationId, new ClientStateToClose(tempTopicConsumerToClose, topicToClose,
                System.currentTimeMillis(), username, clientId));
    } finally {
        mapLock.unlock();
    }

    log.debug(LogUtils.COMBUS_MARKER, "Sending unregister subscriber message for {}/{}", username, clientId);
    try {
        TextMessage tmsg = session.createTextMessage();
        tmsg.setJMSCorrelationID(correlationId);
        tmsg.setJMSReplyTo(topicToClose);
        tmsg.setStringProperty(SerializationConstants.operation,
                INotificationRegistrationService.UNREGISTER_SUBSCRIBER);
        tmsg.setStringProperty("runAsUser", username);
        mp.send(destinations.get(COMBUS.NOTIFICATION_SERVICE_QUEUE), tmsg);
    } catch (JMSException e) {
        MDC.put(LogUtils.USER, username);
        log.warn(LogUtils.COMBUS_MARKER, "Error while sending real-time update subscription remove request for "
                + username + "/" + clientId, e);
        MDC.clear();
    }

}

From source file:org.openanzo.combus.bayeux.BridgeConnectionManager.java

/**
 * Send a JMS message on behalf of the given client to a specific destination. The destination is a string that names an abstract queue such as that in
 * Constants.NOTIFICATION_SERVICE_QUEUE, etc.
 * //from   w w w  .  ja va2 s  . co m
 * @param clientId
 * @param destination
 * @param messageProperties
 * @param msgBody
 * @return returns whether or not this message was published to a topic
 * @throws JMSException
 * @throws AnzoException
 */
protected boolean sendClientMessage(String clientId, AnzoPrincipal principal, String destination,
        Map<?, ?> messageProperties, String msgBody, IOperationContext opContext)
        throws JMSException, AnzoException {
    //long destinationProfiler = profiler.start("Resolving destination.");
    Destination dest = destinations.get(destination);
    //profiler.stop(destinationProfiler);
    if (dest == null && destination.startsWith("services/")) {
        dest = session.createQueue(destination);
        destinations.put(destination, dest);
    }
    if (dest == null) { // we probably have a statement channel
        //long nullDestProfiler = profiler.start("Sending client message with null destination.");
        if (destination == null || !destination.startsWith(NAMESPACES.STREAM_TOPIC_PREFIX)) {
            //profiler.stop(nullDestProfiler);
            throw new AnzoException(ExceptionConstants.COMBUS.INVALID_TOPIC, destination);
        }
        // first we have to get the named graph uri out of the statement channel topic.
        String uri = UriGenerator.stripEncapsulatedString(NAMESPACES.STREAM_TOPIC_PREFIX, destination);
        URI graphUri = Constants.valueFactory.createURI(uri);
        if (!userHasGraphAddAccess(graphUri, principal, opContext)) {
            //profiler.stop(nullDestProfiler);
            throw new AnzoException(ExceptionConstants.COMBUS.NOT_AUTHORIZED_FOR_TOPIC,
                    opContext.getOperationPrincipal().getUserURI().toString(), destination);
        }
        Topic topic = session.createTopic(destination);

        TextMessage tmsg = session.createTextMessage();
        for (Map.Entry<?, ?> prop : messageProperties.entrySet()) {
            tmsg.setStringProperty(prop.getKey().toString(), prop.getValue().toString());
        }
        tmsg.setText(msgBody);
        mp.send(topic, tmsg);
        //profiler.stop(nullDestProfiler);
        return true;
    } else {
        TemporaryTopic tempTopicForReply;
        //long = clientStateProfiler = profiler.start("Obtaining Bayeux client state.");
        mapLock.lock();
        try {
            ClientState state = clientIdToClientState.get(clientId);
            if (state == null) {
                throw new AnzoException(ExceptionConstants.CLIENT.CLIENT_NOT_CONNECTED);
            }
            tempTopicForReply = state.topic;
        } finally {
            mapLock.unlock();
            //profiler.stop(clientStateProfiler);
        }

        //long prepareJmsProfiler = profiler.start("Preparing JMS Message.");
        TextMessage tmsg = session.createTextMessage();
        int priority = 4;
        for (Map.Entry<?, ?> prop : messageProperties.entrySet()) {
            if (JMS_MSG_PROPERTY_CORRELATION_ID.equals(prop.getKey())) {
                tmsg.setJMSCorrelationID(prop.getValue().toString());
            }
            if (JMS_MSG_PROPERTY_PRIORITY.equals(prop.getKey())) {
                priority = Integer.parseInt(prop.getValue().toString());
            } else {
                tmsg.setStringProperty(prop.getKey().toString(), prop.getValue().toString());
            }
        }
        tmsg.setJMSPriority(priority);
        tmsg.setJMSReplyTo(tempTopicForReply);
        tmsg.setText(msgBody);
        String username = principal.getName();
        tmsg.setStringProperty("runAsUser", username);
        //profiler.stop(prepareJmsProfiler);
        long sendJmsProfiler = profiler.start("Sending JMS Message");
        mp.setPriority(priority);
        mp.send(dest, tmsg);
        profiler.stop(sendJmsProfiler);
        return false;
    }

}

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

public TextMessage createTextMessage(Session session, String correlationID, String message)
        throws javax.naming.NamingException, JMSException {
    TextMessage textMessage = null;
    textMessage = session.createTextMessage();
    if (null != correlationID) {
        if (correlationIdMaxLength >= 0) {
            int cidlen;
            if (correlationID.startsWith(correlationIdToHexPrefix)) {
                cidlen = correlationID.length() - correlationIdToHexPrefix.length();
            } else {
                cidlen = correlationID.length();
            }//from  w w w  .  j  a v a 2s  .  c om
            if (cidlen > correlationIdMaxLength) {
                correlationID = correlationIdToHexPrefix
                        + correlationID.substring(correlationID.length() - correlationIdMaxLength);
                if (log.isDebugEnabled())
                    log.debug("correlationId shortened to [" + correlationID + "]");
            }
        }
        if (correlationIdToHex && correlationID.startsWith(correlationIdToHexPrefix)) {
            String hexCorrelationID = correlationIdToHexPrefix;
            int i;
            for (i = correlationIdToHexPrefix.length(); i < correlationID.length(); i++) {
                int c = correlationID.charAt(i);
                hexCorrelationID += Integer.toHexString(c);
            }
            ;
            correlationID = hexCorrelationID;
            if (log.isDebugEnabled())
                log.debug("correlationId changed, based on hexidecimal values, to [" + correlationID + "]");
        }
        textMessage.setJMSCorrelationID(correlationID);
    }
    textMessage.setText(message);
    return textMessage;
}

From source file:org.openanzo.combus.endpoint.BaseServiceListener.java

private void processMessage(Message request) {
    try {/* ww  w  .j a  v  a  2 s .c o m*/
        IOperationContext context = null;
        TextMessage response = null;
        String operation = request.getStringProperty(SerializationConstants.operation);
        try {
            Destination replyTo = null;
            try {
                if (mp == null) {
                    mp = session.createProducer(null);
                    mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                }
                replyTo = request.getJMSReplyTo();

                String resultFormat = request.getStringProperty(SerializationConstants.resultFormat);
                context = new BaseOperationContext(operation, request.getJMSCorrelationID(), null);
                if (request.propertyExists(SerializationConstants.userDescription)) {
                    context.setAttribute(SerializationConstants.userDescription,
                            request.getStringProperty(SerializationConstants.userDescription));
                }
                if (request.propertyExists(OPTIONS.SKIPCACHE)) {
                    context.setAttribute(OPTIONS.SKIPCACHE, request.getBooleanProperty(OPTIONS.SKIPCACHE));
                }
                if (request.propertyExists(OPTIONS.INCLUDEMETADATAGRAPHS)) {
                    context.setAttribute(OPTIONS.INCLUDEMETADATAGRAPHS,
                            request.getBooleanProperty(OPTIONS.INCLUDEMETADATAGRAPHS));
                }

                AnzoPrincipal callerPrincipal = getContextPrincipal(context, request);
                context.setOperationPrincipal(callerPrincipal);

                if (log.isTraceEnabled()) {
                    log.trace(LogUtils.COMBUS_MARKER,
                            MessageUtils.prettyPrint(request,
                                    "Message Recieved from [" + callerPrincipal.getName() + "]"
                                            + ((replyTo != null) ? (" with replyto [" + replyTo + "]") : "")));
                } else if (log.isDebugEnabled()) {
                    log.debug(LogUtils.COMBUS_MARKER, "Message Recieved from [" + callerPrincipal.getName()
                            + "]" + ((replyTo != null) ? (" with replyto [" + replyTo + "]") : ""));
                }
                context.setMDC();
                Boolean analyzeRequest = request
                        .getBooleanProperty(RequestAnalysis.CONTEXT_PROP_REQUEST_ENABLED);
                if (analyzeRequest || recorder != null) {
                    RequestAnalysis.setCurrentContext(context.getAttributes());
                    RequestAnalysis.setRequestAnalysisEnabled(true);
                }

                if (recorder != null) {
                    recorder.recordRequest((TextMessage) request, request.getStringProperty("JMSXUserID"),
                            request.getStringProperty(SerializationConstants.runAsUser));
                }

                long start = 0, end = 0;
                if (RequestAnalysis.isAnalysisEnabled(context.getAttributes())) {
                    start = System.currentTimeMillis();
                }

                response = handleMessage(context, replyTo, resultFormat, operation, (TextMessage) request, mp);
                if (RequestAnalysis.isAnalysisEnabled(context.getAttributes())) {
                    end = System.currentTimeMillis();
                    RequestAnalysis.addAnalysisProperty(RequestAnalysis.ANS_PROP_OPERATION_TIME,
                            String.valueOf(end - start));
                }

                if (response != null) {
                    response.setIntProperty(SerializationConstants.protocolVersion, Constants.VERSION);
                    if (operation != null) {
                        response.setStringProperty(SerializationConstants.operation, operation);
                    }
                    Integer totalSolutions = context.getAttribute(SerializationConstants.totalSolutions,
                            Integer.class);
                    if (totalSolutions != null) {
                        response.setIntProperty(SerializationConstants.totalSolutions,
                                totalSolutions.intValue());
                    }
                    if (analyzeRequest) {
                        for (String name : RequestAnalysis.getAnalysisPropertyNames()) {
                            response.setStringProperty(name, context.getAttribute(name).toString());
                        }
                    }
                }

                if (response != null && replyTo != null) {
                    response.setJMSCorrelationID(request.getJMSCorrelationID());
                    if (log.isTraceEnabled()) {
                        log.trace(LogUtils.COMBUS_MARKER,
                                MessageUtils.prettyPrint(response, "Sending Response to [" + replyTo + "]"));
                    } else if (log.isDebugEnabled()) {
                        log.debug(LogUtils.COMBUS_MARKER, "Sending Response to [" + replyTo + "]");
                    }
                    mp.send(replyTo, response);
                }

            } catch (JMSException jmex) {
                response = sendJMSErrorMessage(replyTo, request, jmex,
                        ExceptionConstants.COMBUS.JMS_SERVICE_EXCEPTION, jmex.toString());
            } catch (AnzoException jmex) {
                response = sendJMSErrorMessage(replyTo, request, jmex, jmex.getErrorCode(), jmex.getArgs());
            } catch (AnzoRuntimeException jmex) {
                response = sendJMSErrorMessage(replyTo, request, jmex, jmex.getErrorCode(), jmex.getArgs());
            } catch (RuntimeException jmex) {
                response = sendJMSErrorMessage(replyTo, request, jmex,
                        ExceptionConstants.COMBUS.JMS_SERVICE_EXCEPTION, jmex.toString());
            } catch (Throwable jmex) {
                response = sendJMSErrorMessage(replyTo, request, jmex,
                        ExceptionConstants.COMBUS.JMS_SERVICE_EXCEPTION, jmex.toString());
            }

            if (recorder != null) {
                if (response != null) {
                    recorder.recordResponse(response);
                } else {
                    recorder.recordResponse(request.getJMSCorrelationID(), operation);
                }
            }
        } finally {
            if (context != null) {
                context.clearMDC();
            }
        }
    } catch (JMSException jmsex) {
        if (jmsex.getCause() instanceof InterruptedException) {
            log.debug(LogUtils.COMBUS_MARKER, "Thread interrupted in order to stop.", jmsex);
        } else {
            log.error(LogUtils.COMBUS_MARKER, "Error in BaseService Listener's process thread loop", jmsex);
        }
    } catch (Throwable jmex) {
        log.error(LogUtils.COMBUS_MARKER, "Error in BaseService Listener's process thread loop", jmex);
    }
}