Example usage for javax.jms MessageProducer send

List of usage examples for javax.jms MessageProducer send

Introduction

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

Prototype


void send(Message message) throws JMSException;

Source Link

Document

Sends a message using the MessageProducer 's default delivery mode, priority, and time to live.

Usage

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects all message and then wait for the redelivered
 * message./*from  w  w w  .  jav a 2  s .c  o m*/
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void allUnacknowledgeMessageListenerTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    int sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();

    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "multipleUnacknowledgeQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "multipleUnacknowledgeQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    MessageConsumer receiver = andesJMSConsumer.getReceiver();
    receiver.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
            try {
                TextMessage textMessage = (TextMessage) message;
                if (getMessageList(receivedMessages).contains(textMessage.getText())) {
                    message.acknowledge();
                }
                receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
            } catch (JMSException e) {
                throw new RuntimeException("Exception occurred when receiving messages.", e);
            }
        }
    });

    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount * 2; i++) {
        if (i < sendCount) {
            Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                    "Invalid messages received. #" + Integer.toString(i) + " expected.");
        } else {
            validateMessageContentAndDelay(receivedMessages, i - sendCount, i,
                    "#" + Integer.toString(i - sendCount));
        }
    }

    Assert.assertEquals(receivedMessages.size(), sendCount * 2, "Message receiving failed.");
}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects the first message and then wait for the redelivered
 * message./*  ww w. j a  va 2s. co  m*/
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void firstMessageInvalidOnlyQueueMessageListenerTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    long sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();
    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "firstMessageInvalidOnlyQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "firstMessageInvalidOnlyQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);
    publisherConfig.setPrintsPerMessageCount(sendCount / 10L);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    MessageConsumer receiver = andesJMSConsumer.getReceiver();
    receiver.setMessageListener(new MessageListener() {
        private boolean receivedFirstMessage = false;

        @Override
        public void onMessage(Message message) {
            try {
                TextMessage textMessage = (TextMessage) message;
                if (!receivedFirstMessage && "#0".equals(textMessage.getText())) {
                    receivedFirstMessage = true;
                } else {
                    message.acknowledge();
                }
                receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
            } catch (JMSException e) {
                throw new RuntimeException("Exception occurred when receiving messages.", e);
            }
        }
    });

    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount; i++) {
        Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                "Invalid messages received. #" + Integer.toString(i) + " expected.");
    }

    validateMessageContentAndDelay(receivedMessages, 0, 10, "#0");

    Assert.assertEquals(receivedMessages.size(), sendCount + 1, "Message receiving failed.");
}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects the first message and then wait for the redelivered
 * message./*from   ww  w. j a va2 s  .  c  om*/
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 * Here message receive method is used instead of the message listener to receive messages.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void firstMessageInvalidOnlyQueueMessageReceiverTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    long sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();
    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "firstMessageInvalidOnlyReceiverQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "firstMessageInvalidOnlyReceiverQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);
    publisherConfig.setPrintsPerMessageCount(sendCount / 10L);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    final MessageConsumer receiver = andesJMSConsumer.getReceiver();
    Thread messageReceivingThread = new Thread() {
        private boolean receivedFirstMessage = false;

        public void run() {
            while (receiver != null) {
                try {
                    TextMessage textMessage = (TextMessage) receiver.receive();
                    if (!receivedFirstMessage && "#0".equals(textMessage.getText())) {
                        receivedFirstMessage = true;
                    } else {
                        textMessage.acknowledge();
                    }
                    receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                    andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
                } catch (JMSException e) {
                    throw new RuntimeException("Exception occurred when receiving messages.", e);
                }
            }
        }
    };
    messageReceivingThread.start();
    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }
    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));
    for (int i = 0; i < sendCount; i++) {
        Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                "Invalid messages received. #" + Integer.toString(i) + " expected.");
    }

    validateMessageContentAndDelay(receivedMessages, 0, 10, "#0");

    Assert.assertEquals(receivedMessages.size(), sendCount + 1, "Message receiving failed.");

}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects all message and then wait for the redelivered
 * message.//from  w ww  .j a va  2 s  . co m
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 * Here message receive method is used instead of the message listener to receive messages.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void allUnacknowledgeMessageReceiverTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    int sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();

    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "multipleUnacknowledgeReceiverQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "multipleUnacknowledgeReceiverQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    final MessageConsumer receiver = andesJMSConsumer.getReceiver();
    Thread messageReceivingThread = new Thread() {
        public void run() {
            while (receiver != null) {
                try {
                    TextMessage textMessage = (TextMessage) receiver.receive();
                    if (getMessageList(receivedMessages).contains(textMessage.getText())) {
                        textMessage.acknowledge();
                    }
                    receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                    andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
                } catch (JMSException e) {
                    throw new RuntimeException("Exception occurred when receiving messages.", e);
                }
            }
        }
    };
    messageReceivingThread.start();
    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount * 2; i++) {
        if (i < sendCount) {
            Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                    "Invalid messages received. #" + Integer.toString(i) + " expected.");
        } else {
            validateMessageContentAndDelay(receivedMessages, i - sendCount, i,
                    "#" + Integer.toString(i - sendCount));
        }
    }

    Assert.assertEquals(receivedMessages.size(), sendCount * 2, "Message receiving failed.");
}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects a message after each 3 received messages and then wait
 * for the redelivered message.//from  w  ww .  ja  v  a 2  s . c o  m
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void oneByOneUnacknowledgeMessageListenerTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    long sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();

    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "oneByOneUnacknowledgeQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    MessageConsumer receiver = andesJMSConsumer.getReceiver();
    receiver.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
            try {
                TextMessage textMessage = (TextMessage) message;
                if (Integer.parseInt(textMessage.getText().split("#")[1]) % 3 != 0
                        || getMessageList(receivedMessages).contains(textMessage.getText())) {
                    message.acknowledge();
                }
                receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
            } catch (JMSException e) {
                throw new RuntimeException("Exception occurred when receiving messages.", e);
            }
        }
    });

    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount; i++) {
        Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                "Invalid messages received. #" + Integer.toString(i) + " expected.");
    }

    validateMessageContentAndDelay(receivedMessages, 0, 10, "#0");
    validateMessageContentAndDelay(receivedMessages, 1, 11, "#3");
    validateMessageContentAndDelay(receivedMessages, 2, 12, "#6");
    validateMessageContentAndDelay(receivedMessages, 3, 13, "#9");

    Assert.assertEquals(receivedMessages.size(), sendCount + 4, "Message receiving failed.");
}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects first 4 messages and then wait for the redelivered
 * message./*from  w  w  w.  ja  v  a  2 s .c  o  m*/
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void firstFewUnacknowledgeMessageListenerTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    long sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();

    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "firstFewUnacknowledgeQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "firstFewUnacknowledgeQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    MessageConsumer receiver = andesJMSConsumer.getReceiver();
    receiver.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
            try {
                TextMessage textMessage = (TextMessage) message;
                if (Integer.parseInt(textMessage.getText().split("#")[1]) >= 4
                        || getMessageList(receivedMessages).contains(textMessage.getText())) {
                    message.acknowledge();
                }
                receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
            } catch (JMSException e) {
                throw new RuntimeException("Exception occurred when receiving messages.", e);
            }
        }
    });

    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount; i++) {
        Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                "Invalid messages received. #" + Integer.toString(i) + " expected.");
    }

    validateMessageContentAndDelay(receivedMessages, 0, 10, "#0");
    validateMessageContentAndDelay(receivedMessages, 1, 11, "#1");
    validateMessageContentAndDelay(receivedMessages, 2, 12, "#2");
    validateMessageContentAndDelay(receivedMessages, 3, 13, "#3");

    Assert.assertEquals(receivedMessages.size(), sendCount + 4, "Message receiving failed.");
}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects a message after each 3 received messages and then wait
 * for the redelivered message./*from www  .j  av  a2 s.  c  om*/
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 * Here message receive method is used instead of the message listener to receive messages.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void oneByOneUnacknowledgeMessageReceiverTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    long sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();

    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "oneByOneUnacknowledgeReceiverQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeReceiverQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    final MessageConsumer receiver = andesJMSConsumer.getReceiver();
    Thread messageReceivingThread = new Thread() {
        public void run() {
            while (receiver != null) {
                try {
                    TextMessage textMessage = (TextMessage) receiver.receive();
                    if (Integer.parseInt(textMessage.getText().split("#")[1]) % 3 != 0
                            || getMessageList(receivedMessages).contains(textMessage.getText())) {
                        textMessage.acknowledge();
                    }
                    receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                    andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
                } catch (JMSException e) {
                    throw new RuntimeException("Exception occurred when receiving messages.", e);
                }
            }
        }
    };
    messageReceivingThread.start();
    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount; i++) {
        Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                "Invalid messages received. #" + Integer.toString(i) + " expected.");
    }

    validateMessageContentAndDelay(receivedMessages, 0, 10, "#0");
    validateMessageContentAndDelay(receivedMessages, 1, 11, "#3");
    validateMessageContentAndDelay(receivedMessages, 2, 12, "#6");
    validateMessageContentAndDelay(receivedMessages, 3, 13, "#9");

    Assert.assertEquals(receivedMessages.size(), sendCount + 4, "Message receiving failed.");
}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 10 messages and the subscriber rejects first 4 messages and then wait for the redelivered
 * message./*from   w w w . ja va  2  s.  com*/
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 * Here message receive method is used instead of the message listener to receive messages.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void firstFewUnacknowledgeMessageReceiverTestCase() throws AndesClientConfigurationException,
        XPathExpressionException, IOException, JMSException, AndesClientException, NamingException {
    long sendCount = 10;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();

    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "firstFewUnacknowledgeReceiverQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "firstFewUnacknowledgeReceiverQueue");
    publisherConfig.setNumberOfMessagesToSend(sendCount);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    final MessageConsumer receiver = andesJMSConsumer.getReceiver();
    Thread messageReceivingThread = new Thread() {
        public void run() {
            while (receiver != null) {
                try {
                    TextMessage textMessage = (TextMessage) receiver.receive();
                    if (Integer.parseInt(textMessage.getText().split("#")[1]) >= 4
                            || getMessageList(receivedMessages).contains(textMessage.getText())) {
                        textMessage.acknowledge();
                    }
                    receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                    andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
                } catch (JMSException e) {
                    throw new RuntimeException("Exception occurred when receiving messages.", e);
                }
            }
        }
    };
    messageReceivingThread.start();
    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount; i++) {
        Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                "Invalid messages received. #" + Integer.toString(i) + " expected.");
    }

    validateMessageContentAndDelay(receivedMessages, 0, 10, "#0");
    validateMessageContentAndDelay(receivedMessages, 1, 11, "#1");
    validateMessageContentAndDelay(receivedMessages, 2, 12, "#2");
    validateMessageContentAndDelay(receivedMessages, 3, 13, "#3");

    Assert.assertEquals(receivedMessages.size(), sendCount + 4, "Message receiving failed.");
}

From source file:org.wso2.mb.integration.tests.amqp.functional.RedeliveryDelayTestCase.java

/**
 * This test publishes 1000 messages and the subscriber reject each 100th message and then wait for the redelivered
 * message.//ww w . j av a 2  s .c om
 * <p/>
 * The redelivered message is tested against the same message content with the original message and the timestamps
 * are also checked against the original message timestamp to make sure that the message was delayed.
 *
 * @throws AndesClientConfigurationException
 * @throws XPathExpressionException
 * @throws IOException
 * @throws JMSException
 * @throws AndesClientException
 * @throws NamingException
 */
@Test(groups = { "wso2.mb", "queue" })
public void oneByOneUnacknowledgeMessageListenerForMultipleMessagesTestCase()
        throws AndesClientConfigurationException, XPathExpressionException, IOException, JMSException,
        AndesClientException, NamingException {
    long sendCount = 1000;
    final List<ImmutablePair<String, Calendar>> receivedMessages = new ArrayList<>();

    // Creating a consumer client configuration
    AndesJMSConsumerClientConfiguration consumerConfig = new AndesJMSConsumerClientConfiguration(getAMQPPort(),
            ExchangeType.QUEUE, "oneByOneUnacknowledgeMessageListenerForMultiple");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeMessageListenerForMultiple");
    publisherConfig.setNumberOfMessagesToSend(sendCount);

    // Creating clients
    AndesClient consumerClient = new AndesClient(consumerConfig, true);
    final AndesJMSConsumer andesJMSConsumer = consumerClient.getConsumers().get(0);
    MessageConsumer receiver = andesJMSConsumer.getReceiver();
    receiver.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
            try {
                TextMessage textMessage = (TextMessage) message;
                if (Integer.parseInt(textMessage.getText().split("#")[1]) % 100 != 0
                        || getMessageList(receivedMessages).contains(textMessage.getText())) {
                    message.acknowledge();
                }
                receivedMessages.add(ImmutablePair.of(textMessage.getText(), Calendar.getInstance()));
                andesJMSConsumer.getReceivedMessageCount().incrementAndGet();
            } catch (JMSException e) {
                throw new RuntimeException("Exception occurred when receiving messages.", e);
            }
        }
    });

    AndesClient publisherClient = new AndesClient(publisherConfig, true);
    AndesJMSPublisher andesJMSPublisher = publisherClient.getPublishers().get(0);
    MessageProducer sender = andesJMSPublisher.getSender();
    for (int i = 0; i < sendCount; i++) {
        TextMessage textMessage = andesJMSPublisher.getSession().createTextMessage("#" + Integer.toString(i));
        sender.send(textMessage);
    }

    AndesClientUtils.waitForMessagesAndShutdown(consumerClient, AndesClientConstants.DEFAULT_RUN_TIME * 2);
    log.info("Received Messages : " + getMessageList(receivedMessages));

    for (int i = 0; i < sendCount; i++) {
        Assert.assertEquals(receivedMessages.get(i).getLeft(), "#" + Integer.toString(i),
                "Invalid messages received. #" + Integer.toString(i) + " expected.");
    }

    validateMessageContentAndDelay(receivedMessages, 0, 1000, "#0");
    validateMessageContentAndDelay(receivedMessages, 99, 1001, "#100");
    validateMessageContentAndDelay(receivedMessages, 199, 1002, "#200");
    validateMessageContentAndDelay(receivedMessages, 299, 1003, "#300");
    validateMessageContentAndDelay(receivedMessages, 399, 1004, "#400");
    validateMessageContentAndDelay(receivedMessages, 499, 1005, "#500");
    validateMessageContentAndDelay(receivedMessages, 599, 1006, "#600");
    validateMessageContentAndDelay(receivedMessages, 699, 1007, "#700");
    validateMessageContentAndDelay(receivedMessages, 799, 1008, "#800");
    validateMessageContentAndDelay(receivedMessages, 899, 1009, "#900");

    Assert.assertEquals(receivedMessages.size(), sendCount + 10, "Message receiving failed.");
}

From source file:org.apache.activemq.usecases.DurableSubscriberWithNetworkDisconnectTest.java

public void testSendOnAReceiveOnBWithTransportDisconnect() throws Exception {
    bridgeBrokers(SPOKE, HUB);/*from ww  w .  j ava 2s  .  c om*/

    startAllBrokers();

    // Setup connection
    URI hubURI = brokers.get(HUB).broker.getVmConnectorURI();
    URI spokeURI = brokers.get(SPOKE).broker.getVmConnectorURI();
    ActiveMQConnectionFactory facHub = new ActiveMQConnectionFactory(hubURI);
    ActiveMQConnectionFactory facSpoke = new ActiveMQConnectionFactory(spokeURI);
    Connection conHub = facHub.createConnection();
    Connection conSpoke = facSpoke.createConnection();
    conHub.setClientID("clientHUB");
    conSpoke.setClientID("clientSPOKE");
    conHub.start();
    conSpoke.start();
    Session sesHub = conHub.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sesSpoke = conSpoke.createSession(false, Session.AUTO_ACKNOWLEDGE);

    ActiveMQTopic topic = new ActiveMQTopic("TEST.FOO");
    String consumerName = "consumerName";

    // Setup consumers
    MessageConsumer remoteConsumer = sesSpoke.createDurableSubscriber(topic, consumerName);
    remoteConsumer.setMessageListener(new MessageListener() {
        public void onMessage(Message msg) {
            try {
                TextMessage textMsg = (TextMessage) msg;
                receivedMsgs++;
                LOG.info("Received messages (" + receivedMsgs + "): " + textMsg.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });

    // allow subscription information to flow back to Spoke
    sleep(1000);

    // Setup producer
    MessageProducer localProducer = sesHub.createProducer(topic);
    localProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

    // Send messages
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        sleep(50);
        if (i == 50 || i == 150) {
            if (simulateStalledNetwork) {
                socketProxy.pause();
            } else {
                socketProxy.close();
            }
            networkDownTimeStart = System.currentTimeMillis();
        } else if (networkDownTimeStart > 0) {
            // restart after NETWORK_DOWN_TIME seconds
            sleep(NETWORK_DOWN_TIME);
            networkDownTimeStart = 0;
            if (simulateStalledNetwork) {
                socketProxy.goOn();
            } else {
                socketProxy.reopen();
            }
        } else {
            // slow message production to allow bridge to recover and limit message duplication
            sleep(500);
        }
        Message test = sesHub.createTextMessage("test-" + i);
        localProducer.send(test);
    }

    LOG.info("waiting for messages to flow");
    Wait.waitFor(new Wait.Condition() {
        @Override
        public boolean isSatisified() throws Exception {
            return receivedMsgs >= MESSAGE_COUNT;
        }
    });

    assertTrue("At least message " + MESSAGE_COUNT + " must be received, count=" + receivedMsgs,
            MESSAGE_COUNT <= receivedMsgs);
    brokers.get(HUB).broker.deleteAllMessages();
    brokers.get(SPOKE).broker.deleteAllMessages();
    conHub.close();
    conSpoke.close();
}