Example usage for org.joda.time Duration Duration

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

Introduction

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

Prototype

public Duration(Object duration) 

Source Link

Document

Creates a duration from the specified object using the org.joda.time.convert.ConverterManager ConverterManager .

Usage

From source file:com.vaushell.superpipes.nodes.tumblr.N_TB_Delete.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  ww w .j  a  v  a2 s. c o m*/
protected void loop() throws Exception {
    // Receive
    setMessage(getLastMessageOrWait());

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] receive message : " + Message.formatSimple(getMessage()));
    }

    // Delete from Twitter
    if (getMessage().contains("id-tumblr")) {
        final long ID = (long) getMessage().getProperty("id-tumblr");

        new A_Retry<Void>() {
            @Override
            protected Void executeContent() throws IOException, TumblrException {
                if (client.deletePost(getProperties().getConfigString("blogname"), ID)) {
                    return null;
                } else {
                    throw new IOException("Can't delete post with ID=" + 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();

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("[" + getNodeID() + "] can delete post ID=" + ID);
        }

        sendMessage();
    }
}

From source file:com.vaushell.superpipes.nodes.tumblr.N_TB_Post.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from www  .  jav a2s  .  com*/
protected void loop() throws Exception {
    // Receive
    setMessage(getLastMessageOrWait());

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] receive message : " + Message.formatSimple(getMessage()));
    }

    if (!getMessage().contains(Message.KeyIndex.URI)) {
        throw new IllegalArgumentException("message doesn't have an uri");
    }

    // Send to TB
    final URI uri = (URI) getMessage().getProperty(Message.KeyIndex.URI);

    final DateTime date;
    if (getProperties().getConfigBoolean("backdating", Boolean.FALSE)) {
        date = (DateTime) getMessage().getProperty(Message.KeyIndex.PUBLISHED_DATE);
    } else {
        date = null;
    }

    final String desc;
    if (getMessage().contains(Message.KeyIndex.CONTENT)) {
        desc = (String) getMessage().getProperty(Message.KeyIndex.CONTENT);
    } else {
        desc = (String) getMessage().getProperty(Message.KeyIndex.DESCRIPTION);
    }

    final long ID = new A_Retry<Long>() {
        @Override
        protected Long executeContent() throws IOException, TumblrException {
            final String title = (String) getMessage().getProperty(Message.KeyIndex.TITLE);

            final long ID = client.postLink(getProperties().getConfigString("blogname"),
                    uri == null ? null : uri.toString(), title, desc,
                    (Tags) getMessage().getProperty(Message.KeyIndex.TAGS), date);

            if (ID < 0) {
                throw new IOException("Cannot post link with title=" + title);
            } 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();

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] receive ID : " + ID);
    }

    getMessage().setProperty("id-tumblr", ID);

    sendMessage();
}

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

License:Open Source License

public N_TW() {
    // Read every 10 minutes
    super(new Duration(600000L), null);

    this.client = new TwitterClient();
}

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

License:Open Source License

@Override
protected void loop() throws Exception {
    // Receive//from   w  w w  .j  ava 2 s  . co m
    setMessage(getLastMessageOrWait());

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] receive message : " + Message.formatSimple(getMessage()));
    }

    // Delete from Twitter
    if (getMessage().contains("id-twitter")) {
        final long ID = (long) getMessage().getProperty("id-twitter");

        new A_Retry<Void>() {
            @Override
            protected Void executeContent() throws IOException, OAuthException {
                if (client.deleteTweet(ID)) {
                    return null;
                } else {
                    throw new IOException("Can't delete post with ID=" + 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();

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("[" + getNodeID() + "] can delete tweet ID=" + ID);
        }

        sendMessage();
    }
}

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 {/* w w  w .j a 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);
    }
}

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

License:Open Source License

private long tweet(final String message) throws RetryException {
    return new A_Retry<Long>() {
        @Override/*w  w  w .  jav  a  2s  .co  m*/
        protected Long executeContent() throws IOException, OAuthException {
            final long ID = client.tweet(message);

            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();
}

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

License:Open Source License

@Override
protected void loop() throws Exception {
    // Receive// w  ww .  j  ava2 s  .  com
    setMessage(getLastMessageOrWait());

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNodeID() + "] receive message : " + Message.formatSimple(getMessage()));
    }

    // Delete from Twitter
    if (getMessage().contains("id-twitter")) {
        final long oldID = (long) getMessage().getProperty("id-twitter");

        final long newID = new A_Retry<Long>() {
            @Override
            protected Long executeContent() throws IOException, OAuthException {
                final long ID = client.retweet(oldID);

                if (ID < 0) {
                    throw new IOException("Cannot retweet with id=" + oldID);
                } 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();

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("[" + getNodeID() + "] receive ID=" + newID);
        }

        getMessage().setProperty("id-twitter", newID);

        sendMessage();
    }
}

From source file:com.vaushell.superpipes.tools.retry.A_Retry.java

License:Open Source License

public A_Retry() {
    this.retry = 0;
    this.waitTime = new Duration(0L);
    this.waitTimeMultiplier = 1.0;
    this.maxDuration = new Duration(0L);

    this.tryCount = 0;
    this.random = new Random();
}

From source file:com.vaushell.superpipes.transforms.uri.T_CheckURI.java

License:Open Source License

@Override
public Message transform(final Message message) throws Exception {
    // Receive/*from  www . ja v a  2s.c o  m*/
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("[" + getNode().getNodeID() + "/" + getClass().getSimpleName() + "] transform message : "
                + Message.formatSimple(message));
    }

    if (!message.contains(Message.KeyIndex.URI)) {
        return null;
    }

    final URI uri = (URI) message.getProperty(Message.KeyIndex.URI);

    try {
        new A_Retry<Void>() {
            @Override
            protected Void executeContent() throws IOException {
                if (HTTPhelper.isURIvalid(client, uri,
                        getProperties().getConfigDuration("timeout", new Duration(20L * 1000L)))) {
                    return null;
                } else {
                    throw new IOException("Cannot validate URI=" + uri);
                }
            }
        }.setRetry(getProperties().getConfigInteger("retry", 3))
                .setWaitTime(getProperties().getConfigDuration("wait-time", new Duration(2000L)))
                .setWaitTimeMultiplier(getProperties().getConfigDouble("wait-time-multiplier", 2.0))
                .setJitterRange(getProperties().getConfigInteger("jitter-range", 500))
                .setMaxDuration(getProperties().getConfigDuration("max-duration", new Duration(10_000L)))
                .execute();

        return message;
    } catch (final RetryException ex) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("[" + getNode().getNodeID() + "/" + getClass().getSimpleName() + "] Invalid URI : "
                    + uri.toString(), ex);
        }

        return null;
    }
}

From source file:com.xwiki.authentication.sts.STSTokenValidator.java

License:Open Source License

/**
 * checkExpiration(Instant notBefore, Instant notOnOrAfter)
 * Function checks that date now is after (notBefore parameter)
 * and now is before notOnOrAfter (calculating Skew - which is new Duration(this.maxClockSkew) object )
 *      //w  w w .ja  va 2  s  .  co  m
 * @param notBefore  Instant now is after not Before (plus skew)
 * @param notOnOrAfter Instant now is before notOnOrAfter (minus skew)
 * @return true - if check  - is ok, or false if check is fault
 */
private boolean checkExpiration(Instant notBefore, Instant notOnOrAfter) {
    Instant now = new Instant();
    Duration skew = new Duration(maxClockSkew);
    log.debug("Time expiration. Now:" + now + " now+sqew: " + now.plus(skew) + " now-sqew: " + now.minus(skew)
            + " notBefore: " + notBefore + " notAfter: " + notOnOrAfter);
    if (now.plus(skew).isAfter(notBefore) && now.minus(skew).isBefore(notOnOrAfter)) {
        log.debug("Time is in range");
        return true;
    }
    return false;
}