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.mule.transport.jms.integration.AbstractJmsFunctionalTestCase.java

public void send(Scenario scenario) throws Exception {
    Connection connection = null;
    try {/*from   w w w .  ja v  a 2  s . c  om*/
        connection = getConnection(false, false);
        connection.start();
        Session session = null;
        try {
            session = connection.createSession(scenario.isTransacted(), scenario.getAcknowledge());
            Destination destination = createInputDestination(session, scenario);
            MessageProducer producer = null;
            try {
                producer = session.createProducer(destination);
                if (scenario.isPersistent()) {
                    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                }
                scenario.send(session, producer);
            } finally {
                if (producer != null) {
                    producer.close();
                }
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

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

public void send(MessageProducer producer, Message message, boolean persistent, int priority, long ttl,
        boolean topic, ImmutableEndpoint endpoint) throws JMSException {
    producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl);
}

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

public void send(MessageProducer producer, Message message, Destination dest, boolean persistent, int priority,
        long ttl, boolean topic, ImmutableEndpoint endpoint) throws JMSException {
    producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority,
            ttl);/* w  w  w  .j  av  a 2 s .  co  m*/
}

From source file:org.nuxeo.ecm.core.jms.CoreEventPublisher.java

public void publish(Object content, Topic topic, MessageFactory factory, String eventId) throws JMSException {
    TopicConnection connection = null;// w w  w .  ja va 2  s . co m
    TopicSession session = null;
    TopicPublisher publisher = null;
    try {
        // get a connection from topic connection pool
        connection = getTopicConnection();

        // create a not transacted session
        session = connection.createTopicSession(transacted, TopicSession.AUTO_ACKNOWLEDGE);

        // create the publisher
        publisher = session.createPublisher(topic);
        publisher.setDeliveryMode(isDeliveryPersistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
        publisher.setDisableMessageID(isDisableMessageID);
        publisher.setDisableMessageTimestamp(isDisableMessageTimestamp);
        // create the message using the given factory
        Message msg = factory.createMessage(session, content);
        if (eventId != null) {
            msg.setStringProperty("NuxeoEventId", eventId);
        }
        // publish the message
        publisher.publish(topic, msg);
    } finally {
        if (publisher != null) {
            publisher.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.perfcake.message.sender.JmsSenderTest.java

@Test
public void testBasicSend() throws Exception {
    final Properties props = new Properties();
    props.setProperty("messageType", "STRING");
    props.setProperty("target", "queue/test");

    final JmsSender sender = (JmsSender) ObjectFactory.summonInstance(JmsSender.class.getName(), props);

    Assert.assertEquals(sender.getMessageType(), JmsSender.MessageType.STRING);
    Assert.assertEquals(sender.getTarget(), "queue/test");
    Assert.assertEquals(sender.isPersistent(), true);
    Assert.assertEquals(sender.isTransacted(), false);

    try {/*from w  ww  .  ja v  a  2 s.  co  m*/
        sender.init();

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));

        // STRING Type
        final org.perfcake.message.Message message = new org.perfcake.message.Message();
        final String payload1 = "Hello World!";
        message.setPayload(payload1);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        Message response = JmsHelper.readMessage(factory, 500, queue);
        Assert.assertEquals(response.getJMSDeliveryMode(), DeliveryMode.PERSISTENT);
        Assert.assertTrue(response instanceof TextMessage);
        Assert.assertEquals(((TextMessage) response).getText(), payload1);

        // OBJECT Type
        sender.setMessageType(JmsSender.MessageType.OBJECT);
        final Long payload2 = 42L;
        message.setPayload(payload2);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        response = JmsHelper.readMessage(factory, 500, queue);
        Assert.assertTrue(response instanceof ObjectMessage);
        Assert.assertTrue(((ObjectMessage) response).getObject() instanceof Long);
        Assert.assertEquals((Long) ((ObjectMessage) response).getObject(), payload2);

        // BYTEARRAY Type
        sender.setMessageType(JmsSender.MessageType.BYTEARRAY);
        message.setPayload(payload1);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        response = JmsHelper.readMessage(factory, 500, queue);
        Assert.assertTrue(response instanceof BytesMessage);
        Assert.assertEquals(((BytesMessage) response).readUTF(), payload1);

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, queue));

    } finally {
        sender.close();
    }
}

From source file:org.perfcake.message.sender.JmsSenderTest.java

@Test
public void testTransactedSecuredSend() throws Exception {
    final String payload = "Hello my secret World!";

    final Properties props = new Properties();
    props.setProperty("messagetType", "STRING");
    props.setProperty("target", "queue/secured_test");
    props.setProperty("username", "frank");
    props.setProperty("password", "frank");

    final JmsSender sender = (JmsSender) ObjectFactory.summonInstance(JmsSender.class.getName(), props);

    Assert.assertEquals(sender.getUsername(), "frank");
    Assert.assertEquals(sender.getPassword(), "frank");

    sender.setTransacted(true);/*from w ww. j av a 2  s  . c  o  m*/

    try {
        sender.init();

        // make sure the destination is empty
        Assert.assertNull(JmsHelper.readMessage(factory, 500, securedQueue));

        // STRING Type
        final org.perfcake.message.Message message = new org.perfcake.message.Message();
        message.setPayload(payload);
        sender.preSend(message, null, null);
        sender.send(message, null);
        sender.postSend(message);

        // make sure the destination is empty because the message is not yet commited (done in close())
        Assert.assertNull(JmsHelper.readMessage(factory, 500, securedQueue));
    } finally {
        sender.close();
    }

    final Message response = JmsHelper.readMessage(factory, 500, securedQueue);
    Assert.assertEquals(response.getJMSDeliveryMode(), DeliveryMode.PERSISTENT);
    Assert.assertTrue(response instanceof TextMessage);
    Assert.assertEquals(((TextMessage) response).getText(), payload);

    // make sure the destination is empty
    Assert.assertNull(JmsHelper.readMessage(factory, 500, securedQueue));
}

From source file:org.springframework.integration.jms.ChannelPublishingJmsMessageListener.java

/**
 * Specify the delivery mode for JMS reply Messages.
 * @see javax.jms.MessageProducer#setDeliveryMode(int)
 *//*ww w.ja va  2 s . c  om*/
public void setReplyDeliveryPersistent(boolean replyDeliveryPersistent) {
    this.replyDeliveryMode = replyDeliveryPersistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT;
}

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

/**
 * Perform actual send of JMS message to the Destination selected
 *
 * @param message the JMS message// ww  w .j  av a 2  s  .  c  om
 */
public void send(Message message, MessageContext msgCtx) {

    Boolean jtaCommit = null;
    Boolean rollbackOnly = null;
    Boolean persistent = null;
    Integer priority = null;
    Integer timeToLive = null;
    if (msgCtx != null) {

        jtaCommit = getBooleanProperty(msgCtx, BaseConstants.JTA_COMMIT_AFTER_SEND);
        rollbackOnly = getBooleanProperty(msgCtx, BaseConstants.SET_ROLLBACK_ONLY);
        persistent = getBooleanProperty(msgCtx, JMSConstants.JMS_DELIVERY_MODE);
        priority = getIntegerProperty(msgCtx, JMSConstants.JMS_PRIORITY);
        timeToLive = getIntegerProperty(msgCtx, JMSConstants.JMS_TIME_TO_LIVE);

    }
    // Do not commit, if message is marked for rollback
    if (rollbackOnly != null && rollbackOnly) {
        jtaCommit = Boolean.FALSE;
    }

    if (persistent != null) {
        try {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } catch (JMSException e) {
            log.error("Error setting JMS Producer for PERSISTENT delivery", e);
        }
    }
    if (priority != null) {
        try {
            producer.setPriority(priority);
        } catch (JMSException e) {
            log.error("Error setting JMS Producer priority to : " + priority, e);
        }
    }
    if (timeToLive != null) {
        try {
            producer.setTimeToLive(timeToLive);
        } catch (JMSException e) {
            log.error("Error setting JMS Producer TTL to : " + timeToLive, e);
        }
    }

    boolean sendingSuccessful = false;
    // perform actual message sending
    log.info("sending JMS message..");
    try {
        if (isQueue == null) {
            producer.send(message);

        } else {
            if (isQueue) {
                try {
                    ((QueueSender) producer).send(message);
                } catch (JMSException e) {
                    createTempQueueConsumer();
                    ((QueueSender) producer).send(message);
                }

            } else {
                try {
                    ((TopicPublisher) producer).publish(message);
                } catch (JMSException e) {
                    createTempTopicSubscriber();
                    ((TopicPublisher) producer).publish(message);
                }
            }
        }

        // set the actual MessageID to the message context for use by any others down the line
        //String msgId = null;
        //            try {
        //                msgId = message.getJMSMessageID();
        //                if (msgId != null) {
        //                    msgCtx.setProperty(JMSConstants.JMS_MESSAGE_ID, msgId);
        //                }
        //            } catch (JMSException ignore) {}

        sendingSuccessful = true;
        log.info("Message sent");

        //            if (log.isDebugEnabled()) {
        //                log.debug("Sent Message Context ID : " + msgCtx.getMessageID() +
        //                        " with JMS Message ID : " + msgId +
        //                        " to destination : " + producer.getDestination());
        //            }

    } catch (JMSException e) {
        log.error("Error sending message to destination : " + destination, e);

    } finally {

        if (jtaCommit != null) {

            UserTransaction ut = (UserTransaction) msgCtx.getProperty(BaseConstants.USER_TRANSACTION);
            if (ut != null) {

                try {
                    if (sendingSuccessful && jtaCommit) {
                        ut.commit();
                    } else {
                        ut.rollback();
                    }
                    msgCtx.removeProperty(BaseConstants.USER_TRANSACTION);

                    if (log.isDebugEnabled()) {
                        log.debug((sendingSuccessful ? "Committed" : "Rolled back") + " JTA Transaction");
                    }

                } catch (Exception e) {
                    log.error("Error committing/rolling back JTA transaction after "
                            + "sending of message with MessageContext ID : " + msgCtx.getMessageID()
                            + " to destination : " + destination, e);
                }
            }

        } else {
            try {
                if (session.getTransacted()) {
                    if (sendingSuccessful && (rollbackOnly == null || !rollbackOnly)) {
                        session.commit();
                    } else {
                        session.rollback();
                    }
                }

                if (log.isDebugEnabled()) {
                    log.debug((sendingSuccessful ? "Committed" : "Rolled back")
                            + " local (JMS Session) Transaction");
                }

            } catch (JMSException e) {
                log.error("Error committing/rolling back local (i.e. session) "
                        + "transaction after sending of message with MessageContext ID : "
                        + msgCtx.getMessageID() + " to destination : " + destination, e);
            }
        }
    }
}

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

/**
 * Perform actual send of JMS message to the Destination selected
 *///from   w  ww.  j  ava  2  s.  c  o  m
public void send(Object message, JMSEventAdapter.PublisherDetails publisherDetails, String jmsHeaders) {

    Map<String, String> messageProperties = publisherDetails.getMessageConfig();

    Boolean jtaCommit = getBooleanProperty(messageProperties, BaseConstants.JTA_COMMIT_AFTER_SEND);
    Boolean rollbackOnly = getBooleanProperty(messageProperties, BaseConstants.SET_ROLLBACK_ONLY);
    Boolean persistent = getBooleanProperty(messageProperties, JMSConstants.JMS_DELIVERY_MODE);
    Integer priority = getIntegerProperty(messageProperties, JMSConstants.JMS_PRIORITY);
    Integer timeToLive = getIntegerProperty(messageProperties, JMSConstants.JMS_TIME_TO_LIVE);

    MessageProducer producer = null;
    Destination destination = null;
    Session session = null;
    boolean sendingSuccessful = false;
    JMSConnectionFactory.JMSPooledConnectionHolder pooledConnection = null;
    try {

        pooledConnection = jmsConnectionFactory.getConnectionFromPool();

        producer = pooledConnection.getProducer();
        session = pooledConnection.getSession();
        Message jmsMessage = convertToJMSMessage(message, publisherDetails.getMessageConfig(), session);
        setJMSTransportHeaders(jmsMessage, jmsHeaders);

        // Do not commit, if message is marked for rollback
        if (rollbackOnly != null && rollbackOnly) {
            jtaCommit = Boolean.FALSE;
        }

        if (persistent != null) {
            try {
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            } catch (JMSException e) {
                handleConnectionException("Error setting JMS Producer for PERSISTENT delivery", e);
            }
        }
        if (priority != null) {
            try {
                producer.setPriority(priority);
            } catch (JMSException e) {
                handleConnectionException("Error setting JMS Producer priority to : " + priority, e);
            }
        }
        if (timeToLive != null) {
            try {
                producer.setTimeToLive(timeToLive);
            } catch (JMSException e) {
                handleConnectionException("Error setting JMS Producer TTL to : " + timeToLive, e);
            }
        }

        // perform actual message sending
        //        try {
        if (jmsSpec11 || isQueue == null) {
            producer.send(jmsMessage);

        } else {
            if (isQueue) {
                ((QueueSender) producer).send(jmsMessage);

            } else {
                ((TopicPublisher) producer).publish(jmsMessage);
            }
        }
        sendingSuccessful = true;

        if (log.isDebugEnabled()) {

            //            // set the actual MessageID to the message context for use by any others down the line
            String msgId = null;
            try {
                msgId = jmsMessage.getJMSMessageID();
            } catch (JMSException jmse) {
                log.error(jmse.getMessage(), jmse);
            }

            log.debug(" with JMS Message ID : " + msgId + " to destination : " + producer.getDestination());
        }

    } catch (JMSException e) {
        handleConnectionException("Error sending message to destination : " + destination, e);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        if (jtaCommit != null) {

            try {
                if (session.getTransacted()) {
                    if (sendingSuccessful && (rollbackOnly == null || !rollbackOnly)) {
                        session.commit();
                    } else {
                        session.rollback();
                    }
                }

                if (log.isDebugEnabled()) {
                    log.debug((sendingSuccessful ? "Committed" : "Rolled back")
                            + " local (JMS Session) Transaction");
                }

            } catch (Exception e) {
                handleConnectionException("Error committing/rolling back local (i.e. session) "
                        + "transaction after sending of message "//with MessageContext ID : " +
                        + " to destination : " + destination, e);
            }
        }
        if (pooledConnection != null) {
            jmsConnectionFactory.returnPooledConnection(pooledConnection);
        }
    }
}

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

/**
 * Perform actual send of JMS message to the Destination selected
 *
 * @param messageProperties the Axis2 MessageContext
 *///from  w w  w  .j  a v a2  s .com
public void send(Message jmsMessage, Map<String, String> messageProperties) {

    Boolean jtaCommit = getBooleanProperty(messageProperties, BaseConstants.JTA_COMMIT_AFTER_SEND);
    Boolean rollbackOnly = getBooleanProperty(messageProperties, BaseConstants.SET_ROLLBACK_ONLY);
    Boolean persistent = getBooleanProperty(messageProperties, JMSConstants.JMS_DELIVERY_MODE);
    Integer priority = getIntegerProperty(messageProperties, JMSConstants.JMS_PRIORITY);
    Integer timeToLive = getIntegerProperty(messageProperties, JMSConstants.JMS_TIME_TO_LIVE);

    // Do not commit, if message is marked for rollback
    if (rollbackOnly != null && rollbackOnly) {
        jtaCommit = Boolean.FALSE;
    }

    if (persistent != null) {
        try {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } catch (JMSException e) {
            handleException("Error setting JMS Producer for PERSISTENT delivery", e);
        }
    }
    if (priority != null) {
        try {
            producer.setPriority(priority);
        } catch (JMSException e) {
            handleException("Error setting JMS Producer priority to : " + priority, e);
        }
    }
    if (timeToLive != null) {
        try {
            producer.setTimeToLive(timeToLive);
        } catch (JMSException e) {
            handleException("Error setting JMS Producer TTL to : " + timeToLive, e);
        }
    }

    boolean sendingSuccessful = false;
    // perform actual message sending
    try {
        if (jmsSpec11 || isQueue == null) {
            producer.send(jmsMessage);

        } else {
            if (isQueue) {
                ((QueueSender) producer).send(jmsMessage);

            } else {
                ((TopicPublisher) producer).publish(jmsMessage);
            }
        }

        //            // set the actual MessageID to the message context for use by any others down the line
        String msgId = null;
        try {
            msgId = jmsMessage.getJMSMessageID();
            //                if (msgId != null) {
            //                    msgCtx.setProperty(JMSConstants.JMS_MESSAGE_ID, msgId);
            //                }
        } catch (JMSException ignore) {
        }

        sendingSuccessful = true;

        if (log.isDebugEnabled()) {
            log.debug(" with JMS Message ID : " + msgId + " to destination : " + producer.getDestination());
        }

    } catch (JMSException e) {
        handleException("Error sending message to destination : " + destination, e);

    } finally {

        if (jtaCommit != null) {

            //                UserTransaction ut = (UserTransaction) msgCtx.getProperty(BaseConstants.USER_TRANSACTION);
            //                if (ut != null) {
            //
            //                    try {
            //                        if (sendingSuccessful && jtaCommit) {
            //                            ut.commit();
            //                        } else {
            //                            ut.rollback();
            //                        }
            //                        msgCtx.removeProperty(BaseConstants.USER_TRANSACTION);
            //
            //                        if (log.isDebugEnabled()) {
            //                            log.debug((sendingSuccessful ? "Committed" : "Rolled back") +
            //                                " JTA Transaction");
            //                        }
            //
            //                    } catch (Exception e) {
            //                        handleException("Error committing/rolling back JTA transaction after " +
            //                            "sending of message "+//with MessageContext ID : " + msgCtx.getMessageID() +
            //                            " to destination : " + destination, e);
            //                    }
            //                }
            //
            //            } else {
            try {
                if (session.getTransacted()) {
                    if (sendingSuccessful && (rollbackOnly == null || !rollbackOnly)) {
                        session.commit();
                    } else {
                        session.rollback();
                    }
                }

                if (log.isDebugEnabled()) {
                    log.debug((sendingSuccessful ? "Committed" : "Rolled back")
                            + " local (JMS Session) Transaction");
                }

            } catch (JMSException e) {
                handleException("Error committing/rolling back local (i.e. session) "
                        + "transaction after sending of message "//with MessageContext ID : " +
                        + " to destination : " + destination, e);
            }
        }
    }
}