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.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   www  .j  a va  2s  .  c  om*/
 * @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

/**
 * Perform actual send of JMS message to the Destination selected
 *//* w  w w.  j a  va  2 s.  c  om*/
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:tools.ProducerTool.java

@Override
public void run() {
    Connection connection = null;
    Session session = null;//w  ww. ja  va 2  s.  co  m
    try {
        connection = connectionFactory.createConnection();
        if (clientId != null) {
            connection.setClientID(clientId);
        }
        session = connection.createSession(transacted, acknowledgeMode);
        Destination destination = null;
        if (jndiLookupDestinations) {
            destination = (Destination) context.lookup(destinationName);
        } else {
            if (useQueueDestinations) {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryQueue();
                } else {
                    destination = session.createQueue(destinationName);
                }
            } else {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryTopic();
                } else {
                    destination = session.createTopic(destinationName);
                }
            }
        }

        MessageProducer producer = session.createProducer(destination);
        if (durable) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }

        int numMessagesToSend = useFinalControlMessage ? numMessages - 1 : numMessages;

        for (int i = 0; i < numMessagesToSend; i++) {
            String messageText = "Message " + i + " at " + new Date();
            if (bytesLength > -1) {
                byte[] messageTextBytes = messageText.getBytes(StandardCharsets.UTF_8);
                BytesMessage bytesMessage = session.createBytesMessage();
                bytesMessage.writeBytes(messageTextBytes);
                if (messageTextBytes.length < bytesLength) {
                    byte[] paddingBytes = new byte[bytesLength - messageTextBytes.length];
                    bytesMessage.writeBytes(paddingBytes);
                }
                if (messageGroupId != null) {
                    bytesMessage.setStringProperty("JMSXGroupID", messageGroupId);
                }
                LOGGER.info("Sending bytes message");
                producer.send(bytesMessage);
            } else {
                TextMessage textMessage = session.createTextMessage(messageText);
                if (messageGroupId != null) {
                    textMessage.setStringProperty("JMSXGroupID", messageGroupId);
                }
                LOGGER.info("Sending text message: " + messageText);
                producer.send(textMessage);
            }

            if (perMessageSleepMS > 0) {
                Thread.sleep(perMessageSleepMS);
            }
            if (transacted) {
                if ((i + 1) % batchSize == 0) {
                    session.commit();
                }
            }
        }
        if (useFinalControlMessage) {
            Message message = session.createMessage();
            if (messageGroupId != null) {
                message.setStringProperty("JMSXGroupID", messageGroupId);
            }
            LOGGER.info("Sending message");
            producer.send(message);
            if (transacted) {
                session.commit();
            }
        }
        producer.close();
    } catch (Exception ex) {
        LOGGER.error("ProducerTool hit exception: " + ex.getMessage(), ex);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
    }
}