Example usage for javax.jms MessageProducer setDeliveryMode

List of usage examples for javax.jms MessageProducer setDeliveryMode

Introduction

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

Prototype


void setDeliveryMode(int deliveryMode) throws JMSException;

Source Link

Document

Sets the producer's default delivery mode.

Usage

From source file:org.dawnsci.commandserver.mx.example.ActiveMQProducer.java

public static void main(String[] args) throws Exception {

    QueueConnectionFactory connectionFactory = ConnectionFactoryFacade
            .createConnectionFactory("tcp://ws097.diamond.ac.uk:61616");
    Connection send = connectionFactory.createConnection();

    Session session = send.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("testQ");

    final MessageProducer producer = session.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);

    Message message = session.createTextMessage("Hello World");
    producer.send(message);/*  ww w .  j  a  v  a  2 s .  c  om*/

    message = session.createTextMessage("...and another message");
    producer.send(message);

    message = session.createObjectMessage(new TestObjectBean("this could be", "anything"));
    producer.send(message);

    // Test JSON
    SweepBean col = new SweepBean("fred", "d0000000001", 0, 100);

    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(col);

    message = session.createTextMessage(jsonString);
    producer.send(message);

    producer.close();
    session.close();
    send.close();

    // Now we peak at the queue
    // If the consumer is not going, the messages should still be there
    if (REQUIRE_PEAK) {
        QueueConnection qCon = connectionFactory.createQueueConnection();
        QueueSession qSes = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queue = qSes.createQueue("testQ");
        qCon.start();

        QueueBrowser qb = qSes.createBrowser(queue);
        Enumeration e = qb.getEnumeration();
        if (e.hasMoreElements())
            System.out.println("Peak at queue:");
        while (e.hasMoreElements()) {
            Message m = (Message) e.nextElement();
            if (m == null)
                continue;
            if (m instanceof TextMessage) {
                TextMessage t = (TextMessage) m;
                System.out.println(t.getText());
            } else if (m instanceof ObjectMessage) {
                ObjectMessage o = (ObjectMessage) m;
                System.out.println(o.getObject());
            }
        }

        qb.close();
        qSes.close();
        qCon.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);//w  w w.j  a  v a  2  s .  c o m
    connection.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);// w ww.j  a v  a 2 s  .c  om
    connection.close();
}

From source file:com.datatorrent.lib.io.jms.JMSTestBase.java

public void produceMsg(String text) throws Exception {
    // Create a ConnectionFactory
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

    // Create a Connection
    Connection connection = connectionFactory.createConnection();
    connection.start();/*from   w  ww.  ja  v a 2 s  .  c o m*/

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

    // Create the destination (Topic or Queue)
    Destination destination = session.createQueue("TEST.FOO");

    // 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(text);
    producer.send(message);

    // Clean up
    session.close();
    connection.close();
}

From source file:org.ahp.core.messaging.AhpJmsProducer.java

public void sendTextMessage(String pTextMessage, AhpJmsDestinationTypes pAhpJmsDestinationTypes,
        AhpJmsDestinationNames pAhpJmsDestinationNames) {
    Connection lConnection = null;
    Session lSession = null;/*  www  . j  a  va2 s . co m*/
    try {
        lConnection = this.mJmsConnectionFactory.createConnection();
        lSession = lConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        Destination lDestination = null;
        if (pAhpJmsDestinationTypes.equals(AhpJmsDestinationTypes.Queue))
            lDestination = lSession.createQueue(pAhpJmsDestinationNames.toString());
        else
            lDestination = lSession.createTopic(pAhpJmsDestinationNames.toString());
        MessageProducer lMessageProducer = lSession.createProducer(lDestination);
        lMessageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        //lMessageProducer.setTimeToLive( timeToLive );
        Message lTextMessage = lSession.createTextMessage(pTextMessage);
        LOGGER.debug("AhpJmsProducer sending message to Destination " + pAhpJmsDestinationNames.toString()
                + " TextMessage :: \n" + pTextMessage);
        lMessageProducer.send(lTextMessage);
        lSession.commit();
    } catch (JMSException exJms) {
        LOGGER.error("", exJms);
    } finally {
        try {
            lConnection.close();
        } catch (JMSException exJms) {
            LOGGER.error("", exJms);
        }
    }
}

From source file:jenkins.plugins.logstash.persistence.ActiveMqDao.java

@Override
public void push(String data) throws IOException {
    TopicConnection connection = null;/*  w w  w. j  a v a  2 s  .co m*/
    Session session = null;
    try {
        // Create a Connection
        connection = connectionFactory.createTopicConnection();

        connection.start();

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

        // Create the destination Queue
        Destination destination = session.createTopic(key);

        // Create the MessageProducer from the Session to the Queue
        MessageProducer producer = session.createProducer(destination);

        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        // Create the message
        TextMessage message = session.createTextMessage(data);
        message.setJMSType("application/json");
        // Tell the producer to send the message
        producer.send(message);

        //logger.log( Level.FINER, String.format("JMS message sent with ID [%s]", message.getJMSMessageID()));

    } catch (JMSException e) {
        logger.log(Level.SEVERE, null != e.getMessage() ? e.getMessage() : e.getClass().getName());
        throw new IOException(e);
    } finally {
        // Clean up
        try {
            if (null != session)
                session.close();
        } catch (JMSException e) {
            logger.log(Level.WARNING, null != e.getMessage() ? e.getMessage() : e.getClass().getName());
        }
        try {
            if (null != connection)
                connection.close();
        } catch (JMSException e) {
            logger.log(Level.WARNING, null != e.getMessage() ? e.getMessage() : e.getClass().getName());
        }
    }
}

From source file:com.datatorrent.lib.io.jms.JMSStringInputOperatorTest.java

private void produceMsg(int numMessages) throws Exception {
    // Create a ConnectionFactory
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

    // Create a Connection
    Connection connection = connectionFactory.createConnection();
    connection.start();/*from   w w w.jav  a 2  s .  com*/

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

    // Create the destination (Topic or Queue)
    Destination destination = session.createQueue("TEST.FOO");

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

    // Create a messages
    String text = "Hello world! From tester producer";
    TextMessage message = session.createTextMessage(text);
    for (int i = 0; i < numMessages; i++) {
        producer.send(message);
    }

    // Clean up
    session.close();
    connection.close();

}

From source file:com.oneops.controller.jms.InductorPublisher.java

private MessageProducer newMessageProducer(String queueName) throws JMSException {
    // Create the session
    Destination destination = session.createQueue(queueName);
    // Create the producer.
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    logger.info("Created message producer for queue " + queueName);
    return producer;
}

From source file:de.klemp.middleware.controller.Controller.java

/**
 * This method creates a connection to the message broker Active MQ and
 * sends the message to the given topic. The method is used by all methods
 * of the second component./*from w  ww .j a va2 s. c  om*/
 * 
 * @param message
 *            send to the output device
 * @param topic
 *            of the message broker
 */
public static void sendMessage(String message, String topic) {
    String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

    // Create a Connection
    Connection connection;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Create the destination (Topic or Queue)
        Destination destination = session.createTopic(topic);
        // 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 message1 = session.createTextMessage(message);
        // Tell the producer to send the message
        producer.send(message1);
        session.close();
        connection.close();
    } catch (JMSException e) {
        logger.error("Message could not be sended to activemq", e);
    }

}

From source file:de.klemp.middleware.controller.Controller.java

public static void sendMessageFast(String message, String topic) {
    String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

    // Create a Connection
    Connection connection;/*from w  ww  .  ja va  2 s . c o m*/
    try {
        connectionFactory.setOptimizeAcknowledge(true);
        connectionFactory.setUseAsyncSend(true);
        connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Create the destination (Topic or Queue)
        Destination destination = session.createTopic(topic);
        // 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 message1 = session.createTextMessage(message);
        // Tell the producer to send the message
        producer.send(message1);
        session.close();
        connection.close();
    } catch (JMSException e) {
        logger.error("Message could not be sended to activemq", e);
    }

}