Example usage for org.joda.time Duration getMillis

List of usage examples for org.joda.time Duration getMillis

Introduction

In this page you can find the example usage for org.joda.time Duration getMillis.

Prototype

public long getMillis() 

Source Link

Document

Gets the length of this duration in milliseconds.

Usage

From source file:com.tomtom.speedtools.testutils.resteasy.AsynchronousTestResponse.java

License:Apache License

/**
 * Verify that the resource invocation resulted in a response with given response code.
 *
 * @param expectedStatus When set, the expected status code of the response.
 * @param timeout        Time out value.
 *//*from   ww  w. j ava 2s .c om*/
public void verifyStatus(final int expectedStatus, @Nonnull final Duration timeout) {
    verifyStatus(expectedStatus, timeout.getMillis(), TimeUnit.MILLISECONDS);
}

From source file:com.vaushell.shaarlijavaapi.ShaarliClient.java

License:Open Source License

private DateTime generateDateID() {
    synchronized (this) {
        DateTime now = new DateTime();

        if (lastGeneratedDate != null) {
            Duration diff = new Duration(lastGeneratedDate, now);
            while (diff.getMillis() < 1000L) {
                try {
                    // It's better than a Thread.sleep in a synchronized block
                    wait(1000L - diff.getMillis());
                } catch (final InterruptedException ex) {
                    // Ignore
                }//from   w ww.  j av  a2s. com

                now = new DateTime();
                diff = new Duration(lastGeneratedDate, now);
            }
        }

        lastGeneratedDate = now;

        return lastGeneratedDate;
    }
}

From source file:com.vaushell.superpipes.dispatch.ErrorMailer.java

License:Open Source License

private List<String> getLastErrorsOrWait() throws InterruptedException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getClass().getSimpleName() + "] getLastErrorsOrWait");
    }//from   w  ww  .j  a  va2  s .  co m

    synchronized (internalStack) {
        Duration remaining;
        if (lastPop == null) {
            remaining = new Duration(0L);
        } else {
            // Null for now
            final Duration elapsed = new Duration(lastPop, null);
            remaining = antiBurst.minus(elapsed);
        }

        while (internalStack.isEmpty() || remaining.getMillis() > 0L) {
            if (internalStack.isEmpty() || remaining.getMillis() <= 0L) {
                internalStack.wait();
            } else {
                internalStack.wait(remaining.getMillis());
            }

            if (lastPop == null) {
                remaining = new Duration(0L);
            } else {
                final Duration elapsed = new Duration(lastPop, null);
                remaining = antiBurst.minus(elapsed);
            }
        }

        final List<String> ret = new ArrayList<>();
        ret.addAll(internalStack);

        internalStack.clear();

        lastPop = new DateTime();

        return ret;
    }
}

From source file:com.vaushell.superpipes.nodes.A_Node.java

License:Open Source License

public A_Node(final Duration defaultDelay, final Duration defaultAntiBurst) {
    super();//from  www . j  a  va 2s  . c om

    this.activated = true;
    this.internalStack = new LinkedList<>();
    this.transformsIN = new ArrayList<>();
    this.transformsOUT = new ArrayList<>();
    this.properties = new ConfigProperties();
    this.lastPop = null;
    this.message = null;

    if (defaultAntiBurst != null && defaultAntiBurst.getMillis() <= 0L) {
        throw new IllegalArgumentException("defaultAntiBurst can't be <=0. Should be null.");
    }
    this.antiBurst = defaultAntiBurst;

    if (defaultDelay != null && defaultDelay.getMillis() <= 0L) {
        throw new IllegalArgumentException("defaultDelay can't be <=0. Should be null.");
    }
    this.delay = defaultDelay;
}

From source file:com.vaushell.superpipes.nodes.A_Node.java

License:Open Source License

/**
 * Pop the last message./*w  w  w .  ja v  a2 s .c  o  m*/
 *
 * @return the message
 * @throws InterruptedException
 */
protected Message getLastMessageOrWait() throws InterruptedException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] getLastMessageOrWait");
    }

    final Message message;
    synchronized (internalStack) {
        while (internalStack.isEmpty()) {
            internalStack.wait();
        }

        message = internalStack.pollLast();
    }

    if (lastPop != null && antiBurst != null) {
        // Null for now
        final Duration elapsed = new Duration(lastPop, null);

        final Duration remaining = antiBurst.minus(elapsed);
        if (remaining.getMillis() > 0L) {
            Thread.sleep(remaining.getMillis());
        }
    }

    lastPop = new DateTime();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("[" + getNodeID() + "] wait message and get message=" + Message.formatSimple(message));
    }

    return message;
}

From source file:com.vaushell.superpipes.nodes.A_Node.java

License:Open Source License

/**
 * Pop the last message.//from   w w  w.  ja  v  a2s  .c o  m
 *
 * @param timeout max time to wait. If timeout is smaller than antiburst, use antiburst.
 * @return the message (or null if empty)
 * @throws InterruptedException
 */
protected Message getLastMessageOrWait(final Duration timeout) throws InterruptedException {
    if (timeout == null) {
        throw new IllegalArgumentException();
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] getLastMessageOrWait() : timeout=" + timeout);
    }

    final Message message;
    synchronized (internalStack) {
        DateTime start = new DateTime();
        Duration remaining = timeout;
        while (internalStack.isEmpty() && remaining.getMillis() > 0L) {
            internalStack.wait(remaining.getMillis());

            final DateTime now = new DateTime();

            final Duration elapsed = new Duration(start, now);

            remaining = remaining.minus(elapsed);

            start = now;
        }

        message = internalStack.pollLast();
    }

    if (lastPop != null && antiBurst != null) {
        // Null for now
        final Duration elapsed = new Duration(lastPop, null);

        final Duration remaining = antiBurst.minus(elapsed);
        if (remaining.getMillis() > 0L) {
            Thread.sleep(remaining.getMillis());
        }
    }

    lastPop = new DateTime();

    if (LOGGER.isDebugEnabled()) {
        if (message == null) {
            LOGGER.debug("[" + getNodeID() + "] wait message for " + timeout + "ms and get nothing");
        } else {
            LOGGER.debug("[" + getNodeID() + "] wait message for " + timeout + "ms and get message="
                    + Message.formatSimple(message));
        }
    }

    return message;
}

From source file:com.vaushell.superpipes.nodes.buffer.N_Buffer.java

License:Open Source License

@Override
protected void loop() throws Exception {
    // 1. Are we allowed to publish ?
    final DateTime now = new DateTime();

    final Duration time2wait = getTimeToWait(now);
    if (time2wait.getMillis() > 0L) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[" + getNodeID() + "] time to wait : " + time2wait
                    + ". During this time, we're trying to catch an incoming message.");
        }//w  ww  .j  av  a  2 s.com

        setMessage(getLastMessageOrWait(time2wait));
        if (getMessage() != null) {
            pushMessage(getMessage());
        }

        // And loop again.
    } else {
        // 2. Pop from stack
        setMessage(popMessage());
        if (getMessage() == null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[" + getNodeID()
                        + "] no time to wait and not message in buffer. We're waiting an incoming message");
            }

            // Nothing : we wait for external
            setMessage(getLastMessageOrWait());

            // Push to stack
            pushMessage(getMessage());

            // And loop to check if we're allowed to publish.
        } else {
            // 3. We published
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[" + getNodeID()
                        + "] no time to wait and we found a message in the buffer. We're sending it.");
            }

            lastWrite = now;

            sendMessage();
        }
    }
}

From source file:com.vaushell.superpipes.nodes.buffer.N_Buffer.java

License:Open Source License

private Duration getTimeToWait(final DateTime from) {
    // Best slot/*from  w w w  .j  a  v  a 2  s. c o  m*/
    Duration minDuration;
    if (slots.isEmpty()) {
        minDuration = new Duration(0L);
    } else {
        minDuration = null;
        for (final Slot slot : slots) {
            final Duration duration = slot.getSmallestDiff(from);
            if (minDuration == null || duration.isShorterThan(minDuration)) {
                minDuration = duration;

                if (minDuration.getMillis() <= 0L) {
                    break;
                }
            }
        }
    }

    // Anti burst
    if (getProperties().containsKey("flow-limit") && lastWrite != null) {
        final Duration diff = new Duration(lastWrite, from);

        final Duration toAdd = getProperties().getConfigDuration("flow-limit").minus(diff);
        if (toAdd.isLongerThan(minDuration)) {
            minDuration = toAdd;
        }
    }

    // First message
    if (!messageIDs.isEmpty()) {
        final long firstID = messageIDs.first();
        final DateTime first = new DateTime(firstID);

        if (first.isAfter(from)) {
            final Duration diff = new Duration(from, first);
            if (diff.isLongerThan(minDuration)) {
                minDuration = diff;
            }
        }
    }

    // Result
    return minDuration;
}

From source file:com.vaushell.superpipes.nodes.test.N_ReceiveBlocking.java

License:Open Source License

/**
 * Get the actual processing message, or wait it.
 *
 * @param timeout a timeout//  w  w w. j a  va2  s.  co m
 * @return the Message
 * @throws InterruptedException
 */
public Message getProcessingMessageOrWait(final Duration timeout) throws InterruptedException {
    if (timeout == null) {
        throw new IllegalArgumentException();
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] getProcessingMessageOrWait() : timeout=" + timeout);
    }

    synchronized (messages) {
        DateTime start = new DateTime();
        Duration remaining = timeout;
        while (messages.isEmpty() && remaining.getMillis() > 0L) {
            messages.wait(remaining.getMillis());

            final DateTime now = new DateTime();

            final Duration elapsed = new Duration(start, now);

            remaining = remaining.minus(elapsed);

            start = now;
        }

        return messages.pollFirst();
    }
}

From source file:com.vaushell.superpipes.nodes.twitter.N_TW_Post.java

License:Open Source License

private long tweetPictureFailsafe(final String message, final byte[] picture) throws RetryException {
    try {//from  ww w.  ja  v a  2 s .  co  m
        return new A_Retry<Long>() {
            @Override
            protected Long executeContent() throws IOException, OAuthException {
                try (ByteArrayInputStream bis = new ByteArrayInputStream(picture)) {
                    final long ID = client.tweetPicture(message, bis);

                    if (ID < 0) {
                        throw new IOException("Cannot tweet with message=" + message);
                    } else {
                        return ID;
                    }
                }
            }
        }.setRetry(getProperties().getConfigInteger("retry", 10))
                .setWaitTime(getProperties().getConfigDuration("wait-time", new Duration(5000L)))
                .setWaitTimeMultiplier(getProperties().getConfigDouble("wait-time-multiplier", 2.0))
                .setJitterRange(getProperties().getConfigInteger("jitter-range", 500))
                .setMaxDuration(getProperties().getConfigDuration("max-duration", new Duration(0L))).execute();
    } catch (final RetryException ex) {
        // Cannot send tweet+picture. Send only tweet.
        final Duration d = getProperties().getConfigDuration("wait-time", new Duration(5000L));
        if (d.getMillis() > 0L) {
            try {
                Thread.sleep(d.getMillis());
            } catch (final InterruptedException ex2) {
                // Ignore
            }
        }

        return tweet(message);
    }
}