Example usage for javax.jms MessageProducer send

List of usage examples for javax.jms MessageProducer send

Introduction

In this page you can find the example usage for javax.jms MessageProducer send.

Prototype


void send(Message message) throws JMSException;

Source Link

Document

Sends a message using the MessageProducer 's default delivery mode, priority, and time to live.

Usage

From source file:org.jbpm.bpel.tutorial.invoice.ComputePricePT_Impl.java

protected void sendInvoiceMessage(float shippingPrice) throws JMSException {
    ServletContext servletContext = endpointContext.getServletContext();
    Integer orderId = (Integer) servletContext.getAttribute(ORDER_ID_ATTR);
    Float linePrice = (Float) servletContext.getAttribute(LINE_PRICE_ATTR);
    float amount = linePrice.floatValue() + shippingPrice;

    // create a session
    Session jmsSession = jmsConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    try {//  w  w w . jav a 2  s.  c  om
        // create the message
        MapMessage invoiceMessage = jmsSession.createMapMessage();
        invoiceMessage.setInt("orderId", orderId.intValue());
        invoiceMessage.setFloat("amount", amount);

        // send it!
        MessageProducer producer = jmsSession.createProducer(invoiceDestination);
        producer.send(invoiceMessage);

        log.debug("Sent invoice message: orderId=" + orderId + ", amount=" + amount);
    } finally {
        jmsSession.close();
    }
}

From source file:org.jbpm.bpel.tutorial.shipping.ShippingPT_Impl.java

protected void sendShippingMessage(String customerId) throws JMSException {
    // create a session
    Session jmsSession = jmsConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    try {//  w ww  .  jav  a2s.  c  o  m
        // create the message
        MapMessage message = jmsSession.createMapMessage();
        message.setString("customerId", customerId);

        // send it!
        MessageProducer producer = jmsSession.createProducer(shippingDestination);
        producer.send(message);

        log.debug("Sent shipping message: customerId=" + customerId);
    } finally {
        jmsSession.close();
    }
}

From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java

public void testBrowseReceive() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");

    connection.start();/*from  w  ww  .jav a 2 s.c om*/

    Message[] outbound = new Message[] { session.createTextMessage("First Message"),
            session.createTextMessage("Second Message"), session.createTextMessage("Third Message") };

    MessageProducer producer = session.createProducer(destination);
    producer.send(outbound[0]);

    // create browser first
    QueueBrowser browser = session.createBrowser((Queue) destination);
    Enumeration enumeration = browser.getEnumeration();

    // create consumer
    MessageConsumer consumer = session.createConsumer(destination);

    // browse the first message
    assertTrue("should have received the first message", enumeration.hasMoreElements());
    assertEquals(outbound[0], (Message) enumeration.nextElement());

    // Receive the first message.
    assertEquals(outbound[0], consumer.receive(1000));
    consumer.close();
    browser.close();
    producer.close();

}

From source file:ubic.gemma.job.grid.util.JMSBrokerMonitorImpl.java

private MapMessage sendTaskSubmissionQueueDiagnosticMessage() throws JMSException {
    MapMessage reply = jmsTemplate.execute(new SessionCallback<MapMessage>() {
        @Override//  ww w  .ja v  a2  s.c  o  m
        public MapMessage doInJms(Session session) throws JMSException {
            Queue replyTo = session.createTemporaryQueue();
            Message message = session.createMessage();
            message.setJMSReplyTo(replyTo);
            Queue queryQueue = session.createQueue("ActiveMQ.Statistics.Destination.tasks.submit");
            MessageProducer producer = session.createProducer(queryQueue);
            MessageConsumer consumer = session.createConsumer(replyTo);
            producer.send(message);
            return (MapMessage) consumer.receive(5000);
        }
    }, true);
    return reply;
}

From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java

public void testBrowseClose() throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQQueue destination = new ActiveMQQueue("TEST");

    connection.start();//w ww.ja v  a 2  s  .  c  o  m

    TextMessage[] outbound = new TextMessage[] { session.createTextMessage("First Message"),
            session.createTextMessage("Second Message"), session.createTextMessage("Third Message") };

    MessageProducer producer = session.createProducer(destination);
    producer.send(outbound[0]);
    producer.send(outbound[1]);
    producer.send(outbound[2]);

    // create browser first
    QueueBrowser browser = session.createBrowser((Queue) destination);
    Enumeration enumeration = browser.getEnumeration();

    // browse some messages
    assertEquals(outbound[0], (Message) enumeration.nextElement());
    assertEquals(outbound[1], (Message) enumeration.nextElement());
    //assertEquals(outbound[2], (Message) enumeration.nextElement());

    browser.close();

    // create consumer
    MessageConsumer consumer = session.createConsumer(destination);

    // Receive the first message.
    TextMessage msg = (TextMessage) consumer.receive(1000);
    assertEquals("Expected " + outbound[0].getText() + " but received " + msg.getText(), outbound[0], msg);
    msg = (TextMessage) consumer.receive(1000);
    assertEquals("Expected " + outbound[1].getText() + " but received " + msg.getText(), outbound[1], msg);
    msg = (TextMessage) consumer.receive(1000);
    assertEquals("Expected " + outbound[2].getText() + " but received " + msg.getText(), outbound[2], msg);

    consumer.close();
    producer.close();

}

From source file:sk.seges.test.jms.multiple.QueueSendReceiveTest.java

@Test
public void testSend() throws Exception {
    Connection connection = testConnectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(testQueueA);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    TextMessage message = session.createTextMessage("test text");
    producer.send(message);
    connection.close();/* w  w  w  . j a  v a 2 s. c  o  m*/
}

From source file:org.fusesource.stompjms.JmsTestSupport.java

protected void sendMessages(Session session, Destination destination, int count) throws JMSException {
    MessageProducer producer = session.createProducer(destination);
    for (int i = 0; i < count; i++) {
        producer.send(session.createTextMessage(messageTextPrefix + i));
    }//from  w  w w  . j a  v a 2s.c  o m
    producer.close();
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueSendReceiveTest.java

@Test
public void testSend() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(activeMQTestQueueA);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    TextMessage message = session.createTextMessage("test text");
    producer.send(message);
    connection.close();//from   w  ww .  jav a 2  s.  c o  m
}

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

private void sendResult(Serializable result, Destination destination, String correlationId)
        throws JMSException {
    if (log.isDebugEnabled())
        log.debug("sending " + result + " to " + destination);

    Connection jmsConnection = jmsConnectionFactory.createConnection();
    try {//  www  .  j a v a2 s. c  o m
        /*
         * if the connection supports xa, the session will be transacted, else the session will
         * auto acknowledge; in either case no explicit transaction control must be performed -
         * see ejb 2.1 - 17.3.5
         */
        Session jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Message resultMessage = jmsSession.createObjectMessage(result);
        resultMessage.setJMSCorrelationID(correlationId);
        MessageProducer producer = jmsSession.createProducer(destination);
        producer.send(resultMessage);
    } finally {
        // there is no need to close the sessions and producers of a closed connection
        // http://download.oracle.com/javaee/1.4/api/javax/jms/Connection.html#close()
        try {
            jmsConnection.close();
        } catch (JMSException e) {
            log.warn("failed to close jms connection", e);
        }
    }
}

From source file:com.fusesource.forge.jmstest.benchmark.command.transport.JMSCommandTransport.java

public void sendCommand(BenchmarkCommand command) {
    log.debug("Sending command message: " + command.toString());
    if (getConnection() != null) {
        try {//from w  w  w .  java2s  .  c o  m
            Session session = getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination dest = getJmsDestinationProvider().getDestination(session, getDestinationName());
            MessageProducer producer = session.createProducer(dest);
            producer.send(session.createObjectMessage((Serializable) command));
            session.close();
        } catch (Exception je) {
            log().error("Could not send Command " + command.toString() + ".", je);
        }
    } else {
        log.warn("Command not sent as JMS connection is not established.");
    }
}