Example usage for org.apache.commons.lang3.tuple ImmutablePair of

List of usage examples for org.apache.commons.lang3.tuple ImmutablePair of

Introduction

In this page you can find the example usage for org.apache.commons.lang3.tuple ImmutablePair of.

Prototype

public static <L, R> ImmutablePair<L, R> of(final L left, final R right) 

Source Link

Document

Obtains an immutable pair of from two objects inferring the generic types.

This factory allows the pair to be created using inference to obtain the generic types.

Usage

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  . 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 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.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./*from   w w w  . j  av  a  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.
 * 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 oneByOneUnacknowledgeMessageReceiverForMultipleMessagesTestCase()
        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, "oneByOneUnacknowledgeMessageReceiverForMultipleQueue");
    consumerConfig.setAcknowledgeMode(JMSAcknowledgeMode.PER_MESSAGE_ACKNOWLEDGE);
    consumerConfig.setAsync(false);

    // Creating a publisher client configuration
    AndesJMSPublisherClientConfiguration publisherConfig = new AndesJMSPublisherClientConfiguration(
            getAMQPPort(), ExchangeType.QUEUE, "oneByOneUnacknowledgeMessageReceiverForMultipleQueue");
    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]) % 100 != 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 * 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:rapture.dp.DefaultDecisionProcessExecutor.java

public void publishSplitChildren(Worker parent, Step step, Workflow flow) {
    String base = step.getExecutable().substring(StepHelper.SPLIT_PREFIX.length() + 1);
    String names[] = base.split(",");
    parent.setWaitCount(names.length);//from  w w  w .  j  av a  2 s . c  o  m
    parent.setStatus(WorkerExecutionState.BLOCKED);
    List<ImmutablePair<Worker, String>> children = Lists.newArrayList();
    List<Worker> stillborn = Lists.newArrayList();

    // prepare children and eliminate
    for (int i = 0; i < names.length; i++) {
        Step target = getStep(names[i], flow);
        Worker child = SplitUtils.createSplitChild(parent, flow, i, names.length, target);
        if (target == null) {
            child.setDetail("Attempt to start worker with non-extant step " + names[i] + " from "
                    + step.getName() + " in " + flow.getWorkflowURI());
            log.error(child.getDetail());
            parent.setWaitCount(parent.getWaitCount() - 1);
            child.setStatus(WorkerExecutionState.ERROR);
            saveWorker(child);
            stillborn.add(child);
        } else {
            saveWorker(child);
            children.add(ImmutablePair.of(child, calculateCategory(target, flow)));
        }
    }
    saveWorker(parent);

    // register workers with workorder
    String workOrderUri = parent.getWorkOrderURI();
    WorkOrder workOrder = WorkOrderStorage.readByFields(workOrderUri);

    try {
        grabMultiWorkerLock(workOrder, parent, FORCE);
        workOrder = WorkOrderStorage.readByFields(workOrderUri);
        for (Pair<Worker, String> pair : children) {
            workOrder.getWorkerIds().add(pair.getLeft().getId());
            if (log.isDebugEnabled()) {
                String at = (pair.getLeft().getStack().size() > 0) ? pair.getLeft().getStack().get(0)
                        : "UNKNOWN_LOCATION";
                log.debug("Adding new worker " + pair.getLeft().getId() + " at " + at);
            }
        }
        for (Worker child : stillborn) {
            workOrder.getWorkerIds().add(child.getId());
        }
        WorkOrderStorage.add(new RaptureURI(workOrderUri, Scheme.WORKORDER), workOrder,
                ContextFactory.getKernelUser().getUser(), "Update for split");
        JoinCountdown countdown = new JoinCountdown();
        countdown.setParentId(parent.getId());
        countdown.setWorkOrderURI(parent.getWorkOrderURI());
        countdown.setWaitCount(children.size());
        JoinCountdownStorage.add(null, countdown, ContextFactory.getKernelUser().getUser(),
                "Starting Countdown");
    } finally {
        releaseMultiWorkerLock(workOrder, parent, FORCE);
    }

    // publish viable children
    for (Pair<Worker, String> pair : children) {
        publishStep(pair.getLeft(), pair.getRight());
    }
}

From source file:rapture.kernel.DecisionApiImpl.java

/**
 * Trusted method to get the workflow and the selected step as a pair.
 *///w ww .  j  av a2s  . c  o m
public Pair<Workflow, Step> getWorkflowWithStep(CallingContext context, String stepURI) {
    RaptureURI uri = new RaptureURI(stepURI, Scheme.WORKFLOW);
    String stepName = uri.getElement();
    if (stepName == null) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST, String.format(
                "The Step URI passed in does not contain an 'element' indicating the step, but it requires one: [%s]",
                uri.toString()));
    }
    Workflow workflow = getWorkflowNotNull(context, stepURI);
    return ImmutablePair.of(workflow, getStep(workflow, stepName));
}