Example usage for javax.jms DeliveryMode PERSISTENT

List of usage examples for javax.jms DeliveryMode PERSISTENT

Introduction

In this page you can find the example usage for javax.jms DeliveryMode PERSISTENT.

Prototype

int PERSISTENT

To view the source code for javax.jms DeliveryMode PERSISTENT.

Click Source Link

Document

This delivery mode instructs the JMS provider to log the message to stable storage as part of the client's send operation.

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);//from w  w  w  .j a v  a2 s .  c  o m

    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.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);//from www.  j av  a  2s. co  m
    connection.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  ww  .  ja  v a2  s.  c o  m
    connection.close();
}

From source file:com.oneops.inductor.MessagePublisher.java

public void init() {
    try {//from   ww w . j a  v  a 2  s .  c  o  m
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination queue = session.createQueue(topic);
        regularProducer = session.createProducer(queue);
        priorityProducer = session.createProducer(queue);
        priorityProducer.setPriority(6);

        if (persistent) {
            regularProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
            priorityProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            regularProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            priorityProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        if (timeToLive != 0) {
            regularProducer.setTimeToLive(timeToLive);
            priorityProducer.setTimeToLive(timeToLive);
        }
    } catch (JMSException e) {
        logger.error(e.getMessage());
        logger.error(e.getMessage(), e);
    }
    super.init();
}

From source file:com.centurylink.mdw.util.MDWMessageProducer.java

public void sendMessage(String requestMessage, String queueName, String correlationId, final Queue replyQueue)
        throws JMSException {
    sendMessage(requestMessage, queueName, correlationId, replyQueue, 0, DeliveryMode.PERSISTENT);
}

From source file:org.dawnsci.commandserver.core.consumer.RemoteSubmission.java

/**
 * Submits the bean onto the server. From there events about this
 * bean are tacked by monitoring the status queue.
 * //from   ww  w .jav  a  2s  . co m
 * @param uri
 * @param bean
 */
public synchronized TextMessage submit(StatusBean bean, boolean prepareBean) throws Exception {

    if (getQueueName() == null || "".equals(getQueueName()))
        throw new Exception("Please specify a queue name!");

    Connection send = null;
    Session session = null;
    MessageProducer producer = null;

    try {
        QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri);
        send = connectionFactory.createConnection();

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

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

        ObjectMapper mapper = new ObjectMapper();

        if (getTimestamp() < 1)
            setTimestamp(System.currentTimeMillis());
        if (getPriority() < 1)
            setPriority(1);
        if (getLifeTime() < 1)
            setLifeTime(7 * 24 * 60 * 60 * 1000); // 7 days in ms

        if (prepareBean) {
            if (bean.getUserName() == null)
                bean.setUserName(System.getProperty("user.name"));
            bean.setUniqueId(uniqueId);
            bean.setSubmissionTime(getTimestamp());
        }
        String jsonString = mapper.writeValueAsString(bean);

        TextMessage message = session.createTextMessage(jsonString);

        message.setJMSMessageID(uniqueId);
        message.setJMSExpiration(getLifeTime());
        message.setJMSTimestamp(getTimestamp());
        message.setJMSPriority(getPriority());

        producer.send(message);

        return message;

    } finally {
        if (send != null)
            send.close();
        if (session != null)
            session.close();
        if (producer != null)
            producer.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;/*  w w w . ja v  a2  s.c  o  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:eu.domibus.submission.jms.BackendJMSImpl.java

private Boolean submitErrorMessage(String messageId) {
    Connection connection;//from   www.j  a  v  a2 s.c om
    MessageProducer producer;
    List<ErrorLogEntry> errors = this.errorLogDao.getUnnotifiedErrorsForMessage(messageId);

    try {
        connection = this.cf.createConnection();
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(this.receivingQueue);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        final MapMessage resMessage = session.createMapMessage();
        for (int i = 0; i < errors.size(); ++i) {
            resMessage.setString(String.valueOf(i), errors.get(i).toString());
            errors.get(i).setNotified(new Date());
            this.errorLogDao.update(errors.get(i));
        }

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

        connection.close();
    } catch (JMSException e) {
        BackendJMSImpl.LOG.error("", e);
        return false;
    }
    return true;
}

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

@Override
public void push(String data) throws IOException {
    TopicConnection connection = null;//from w  w  w.j av a2  s.com
    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.centurylink.mdw.util.MDWMessageProducer.java

/**
 * @param xml//w  ww  .j  a  v  a 2  s .  c o m
 * @param queueName
 * @param correlationId
 * @param delaySeconds
 * @throws JMSException
 */
public void sendMessage(String message, String queueName, String correlationId, int delaySeconds)
        throws JMSException {
    sendMessage(message, queueName, correlationId, null, delaySeconds, DeliveryMode.PERSISTENT);
}