Example usage for javax.jms Message getJMSReplyTo

List of usage examples for javax.jms Message getJMSReplyTo

Introduction

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

Prototype


Destination getJMSReplyTo() throws JMSException;

Source Link

Document

Gets the Destination object to which a reply to this message should be sent.

Usage

From source file:org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener.java

/**
 * Determine a response destination for the given message.
 * <p>The default implementation first checks the JMS Reply-To
 * {@link Destination} of the supplied request; if that is not {@code null}
 * it is returned; if it is {@code null}, then the configured
 * {@link #resolveDefaultResponseDestination default response destination}
 * is returned; if this too is {@code null}, then an
 * {@link javax.jms.InvalidDestinationException} is thrown.
 * @param request the original incoming JMS message
 * @param response the outgoing JMS message about to be sent
 * @param session the JMS Session to operate on
 * @return the response destination (never {@code null})
 * @throws JMSException if thrown by JMS API methods
 * @throws javax.jms.InvalidDestinationException if no {@link Destination} can be determined
 * @see #setDefaultResponseDestination// www  . j a v  a2  s .  co m
 * @see javax.jms.Message#getJMSReplyTo()
 */
protected Destination getResponseDestination(Message request, Message response, Session session)
        throws JMSException {

    Destination replyTo = request.getJMSReplyTo();
    if (replyTo == null) {
        replyTo = resolveDefaultResponseDestination(session);
        if (replyTo == null) {
            throw new InvalidDestinationException("Cannot determine response destination: "
                    + "Request message does not contain reply-to destination, and no default response destination set.");
        }
    }
    return replyTo;
}

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

private void moveJmsHeaders(Message in, AdaptrisMessage out) throws JMSException {

    for (JmsPropertyHandler h : JmsPropertyHandler.values()) {
        try {/*from w ww.  ja v  a  2s . co m*/
            h.copy(in, out);
        } catch (Exception e) {
            reportException(h.getKey(), e);
        }
    }
    out.addObjectHeader(JMS_DESTINATION, in.getJMSDestination());
    out.addObjectHeader(JMS_REPLY_TO, in.getJMSReplyTo());
}

From source file:org.apache.synapse.transport.jms.JMSUtils.java

/**
 * Send the given message to the Destination using the given session
 * @param session the session to use to send
 * @param destination the Destination/*ww  w  .j a  va 2 s . co  m*/
 * @param message the JMS Message
 * @throws AxisFault on error
 */
public static void sendMessageToJMSDestination(Session session, Destination destination, Message message)
        throws AxisFault {

    MessageProducer producer = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Sending message to destination : " + destination);
        }

        if (destination instanceof Queue) {
            producer = ((QueueSession) session).createSender((Queue) destination);
            ((QueueSender) producer).send(message);
        } else {
            producer = ((TopicSession) session).createPublisher((Topic) destination);
            ((TopicPublisher) producer).publish(message);
        }

        if (log.isDebugEnabled()) {
            log.debug("Sent message to destination : " + destination + "\nMessage ID : "
                    + message.getJMSMessageID() + "\nCorrelation ID : " + message.getJMSCorrelationID()
                    + "\nReplyTo ID : " + message.getJMSReplyTo());
        }

    } catch (JMSException e) {
        handleException("Error creating a producer or sending to : " + destination, e);
    } finally {
        if (producer != null) {
            try {
                producer.close();
            } catch (JMSException ignore) {
            }
        }
    }
}

From source file:org.jbpm.ejb.impl.CommandListenerBean.java

public void onMessage(Message message) {
    try {//ww w .ja va 2s.c o  m
        // extract command from message
        Command command = extractCommand(message);
        if (command == null) {
            discard(message);
            return;
        }
        // execute command via local command executor bean
        Object result = commandService.execute(command);
        // send a response back if a "reply to" destination is set
        Destination replyTo = message.getJMSReplyTo();
        if (replyTo != null && (result instanceof Serializable || result == null)) {
            sendResult((Serializable) result, replyTo, message.getJMSMessageID());
        }
    } catch (JMSException e) {
        messageDrivenContext.setRollbackOnly();
        log.error("could not process message " + message, e);
    }
}

From source file:org.apache.synapse.transport.jms.JMSMessageReceiver.java

/**
 * The entry point on the recepit of each JMS message
 *
 * @param message the JMS message received
 *//*from   www. jav a 2s  .  c o  m*/
public void onMessage(Message message) {
    // directly create a new worker and delegate processing
    try {
        if (log.isDebugEnabled()) {
            StringBuffer sb = new StringBuffer();
            sb.append("Received JMS message to destination : " + message.getJMSDestination());
            sb.append("\nMessage ID : " + message.getJMSMessageID());
            sb.append("\nCorrelation ID : " + message.getJMSCorrelationID());
            sb.append("\nReplyTo ID : " + message.getJMSReplyTo());
            log.debug(sb.toString());
            if (log.isTraceEnabled() && message instanceof TextMessage) {
                log.trace("\nMessage : " + ((TextMessage) message).getText());
            }
        }
    } catch (JMSException e) {
        if (log.isDebugEnabled()) {
            log.debug("Error reading JMS message headers for debug logging", e);
        }
    }

    // has this message already expired? expiration time == 0 means never expires
    try {
        long expiryTime = message.getJMSExpiration();
        if (expiryTime > 0 && System.currentTimeMillis() > expiryTime) {
            if (log.isDebugEnabled()) {
                log.debug("Discard expired message with ID : " + message.getJMSMessageID());
            }
            return;
        }
    } catch (JMSException ignore) {
    }

    workerPool.execute(new Worker(message));
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

protected JMSHeader createJMSHeader(SubmitContext submitContext, Request request, Hermes hermes,
        Message message, Destination replyToDestination) {
    JMSHeader jmsHeader = new JMSHeader();
    jmsHeader.setMessageHeaders(message, request, hermes, submitContext);
    JMSHeader.setMessageProperties(message, request, hermes, submitContext);
    try {//from w  w w  . ja  v  a2 s .c  o m
        if (message.getJMSReplyTo() == null) {
            message.setJMSReplyTo(replyToDestination);
        }

        if (addSoapAction) {

            message.setStringProperty(JMSHeader.SOAPJMS_SOAP_ACTION, request.getOperation().getName());
            if (request.getOperation() instanceof WsdlOperation) {
                message.setStringProperty(JMSHeader.SOAP_ACTION,
                        ((WsdlOperation) request.getOperation()).getAction());
            } else {
                message.setStringProperty(JMSHeader.SOAP_ACTION, request.getOperation().getName());
            }
        }
    } catch (JMSException e) {
        SoapUI.logError(e);
    }
    return jmsHeader;
}

From source file:de.adorsys.jmspojo.JMSJavaFutureAdapterTest.java

@Before
public void setup() throws Exception {
    broker = new BrokerService();
    broker.setPersistent(false);//from w  w w . j  av  a 2s.c  o  m

    // configure the broker
    broker.addConnector("vm://test");
    broker.setBrokerName("test");
    broker.setUseShutdownHook(false);

    broker.start();

    cf = new ActiveMQConnectionFactory("vm://localhost?create=false");
    qc = cf.createQueueConnection();
    QueueSession createQueueSession = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    testQueue = createQueueSession.createQueue("TestQueue");
    createQueueSession.createReceiver(testQueue).setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            try {
                boolean sendTimeout = message.getBooleanProperty("timeout");
                boolean sendError = message.getBooleanProperty("error");
                if (sendError) {
                    JMSJavaFutureAdapter<PingMessage> jmsSender = new JMSJavaFutureAdapter<PingMessage>(
                            objectMapper, cf, null, TIMEOUT);
                    HashMap<String, Object> messageProperties = new HashMap<String, Object>();
                    messageProperties.put("ERROR", "test error");
                    jmsSender.send(message.getJMSReplyTo(), messageProperties, null);
                } else if (!sendTimeout) {
                    JMSJavaFutureAdapter<PingMessage> jmsSender = new JMSJavaFutureAdapter<PingMessage>(
                            objectMapper, cf, null, TIMEOUT);
                    jmsSender.send(message.getJMSReplyTo(), null, ((TextMessage) message).getText());
                }

            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
    qc.start();

}

From source file:org.apache.axis2.transport.jms.JMSMessageReceiver.java

/**
 * Process a new message received//  w  w  w  . j  ava2s . co m
 *
 * @param message the JMS message received
 * @param ut      UserTransaction which was used to receive the message
 * @return true if caller should commit
 */
public boolean onMessage(Message message, UserTransaction ut) {

    try {
        if (log.isDebugEnabled()) {
            StringBuffer sb = new StringBuffer();
            sb.append("Received new JMS message for service :").append(endpoint.getServiceName());
            sb.append("\nDestination    : ").append(message.getJMSDestination());
            sb.append("\nMessage ID     : ").append(message.getJMSMessageID());
            sb.append("\nCorrelation ID : ").append(message.getJMSCorrelationID());
            sb.append("\nReplyTo        : ").append(message.getJMSReplyTo());
            sb.append("\nRedelivery ?   : ").append(message.getJMSRedelivered());
            sb.append("\nPriority       : ").append(message.getJMSPriority());
            sb.append("\nExpiration     : ").append(message.getJMSExpiration());
            sb.append("\nTimestamp      : ").append(message.getJMSTimestamp());
            sb.append("\nMessage Type   : ").append(message.getJMSType());
            sb.append("\nPersistent ?   : ").append(DeliveryMode.PERSISTENT == message.getJMSDeliveryMode());

            log.debug(sb.toString());
            if (log.isTraceEnabled() && message instanceof TextMessage) {
                log.trace("\nMessage : " + ((TextMessage) message).getText());
            }
        }
    } catch (JMSException e) {
        if (log.isDebugEnabled()) {
            log.debug("Error reading JMS message headers for debug logging", e);
        }
    }

    // update transport level metrics
    try {
        metrics.incrementBytesReceived(JMSUtils.getMessageSize(message));
    } catch (JMSException e) {
        log.warn("Error reading JMS message size to update transport metrics", e);
    }

    // has this message already expired? expiration time == 0 means never expires
    // TODO: explain why this is necessary; normally it is the responsibility of the provider to handle message expiration
    try {
        long expiryTime = message.getJMSExpiration();
        if (expiryTime > 0 && System.currentTimeMillis() > expiryTime) {
            if (log.isDebugEnabled()) {
                log.debug("Discard expired message with ID : " + message.getJMSMessageID());
            }
            return true;
        }
    } catch (JMSException ignore) {
    }

    boolean successful = false;
    try {
        successful = processThoughEngine(message, ut);

    } catch (JMSException e) {
        log.error("JMS Exception encountered while processing", e);
    } catch (AxisFault e) {
        log.error("Axis fault processing message", e);
    } catch (Exception e) {
        log.error("Unknown error processing message", e);

    } finally {
        if (successful) {
            metrics.incrementMessagesReceived();
        } else {
            metrics.incrementFaultsReceiving();
        }
    }

    return successful;
}

From source file:org.apache.camel.component.jms.JmsProducer.java

protected boolean processInOnly(final Exchange exchange, final AsyncCallback callback) {
    final org.apache.camel.Message in = exchange.getIn();

    String destinationName = in.getHeader(JmsConstants.JMS_DESTINATION_NAME, String.class);
    if (destinationName != null) {
        // remove the header so it wont be propagated
        in.removeHeader(JmsConstants.JMS_DESTINATION_NAME);
    }/*from   w  w  w .  j av  a  2  s .  com*/
    if (destinationName == null) {
        destinationName = endpoint.getDestinationName();
    }

    Destination destination = in.getHeader(JmsConstants.JMS_DESTINATION, Destination.class);
    if (destination != null) {
        // remove the header so it wont be propagated
        in.removeHeader(JmsConstants.JMS_DESTINATION);
    }
    if (destination == null) {
        destination = endpoint.getDestination();
    }
    if (destination != null) {
        // prefer to use destination over destination name
        destinationName = null;
    }
    final String to = destinationName != null ? destinationName : "" + destination;

    MessageCreator messageCreator = new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
            Message answer = endpoint.getBinding().makeJmsMessage(exchange, in, session, null);

            // when in InOnly mode the JMSReplyTo is a bit complicated
            // we only want to set the JMSReplyTo on the answer if
            // there is a JMSReplyTo from the header/endpoint and
            // we have been told to preserveMessageQos

            Object jmsReplyTo = answer.getJMSReplyTo();
            if (endpoint.isDisableReplyTo()) {
                // honor disable reply to configuration
                if (LOG.isDebugEnabled()) {
                    LOG.debug("ReplyTo is disabled on endpoint: " + endpoint);
                }
                answer.setJMSReplyTo(null);
            } else {
                // if the binding did not create the reply to then we have to try to create it here
                if (jmsReplyTo == null) {
                    // prefer reply to from header over endpoint configured
                    jmsReplyTo = exchange.getIn().getHeader("JMSReplyTo", String.class);
                    if (jmsReplyTo == null) {
                        jmsReplyTo = endpoint.getReplyTo();
                    }
                }
            }

            // we must honor these special flags to preserve QoS
            // as we are not OUT capable and thus do not expect a reply, and therefore
            // the consumer of this message should not return a reply so we remove it
            // unless we use preserveMessageQos=true to tell that we still want to use JMSReplyTo
            if (jmsReplyTo != null && !(endpoint.isPreserveMessageQos() || endpoint.isExplicitQosEnabled())) {
                // log at debug what we are doing, as higher level may cause noise in production logs
                // this behavior is also documented at the camel website
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Disabling JMSReplyTo: " + jmsReplyTo + " for destination: " + to
                            + ". Use preserveMessageQos=true to force Camel to keep the JMSReplyTo on endpoint: "
                            + endpoint);
                }
                jmsReplyTo = null;
            }

            // the reply to is a String, so we need to look up its Destination instance
            // and if needed create the destination using the session if needed to
            if (jmsReplyTo != null && jmsReplyTo instanceof String) {
                // must normalize the destination name
                String replyTo = normalizeDestinationName((String) jmsReplyTo);
                // we need to null it as we use the String to resolve it as a Destination instance
                jmsReplyTo = null;

                // try using destination resolver to lookup the destination
                if (endpoint.getDestinationResolver() != null) {
                    jmsReplyTo = endpoint.getDestinationResolver().resolveDestinationName(session, replyTo,
                            endpoint.isPubSubDomain());
                }
                if (jmsReplyTo == null) {
                    // okay then fallback and create the queue
                    if (endpoint.isPubSubDomain()) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Creating JMSReplyTo topic: " + replyTo);
                        }
                        jmsReplyTo = session.createTopic(replyTo);
                    } else {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Creating JMSReplyTo queue: " + replyTo);
                        }
                        jmsReplyTo = session.createQueue(replyTo);
                    }
                }
            }

            // set the JMSReplyTo on the answer if we are to use it
            Destination replyTo = null;
            if (jmsReplyTo instanceof Destination) {
                replyTo = (Destination) jmsReplyTo;
            }
            if (replyTo != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Using JMSReplyTo destination: " + replyTo);
                }
                answer.setJMSReplyTo(replyTo);
            } else {
                // do not use JMSReplyTo
                answer.setJMSReplyTo(null);
            }

            return answer;
        }
    };

    doSend(false, destinationName, destination, messageCreator, null);

    // after sending then set the OUT message id to the JMSMessageID so its identical
    setMessageId(exchange);

    // we are synchronous so return true
    callback.done(true);
    return true;
}

From source file:nl.nn.adapterframework.extensions.ifsa.jms.IfsaFacade.java

/**
 * Intended for server-side reponse sending and implies that the received
 * message *always* contains a reply-to address.
 *///from w  ww .j a  v a  2  s.  co  m
public void sendReply(QueueSession session, Message received_message, String response) throws IfsaException {
    QueueSender tqs = null;
    try {
        TextMessage answer = session.createTextMessage();
        answer.setText(response);
        Queue replyQueue = (Queue) received_message.getJMSReplyTo();
        tqs = session.createSender(replyQueue);
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "sending reply to [" + received_message.getJMSReplyTo() + "]");
        ((IFSAServerQueueSender) tqs).sendReply(received_message, answer);
    } catch (Throwable t) {
        throw new IfsaException(t);
    } finally {
        if (tqs != null) {
            try {
                tqs.close();
            } catch (JMSException e) {
                log.warn(getLogPrefix() + "exception closing reply queue sender", e);
            }
        }
    }
}