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:example.tempdest.Consumer.java

public static void main(String[] args) {

    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();//from  w  w w.  ja va2  s. c o m
    }
    System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s");
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
    Connection connection = null;

    try {

        connection = connectionFactory.createConnection();
        connection.start();

        final Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("test-queue");
        MessageConsumer consumer = session.createConsumer(destination);

        int i = 0;
        while (true) {
            Message message = consumer.receive(TIMEOUT);

            if (message != null) {
                if (message instanceof TextMessage) {
                    String text = ((TextMessage) message).getText();
                    System.out.println("Got " + i++ + ". message: " + text);
                    Destination replyTo = message.getJMSReplyTo();
                    MessageProducer producer = session.createProducer(replyTo);
                    producer.send(
                            session.createTextMessage("You made it to the consumer, here is your response"));
                    producer.close();
                }
            } else {
                break;
            }
        }

        consumer.close();
        session.close();

    } catch (Exception e) {
        System.out.println("Caught exception!");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.out.println("Could not close an open connection...");
            }
        }
    }
}

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

public static String messageToString(Message message) throws JMSException {
    StringBuffer result = new StringBuffer();
    // ID & destination
    result.append("id=").append(message.getJMSMessageID()).append(", destination=")
            .append(message.getJMSDestination());
    // replyTo & correlationID
    Destination replyTo = message.getJMSReplyTo();
    if (replyTo != null) {
        result.append(", replyTo=").append(replyTo).append(", correlationId=")
                .append(message.getJMSCorrelationID());
    }/*w ww.  j a  va  2 s  . c  om*/
    // properties
    Enumeration propertyNames = message.getPropertyNames();
    while (propertyNames.hasMoreElements()) {
        String propertyName = (String) propertyNames.nextElement();
        result.append(", ").append(propertyName).append('=').append(message.getObjectProperty(propertyName));
    }
    return result.toString();
}

From source file:org.logicblaze.lingo.jms.JmsTemplateServiceExporter.java

/**
 * Send the given RemoteInvocationResult as a JMS message to the originator
 * //from  w  w w .  ja v a 2  s  . co m
 * @param message
 *            current HTTP message
 * @param result
 *            the RemoteInvocationResult object
 * @throws javax.jms.JMSException
 *             if thrown by trying to send the message
 */
protected void writeRemoteInvocationResult(final Message message, final RemoteInvocationResult result)
        throws JMSException {
    template.send(message.getJMSReplyTo(), new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
            return createResponseMessage(session, message, result);
        }
    });
}

From source file:Supplier.java

public void run() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
    Session session = null;//from  w w  w.j a  v a2s . c o  m
    Destination orderQueue;
    try {
        Connection connection = connectionFactory.createConnection();

        session = connection.createSession(true, Session.SESSION_TRANSACTED);
        orderQueue = session.createQueue(QUEUE);
        MessageConsumer consumer = session.createConsumer(orderQueue);

        connection.start();

        while (true) {
            Message message = consumer.receive();
            MessageProducer producer = session.createProducer(message.getJMSReplyTo());
            MapMessage orderMessage;
            if (message instanceof MapMessage) {
                orderMessage = (MapMessage) message;
            } else {
                // End of Stream
                producer.send(session.createMessage());
                session.commit();
                producer.close();
                break;
            }

            int quantity = orderMessage.getInt("Quantity");
            System.out.println(
                    ITEM + " Supplier: Vendor ordered " + quantity + " " + orderMessage.getString("Item"));

            MapMessage outMessage = session.createMapMessage();
            outMessage.setInt("VendorOrderNumber", orderMessage.getInt("VendorOrderNumber"));
            outMessage.setString("Item", ITEM);

            quantity = Math.min(orderMessage.getInt("Quantity"),
                    new Random().nextInt(orderMessage.getInt("Quantity") * 10));
            outMessage.setInt("Quantity", quantity);

            producer.send(outMessage);
            System.out.println(ITEM + " Supplier: Sent " + quantity + " " + ITEM + "(s)");
            session.commit();
            System.out.println(ITEM + " Supplier: committed transaction");
            producer.close();
        }
        connection.close();
    } catch (JMSException e) {
        e.printStackTrace();
    }
}

From source file:org.calrissian.mango.jms.stream.JmsFileSenderListener.java

@Override
public void onMessage(Message request) {
    Request req;// w  w w .ja v a  2 s. c  o m
    try {
        req = fromRequestMessage(request);
        taskExecutor.execute(new JmsFileSenderRunnable(req, request.getJMSReplyTo()));
    } catch (Exception e1) {
        throw new RuntimeException(e1);
    }

}

From source file:com.cognifide.aet.runner.distribution.RunnerMessageListener.java

private void sendFatalMessage(Message wrapperMessage, String message) {
    try {/*w w  w  . j  a  va2s .co  m*/
        final Destination jmsReplyTo = wrapperMessage.getJMSReplyTo();
        if (jmsReplyTo != null) {
            final MessageProducer producer = session.createProducer(jmsReplyTo);
            final FatalErrorMessage errorMessage = new FatalErrorMessage(
                    "Failed to process test suite: " + message, wrapperMessage.getJMSCorrelationID());
            producer.send(session.createObjectMessage(errorMessage));
            producer.close();
        }
    } catch (Exception e) {
        LOGGER.error("Fatal exception, can't deliver message {}!", message, e);
    }
}

From source file:com.fusesource.forge.jmstest.tests.AsyncConsumer.java

public void onMessage(Message msg) {
    if (receiveCount != null) {
        receiveCount.incrementAndGet();//ww  w. ja  v a  2 s  .co  m
    }
    MessageProducer producer = null;
    try {
        Destination replyDest = msg.getJMSReplyTo();
        if (replyDest != null) {
            Message response = getSession().createTextMessage("Response");
            response.setStringProperty("ServedBy", getName());
            response.setJMSCorrelationID(msg.getJMSCorrelationID());
            for (Enumeration<?> en = msg.getPropertyNames(); en.hasMoreElements();) {
                String key = (String) en.nextElement();
                Object value = msg.getObjectProperty(key);
                if (key.equals("BrokerStamp")) {
                    value = value.toString() + " --";
                }
                response.setObjectProperty(key, value);
            }
            producer = getSession().createProducer(replyDest);
            producer.send(response);
        }
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        if (producer != null) {
            try {
                producer.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@SuppressWarnings("resource")
@Test//  ww w.ja  v a2  s . co m
public void messageCorrelationBasedOnRequestMessageId() throws Exception {
    ActiveMqTestUtils.prepare();

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "producer-temp-reply-consumers.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
    final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);

    final Destination requestDestination = context.getBean("siOutQueue", Destination.class);

    new Thread(() -> {
        final Message requestMessage = jmsTemplate.receive(requestDestination);
        Destination replyTo = null;
        try {
            replyTo = requestMessage.getJMSReplyTo();
        } catch (Exception e) {
            fail();
        }
        jmsTemplate.send(replyTo, (MessageCreator) session -> {
            try {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSMessageID());
                return message;
            } catch (Exception e) {
                // ignore
            }
            return null;
        });
    }).start();
    gateway.exchange(new GenericMessage<String>("foo"));
    context.close();
}

From source file:com.cognifide.aet.runner.distribution.RunnerMessageListener.java

private void processTestSuite(Message wrapperMessage, TaskMessage message) {
    Suite suite = (Suite) message.getData();
    try {/*ww  w . j  av a  2 s  .  com*/
        boolean isMaintenanceMessage = StringUtils.endsWith(wrapperMessage.getJMSDestination().toString(),
                maintenanceDestinationName);
        testRunProcessor.process(suite, wrapperMessage.getJMSReplyTo(), isMaintenanceMessage);
    } catch (JMSException e) {
        LOGGER.error("Error wile processing RUN {}: ", suite.getCorrelationId(), e);
        sendFatalMessage(wrapperMessage, e.getMessage());
    } catch (StorageException e) {
        LOGGER.error("Failed to process test suite", e);
        sendFatalMessage(wrapperMessage, e.getMessage());
    }
}

From source file:org.logicblaze.lingo.jms.JmsServiceExporterMessageListener.java

protected Object createRemoteProxy(Message message, Class parameterType, Object argument) throws JMSException {
    JmsProxyFactoryBean factory = new JmsProxyFactoryBean();
    factory.setDestination(message.getJMSReplyTo());
    String correlationID = (String) argument;
    if (log.isDebugEnabled()) {
        log.debug("Creating a server side remote proxy for correlationID: " + correlationID);
    }/*w w w.  j a  v  a  2 s  .  c om*/
    factory.setCorrelationID(correlationID);
    factory.setMarshaller(getMarshaller());
    factory.setRemoteInvocationFactory(invocationFactory);
    factory.setServiceInterface(parameterType);
    factory.setRequestor(responseRequestor);
    factory.afterPropertiesSet();
    return factory.getObject();
}