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.wso2.esb.integration.common.utils.clients.jmsclient.JMSQueueMessageProducer.java

/**
 * Method to establish the connection with the given Queue, with message persistance as specified.
 * This must be called before calling pushMessage() to send messages.
 *
 * @param persistMessage whether or not messages need to be persisted
 * @param queueName name of the queue/*from ww  w. j  a v  a2  s .co m*/
 * @throws JMSException if connection to the queue fails
 */
public void connect(String queueName, boolean persistMessage) throws JMSException {
    connection = connectionFactory.createConnection();
    connection.start();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(queueName);
    producer = session.createProducer(destination);
    if (persistMessage) {
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    } else {
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    }
}

From source file:org.wso2.mb.integration.common.clients.operations.queue.QueueMessageSender.java

public void run() {
    try {/*from  w  ww  . j a  v  a2  s.co m*/
        Message message = null;
        String everything = "";
        if (readFromFile) {
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            try {
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();

                while (line != null) {
                    sb.append(line);
                    sb.append('\n');
                    line = br.readLine();
                }

                // Remove the last appended next line since there is no next line.
                sb.replace(sb.length() - 1, sb.length() + 1, "");

                everything = sb.toString();
            } finally {

                br.close();

            }
        }

        long threadID = Thread.currentThread().getId();
        int localMessageCount = 0;
        while (messageCounter.get() < numOfMessagesToSend) {
            if (typeOfMessage.equals("text")) {
                if (!readFromFile) {
                    message = queueSession.createTextMessage(
                            "sending Message:-" + messageCounter.get() + "- " + "ThreadID:" + threadID);
                } else {
                    message = queueSession.createTextMessage(everything);
                }
            } else if (typeOfMessage.equals("byte")) {
                message = queueSession.createBytesMessage();
            } else if (typeOfMessage.equals("map")) {
                message = queueSession.createMapMessage();
            } else if (typeOfMessage.equals("object")) {
                message = queueSession.createObjectMessage();
            } else if (typeOfMessage.equals("stream")) {
                message = queueSession.createStreamMessage();
            }
            message.setStringProperty("msgID", Integer.toString(messageCounter.get()));
            synchronized (messageCounter.getClass()) {
                if (messageCounter.get() >= numOfMessagesToSend) {
                    break;
                }
                queueSender.send(message, DeliveryMode.PERSISTENT, 0, jmsExpiration);
                messageCounter.incrementAndGet();
            }
            localMessageCount++;
            if (messageCounter.get() % printNumberOfMessagesPer == 0) {

                log.info((readFromFile ? "(FROM FILE)" : "(SIMPLE MESSAGE) ") + "[QUEUE SEND] ThreadID:"
                        + threadID + " queueName:" + queueName + " localMessageCount:" + localMessageCount
                        + " totalMessageCount:-" + messageCounter.get() + "- count to send:"
                        + numOfMessagesToSend);
            }
            if (isToPrintEachMessage) {
                log.info("(count:" + messageCounter.get() + "/threadID:" + threadID + ") " + message);
            }
            if (delay != 0) {
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    //silently ignore
                }
            }
        }

        stopSending();

    } catch (JMSException e) {
        log.error("Error while publishing messages", e);
    } catch (IOException e) {
        log.error("Error while reading file", e);
    }
}

From source file:org.wso2.mb.integration.common.clients.operations.topic.TopicMessagePublisher.java

public void run() {
    try {// w  w  w.  j  a v  a2 s .  c  o m
        TextMessage textMessage = null;
        String everything = "";
        if (readFromFile) {
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            try {
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();

                while (line != null) {
                    sb.append(line);
                    sb.append('\n');
                    line = br.readLine();
                }
                everything = sb.toString();
            } finally {

                br.close();

            }
        }
        long threadID = Thread.currentThread().getId();
        int localMessageCount = 0;
        while (messageCounter.get() < numOfMessagesToSend) {
            if (!readFromFile) {
                textMessage = topicSession.createTextMessage(
                        "sending Message:-" + messageCounter.get() + "- " + "ThreadID:" + threadID);
            } else {
                textMessage = topicSession.createTextMessage("sending Message:-" + messageCounter.get() + "- "
                        + "ThreadID:" + threadID + "  " + everything);
            }
            textMessage.setStringProperty("msgID", Integer.toString(messageCounter.get()));

            // Check the message count again before publishing since, messages count might have increased from
            // other threads after the last check,
            // Double checking has been done to reduce the synchronized block size
            synchronized (messageCounter.getClass()) {
                if (messageCounter.get() >= numOfMessagesToSend) {
                    break;
                }
                topicPublisher.send(textMessage, DeliveryMode.PERSISTENT, 0, jmsExpiration);

                messageCounter.incrementAndGet();
            }

            localMessageCount++;
            if (messageCounter.get() % printNumberOfMessagesPer == 0) {

                log.info((readFromFile ? "(FROM FILE)" : "(SIMPLE MESSAGE) ") + "[TOPIC SEND] ThreadID:"
                        + threadID + " topicName:" + topicName + " localMessageCount:" + localMessageCount
                        + " totalMessageCount:" + messageCounter.get() + " count to send:"
                        + numOfMessagesToSend);
            }
            if (isToPrintEachMessage) {
                log.info("(count:" + messageCounter.get() + "/threadID:" + threadID + ") " + textMessage);
            }
            if (delay != 0) {
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    //silently ignore
                }
            }
        }
        stopPublishing();

    } catch (JMSException e) {
        log.error("Error while publishing messages", e);
    } catch (IOException e) {
        log.error("Error while reading from file", e);
    }
}

From source file:tools.ProducerTool.java

@Override
public void run() {
    Connection connection = null;
    Session session = null;//from  ww w  . j  a v  a  2 s. c o  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);
            }
        }
    }
}