Example usage for javax.jms TopicSession createProducer

List of usage examples for javax.jms TopicSession createProducer

Introduction

In this page you can find the example usage for javax.jms TopicSession createProducer.

Prototype


MessageProducer createProducer(Destination destination) throws JMSException;

Source Link

Document

Creates a MessageProducer to send messages to the specified destination.

Usage

From source file:eu.eubrazilcc.lvl.storage.activemq.ActiveMQConnector.java

public void sendMessage(final String topicName, final String message) {
    checkArgument(isNotBlank(topicName), "Uninitialized or invalid topic");
    checkArgument(isNotBlank(message), "Uninitialized or invalid message");
    TopicConnection conn = null;/*  w w w  . j  av  a2 s.  com*/
    TopicSession session = null;
    MessageProducer producer = null;
    try {
        conn = (TopicConnection) broker().getProducersConnFactory().createConnection();
        /* conn = broker().getConnFactory().createTopicConnection();
        conn.start(); */
        session = conn.createTopicSession(false, AUTO_ACKNOWLEDGE);
        final Topic topic = session.createTopic(topicName);
        producer = session.createProducer(topic);
        producer.setDeliveryMode(NON_PERSISTENT);
        final TextMessage textMessage = session.createTextMessage(message);
        producer.send(textMessage);
    } catch (JMSException e) {
        LOGGER.error("Failed to send message to topic: " + topicName, e);
    } finally {
        if (producer != null) {
            try {
                producer.close();
            } catch (JMSException ignore) {
            }
        }
        if (session != null) {
            try {
                session.close();
            } catch (JMSException ignore) {
            }
        }
        /* if (conn != null) {
           try {
              conn.close();
           } catch (JMSException ignore) { }
        } */
    }
}

From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java

private void produceExpiredAndOneNonExpiredMessages() throws JMSException {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName);
    TopicConnection connection = connectionFactory.createTopicConnection();
    TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic(topicName);
    MessageProducer producer = session.createProducer(topic);
    producer.setTimeToLive(TimeUnit.SECONDS.toMillis(1));
    for (int i = 0; i < 40000; i++) {
        sendRandomMessage(session, producer);
    }//from  w  w  w.ja  v  a2s  .c  om
    producer.setTimeToLive(TimeUnit.DAYS.toMillis(1));
    sendRandomMessage(session, producer);
    connection.close();
    LOG.info("produceExpiredAndOneNonExpiredMessages done");
}