Example usage for javax.jms Session createTextMessage

List of usage examples for javax.jms Session createTextMessage

Introduction

In this page you can find the example usage for javax.jms Session createTextMessage.

Prototype


TextMessage createTextMessage(String text) throws JMSException;

Source Link

Document

Creates an initialized TextMessage object.

Usage

From source file:org.hoteia.qalingo.core.jms.syncserveur.producer.SyncServeurStatusMessageProducer.java

/**
 * Generates JMS messages// w w w .ja  va  2  s.  com
 * 
 */
public void generateAndSendMessages(final SyncServeurMessageJms syncServeurMessageJms) {
    try {
        final String valueJMSMessage = xmlMapper.getXmlMapper().writeValueAsString(syncServeurMessageJms);

        if (StringUtils.isNotEmpty(valueJMSMessage)) {
            jmsTemplate.send(new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                    TextMessage message = session.createTextMessage(valueJMSMessage);
                    if (logger.isDebugEnabled()) {
                        logger.info("Sending JMS message: " + valueJMSMessage);
                    }
                    return message;
                }
            });
        } else {
            logger.warn("SyncServeur Jms Message is empty");
        }

    } catch (JmsException e) {
        logger.error("Exception during create/send message process");
    } catch (JsonProcessingException e) {
        logger.error("Exception during build message process");
    }
}

From source file:org.jbpm.executor.impl.ExecutorImpl.java

protected void sendMessage(String messageBody, int priority) {
    if (connectionFactory == null && queue == null) {
        throw new IllegalStateException("ConnectionFactory and Queue cannot be null");
    }//from  w ww  .ja va  2  s.  c o  m
    Connection queueConnection = null;
    Session queueSession = null;
    MessageProducer producer = null;
    try {
        queueConnection = connectionFactory.createConnection();
        queueSession = queueConnection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);

        TextMessage message = queueSession.createTextMessage(messageBody);
        producer = queueSession.createProducer(queue);
        producer.setPriority(priority);

        queueConnection.start();

        producer.send(message);
    } catch (Exception e) {
        throw new RuntimeException("Error when sending JMS message with executor job request", e);
    } finally {
        if (producer != null) {
            try {
                producer.close();
            } catch (JMSException e) {
                logger.warn("Error when closing producer", e);
            }
        }

        if (queueSession != null) {
            try {
                queueSession.close();
            } catch (JMSException e) {
                logger.warn("Error when closing queue session", e);
            }
        }

        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e) {
                logger.warn("Error when closing queue connection", e);
            }
        }
    }
}

From source file:org.marketcetera.client.jms.JMSXMLMessageConverter.java

/**
 * Converts a messaging object to a JMS Message.
 *
 * @param inObject the message to be converted. It should either be
 * an order or a report./*  w  w w  .j  a v a  2 s . c  om*/
 * @param session the JMS Session instance.
 *
 * @return the JMS message.
 *
 * @throws javax.jms.JMSException if there were errors serializing the
 * messaging object.
 * @throws org.springframework.jms.support.converter.MessageConversionException if the supplied object was not
 * an acceptable messaging object.
 */
@Override
public Message toMessage(Object inObject, Session session) throws JMSException, MessageConversionException {
    SLF4JLoggerProxy.debug(this, "Converting to JMS {}", inObject); //$NON-NLS-1$
    if ((inObject instanceof ReportBaseImpl) || (inObject instanceof FIXResponseImpl)
            || (inObject instanceof OrderEnvelope) || (inObject instanceof BrokerStatus)) {
        try {
            TextMessage message = session.createTextMessage(toXML(inObject));
            //Set the type property for interoperability with .NET client.
            message.setStringProperty(JMS_TYPE_PROPERTY, inObject.getClass().getSimpleName());
            return message;
        } catch (JAXBException e) {
            throw new MessageConversionException(new I18NBoundMessage1P(
                    Messages.ERROR_CONVERTING_OBJECT_TO_MESSAGE, ObjectUtils.toString(inObject)).getText(), e);
        }
    } else {
        throw new MessageConversionException(
                new I18NBoundMessage1P(Messages.UNEXPECTED_MESSAGE_TO_SEND, ObjectUtils.toString(inObject))
                        .getText());
    }
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

private static Message stringToMessage(String value, Session session) throws JMSException {
    return session.createTextMessage(value);
}

From source file:org.mule.transport.jms.JmsMessageUtilsTestCase.java

@Test
public void testConvertingStringToTextMessage() throws JMSException {
    String text = "Hello world";

    Session session = mock(Session.class);
    TextMessage textMessage = new ActiveMQTextMessage();
    textMessage.setText(text);//w  w w  .j  a  va  2  s .  com
    when(session.createTextMessage(text)).thenReturn(textMessage);

    TextMessage message = (TextMessage) JmsMessageUtils.toMessage(text, session);
    assertEquals(textMessage, message);
    verify(session, times(1)).createTextMessage(text);
}

From source file:org.openengsb.ports.jms.JMSPortTest.java

private String sendWithTempQueue(final String msg) {
    String resultString = jmsTemplate.execute(new SessionCallback<String>() {
        @Override//www . jav  a 2 s.  c  om
        public String doInJms(Session session) throws JMSException {
            Queue queue = session.createQueue("receive");
            MessageProducer producer = session.createProducer(queue);
            TemporaryQueue tempQueue = session.createTemporaryQueue();
            MessageConsumer consumer = session.createConsumer(tempQueue);
            TextMessage message = session.createTextMessage(msg);
            message.setJMSReplyTo(tempQueue);
            producer.send(message);
            TextMessage response = (TextMessage) consumer.receive(10000);
            assertThat(
                    "server should set the value of the correltion ID to the value of the received message id",
                    response.getJMSCorrelationID(), is(message.getJMSMessageID()));
            JmsUtils.closeMessageProducer(producer);
            JmsUtils.closeMessageConsumer(consumer);
            return response != null ? response.getText() : null;
        }
    }, true);
    return resultString;
}

From source file:org.wso2.carbon.bpmn.extensions.jms.JMSSender.java

/**
 * Sends a message to a queue/topic using shared connections, sessions and message producers
 * @param connectionFactory//from w  ww.j  a v a2  s  .  c o  m
 * @param text
 */

public void sendMessage(JMSConnectionFactory connectionFactory, int cacheLevel, String target, String text) {

    try {

        Connection connection = connectionFactory.getConnection();
        Session session = connectionFactory.getSession(connection);
        Destination destination;

        if (connectionFactory.isQueue()) {
            destination = session.createQueue(target);
            if (destination == null) {
            }
        } else {
            destination = session.createTopic(target);
        }

        if (destination != null) {

            log.info("initializing jms message producer..");
            MessageProducer producer = connectionFactory.getMessageProducer(connection, session, destination);

            JMSMessageSender jmsMessageSender = new JMSMessageSender(connection, session, producer, destination,
                    cacheLevel, connectionFactory.isQueue());
            TextMessage message = session.createTextMessage(text);
            jmsMessageSender.send(message, null);

        } else {
            log.error("Destination not specified");
        }
    } catch (JMSException ex) {
        log.error(ex.getMessage(), ex);
    }
}

From source file:org.wso2.carbon.esb.scenario.test.common.jms.ActiveMQJMSClient.java

/**
 * Function to produce message to ActiveMQ Queue
 *
 * @param queueName name of the target queue
 * @param messageStr message to place//from ww  w  .  j  a v a2 s  . c o m
 * @throws JMSException
 */
public void produceMessageToQueue(String queueName, String messageStr) throws JMSException {

    Connection connection = null;
    Session session = null;

    try {
        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

        // Create a Connection
        connection = connectionFactory.createConnection();
        connection.start();
        connection.setExceptionListener(this);

        // Create a Session
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue(queueName);

        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        // Create a messages
        TextMessage message = session.createTextMessage(messageStr);

        // Tell the producer to send the message
        producer.send(message);

    } finally {
        // Clean up
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.wso2.carbon.event.output.adapter.jms.internal.util.JMSMessageSender.java

public Message convertToJMSMessage(Object messageObj, Map<String, String> messageProperties, Session session) {
    Message jmsMessage = null;/* w w w . j  a va2 s .  co  m*/
    try {
        if (messageObj instanceof OMElement) {
            jmsMessage = session.createTextMessage(messageObj.toString());
        } else if (messageObj instanceof String) {
            jmsMessage = session.createTextMessage((String) messageObj);
        } else if (messageObj instanceof Map) {
            MapMessage mapMessage = session.createMapMessage();
            Map sourceMessage = (Map) messageObj;
            for (Object key : sourceMessage.keySet()) {
                mapMessage.setObject((String) key, sourceMessage.get(key));
            }
            jmsMessage = mapMessage;
        }
    } catch (JMSException e) {
        handleException("Failed to publish to topic:" + messageProperties.get(JMSConstants.PARAM_DESTINATION),
                e);
    }

    return jmsMessage;
}

From source file:org.xerela.zap.jms.EventElf.java

/**
 * Create a TextMessage destined for the specified queue.
 *
 * @param queueName the name of the destination queue
 * @param message the message text/*from  w w w.ja va 2  s . c  om*/
 * @return a TextMessage object
 * @throws Exception thrown if an exception occurs
 */
public static TextMessage createTextMessage(String queueName, String message) throws Exception {
    Session jmsSession = (Session) sessionPool.borrowObject(queueName);
    try {
        return jmsSession.createTextMessage(message);
    } finally {
        sessionPool.returnObject(queueName, jmsSession);
    }
}