Example usage for org.joda.time Duration standardMinutes

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

Introduction

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

Prototype

public static Duration standardMinutes(long minutes) 

Source Link

Document

Create a duration with the specified number of minutes assuming that there are the standard number of milliseconds in a minute.

Usage

From source file:com.hackx.beam.WindowedWordCount.java

License:Apache License

public static void main(String[] args) throws IOException {
    Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class);
    final String output = options.getOutput();
    final Duration windowSize = Duration.standardMinutes(options.getWindowSize());
    final Instant minTimestamp = new Instant(options.getMinTimestampMillis());
    final Instant maxTimestamp = new Instant(options.getMaxTimestampMillis());

    Pipeline pipeline = Pipeline.create(options);

    /**/*from w  ww .  ja va2s .  com*/
     * Concept #1: the Beam SDK lets us run the same pipeline with either a bounded or
     * unbounded input source.
     */
    PCollection<String> input = pipeline
            /** Read from the GCS file. */
            .apply(TextIO.Read.from(options.getInputFile()))
            // Concept #2: Add an element timestamp, using an artificial time just to show windowing.
            // See AddTimestampFn for more detail on this.
            .apply(ParDo.of(new AddTimestampFn(minTimestamp, maxTimestamp)));

    /**
     * Concept #3: Window into fixed windows. The fixed window size for this example defaults to 1
     * minute (you can change this with a command-line option). See the documentation for more
     * information on how fixed windows work, and for information on the other types of windowing
     * available (e.g., sliding windows).
     */
    PCollection<String> windowedWords = input
            .apply(Window.<String>into(FixedWindows.of(Duration.standardMinutes(options.getWindowSize()))));

    /**
     * Concept #4: Re-use our existing CountWords transform that does not have knowledge of
     * windows over a PCollection containing windowed values.
     */
    PCollection<KV<String, Long>> wordCounts = windowedWords.apply(new WordCount.CountWords());

    /**
     * Concept #5: Customize the output format using windowing information
     *
     * <p>At this point, the data is organized by window. We're writing text files and and have no
     * late data, so for simplicity we can use the window as the key and {@link GroupByKey} to get
     * one output file per window. (if we had late data this key would not be unique)
     *
     * <p>To access the window in a {@link DoFn}, add a {@link BoundedWindow} parameter. This will
     * be automatically detected and populated with the window for the current element.
     */
    PCollection<KV<IntervalWindow, KV<String, Long>>> keyedByWindow = wordCounts
            .apply(ParDo.of(new DoFn<KV<String, Long>, KV<IntervalWindow, KV<String, Long>>>() {
                @ProcessElement
                public void processElement(ProcessContext context, IntervalWindow window) {
                    context.output(KV.of(window, context.element()));
                }
            }));

    /**
     * Concept #6: Format the results and write to a sharded file partitioned by window, using a
     * simple ParDo operation. Because there may be failures followed by retries, the
     * writes must be idempotent, but the details of writing to files is elided here.
     */
    keyedByWindow.apply(GroupByKey.<IntervalWindow, KV<String, Long>>create())
            .apply(ParDo.of(new WriteWindowedFilesDoFn(output)));

    PipelineResult result = pipeline.run();
    try {
        result.waitUntilFinish();
    } catch (Exception exc) {
        result.cancel();
    }
}

From source file:com.mastfrog.tinymavenproxy.Downloader.java

License:Open Source License

public ChannelFutureListener download(final Path path, final RequestID id, final DownloadReceiver receiver) {
    Collection<URL> urls = config.withPath(path);
    final Map<URL, ResponseFuture> futures = new ConcurrentHashMap<>();
    int size = urls.size();
    final AtomicInteger remaining = new AtomicInteger(size);
    final AtomicBoolean success = new AtomicBoolean();
    class RecvImpl implements Recv {

        @Override/*from w w w.ja  v a  2 s . c  o m*/
        public void onSuccess(URL u, File file, HttpResponseStatus status, HttpHeaders headers) {
            if (success.compareAndSet(false, true)) {
                try (Log<?> log = logger.info("download")) {
                    remaining.set(0);
                    for (Map.Entry<URL, ResponseFuture> e : futures.entrySet()) {
                        if (!u.equals(e.getKey())) {
                            e.getValue().cancel();
                        }
                    }
                    futures.clear();
                    String lastModified = headers.get(Headers.LAST_MODIFIED.name());
                    DateTime lm = null;
                    if (lastModified != null) {
                        lm = Headers.LAST_MODIFIED.toValue(lastModified);
                    }
                    File target = finder.put(path, file, lm);
                    log.add("from", u).add("size", file.length()).add("status", status.code())
                            .addIfNotNull("server", headers.get("Server")).add("id", id);
                    receiver.receive(status, target, headers);
                } catch (IOException ex) {
                    control.internalOnError(ex);
                }
            }
        }

        @Override
        public void onSuccess(URL u, ByteBuf buf, HttpResponseStatus status, HttpHeaders headers) {
            if (success.compareAndSet(false, true)) {
                try (Log<?> log = logger.info("download")) {
                    remaining.set(0);
                    for (Map.Entry<URL, ResponseFuture> e : futures.entrySet()) {
                        if (!u.equals(e.getKey())) {
                            e.getValue().cancel();
                        }
                    }
                    futures.clear();
                    String lastModified = headers.get(Headers.LAST_MODIFIED.name());
                    DateTime lm = null;
                    if (lastModified != null) {
                        lm = Headers.LAST_MODIFIED.toValue(lastModified);
                    }
                    finder.put(path, buf, lm);
                    log.add("from", u).add("size", buf.readableBytes()).add("status", status.code())
                            .addIfNotNull("server", headers.get("Server")).add("id", id);
                    receiver.receive(status, buf, headers);
                }
            }
        }

        final AtomicBoolean failed = new AtomicBoolean();

        @Override
        public void onFail(URL u, HttpResponseStatus status) {
            if (success.get() || !failed.compareAndSet(false, true)) {
                return;
            }
            int remain = remaining.decrementAndGet();
            ResponseFuture f = futures.get(u);
            futures.remove(u);
            if (f != null) {
                f.cancel();
            }
            if (remain == 0) {
                try (Log<?> log = logger.info("downloadFailed")) {
                    log.add("path", path).add("status", status).add("id", id);
                    receiver.failed(status == null ? HttpResponseStatus.NOT_FOUND : status);
                    failedURLs.put(path, path);
                }
            }
        }
    }
    for (final URL u : urls) {
        final RecvImpl impl = new RecvImpl();
        Receiver<State<?>> im = new RespHandler(u, impl);
        ResponseFuture f = client.get().setURL(u).setTimeout(Duration.standardMinutes(2)).onEvent(im)
                //                    .execute(new ResponseHandlerImpl(ByteBuf.class, u, impl));
                .dontAggregateResponse().execute();

        f.onAnyEvent(new Receiver<State<?>>() {

            @Override
            public void receive(State<?> t) {
                switch (t.stateType()) {
                case Closed:
                    impl.onFail(u, HttpResponseStatus.FORBIDDEN);
                    break;
                case HeadersReceived:
                    State.HeadersReceived hr = (State.HeadersReceived) t;
                    if (hr.get().getStatus().code() > 399) {
                        impl.onFail(u, hr.get().getStatus());
                    }
                }
            }

        });
        futures.put(u, f);
    }
    return new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            if (remaining.get() > 0) {
                for (ResponseFuture fu : futures.values()) {
                    fu.cancel();
                }
            }
        }
    };
}

From source file:com.nesscomputing.service.discovery.server.DiscoveryServerMain.java

License:Apache License

@Override
public Module getMainModule(final Config config) {
    return new AbstractModule() {
        @Override// w ww  .j  a  v  a 2 s.c om
        public void configure() {
            install(new BasicGalaxyServerModule(config));

            install(new ZookeeperModule(config));
            install(new DiscoveryClientModule(false));
            install(new NessQuartzModule(config));

            bind(ZookeeperCleanupJob.class);
            QuartzJobBinder.bindQuartzJob(binder(), ZookeeperCleanupJob.class).conditional("zookeeper-cleanup")
                    .delay(Duration.standardMinutes(1)).repeat(Duration.standardHours(8)).register();

            bind(DiscoveryServerConfig.class).toProvider(ConfigProvider.of(DiscoveryServerConfig.class))
                    .in(Scopes.SINGLETON);
            bind(ZookeeperJobProcessor.class).in(Scopes.SINGLETON);

            bind(StateOfTheWorldResource.class);
            bind(ServiceLookupResource.class);
            bind(StaticAnnouncementResource.class);

            bind(ConfigStaticAnnouncer.class).asEagerSingleton();
        }
    };
}

From source file:com.sg.infrastructure.AuthTokenServiceImpl.java

@Autowired
public AuthTokenServiceImpl(@Value("${com.sg.security.key}") String symmetricKey) {
    this.jwtAuthTokenService = new JwtAuthTokenService(symmetricKey, Duration.standardMinutes(1l));
}

From source file:com.sg.rest.webtoken.service.WebTokenServiceImpl.java

@Autowired
public WebTokenServiceImpl(@Value("${com.sg.security.key}") String symmetricKey) {
    this.authTokenComponent = new JwtAuthTokenComponent(symmetricKey, Duration.standardMinutes(2));
}

From source file:com.tomtom.speedtools.metrics.MetricsCollector.java

License:Apache License

@Nonnull
public static MetricsCollector create(@Nonnull final Period interval) {
    assert interval != null;
    switch (interval) {

    case LAST_MONTH:
        return new MetricsCollector(Duration.standardDays(30), 30);

    case LAST_WEEK:
        return new MetricsCollector(Duration.standardDays(7), 24 * 7);

    case LAST_DAY:
        return new MetricsCollector(Duration.standardDays(1), 48);

    case LAST_HOUR:
        return new MetricsCollector(Duration.standardHours(1), 60);

    case LAST_MINUTE:
        return new MetricsCollector(Duration.standardMinutes(1), 30);
    }//  w w w. j ava2 s. c om
    assert false;
    throw new IllegalStateException();
}

From source file:de.fraunhofer.iosb.ilt.tests.CreateEntities.java

License:Open Source License

private void createEntities() throws ServiceFailureException, URISyntaxException, MalformedURLException {
    Map<String, Object> deep1 = new HashMap<>();
    Map<String, Object> deep2 = new HashMap<>();
    Map<String, Object> properties1 = new HashMap<>();
    properties1.put("name", "properties1");
    properties1.put("string", "yes");
    properties1.put("boolean", true);
    properties1.put("integer", 9);
    properties1.put("array", new int[] { 1, 2, 3, 4 });
    properties1.put("array2", new int[][] { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 } });
    properties1.put("array3", new Object[] { deep1, deep2 });
    deep1.put("deep1.1", new int[] { 1, 2, 3, 4 });
    deep1.put("deep1.2", new int[] { 2, 3, 4, 5 });
    deep2.put("string", "yes");
    deep2.put("boolean", true);
    deep2.put("integer", 9);
    Thing thing = new Thing("Thing 1", "The first thing.");
    thing.setProperties(properties1);/*from www.  j a va2s.  c  o  m*/
    service.create(thing);
    things.add(thing);

    Map<String, Object> properties2 = new HashMap<>();
    properties2.put("name", "properties2");
    properties2.put("string", "no");
    properties2.put("boolean", false);
    properties2.put("integer", 10);
    properties2.put("array", new int[] { 4, 3, 2, 1 });
    properties2.put("deep", properties1);
    thing = new Thing("Thing 2", "The second thing.");
    thing.setProperties(properties2);
    service.create(thing);
    things.add(thing);

    Map<String, Object> properties3 = new HashMap<>();
    properties3.put("name", "properties3");
    properties3.put("string", "yes");
    properties3.put("boolean", true);
    properties3.put("integer", 11);
    properties3.put("array", new int[] { 2, 1, 4, 3 });
    thing = new Thing("Thing 3", "The third thing.");
    thing.setProperties(properties3);
    service.create(thing);
    things.add(thing);

    Map<String, Object> properties4 = new HashMap<>();
    properties4.put("name", "properties4");
    properties4.put("string", "no");
    properties4.put("boolean", false);
    properties4.put("integer", 1);
    properties4.put("array", new int[] { 3, 4, 1, 2 });
    thing = new Thing("Thing 4", "The fourt thing.");
    thing.setProperties(properties4);
    service.create(thing);
    things.add(thing);

    Location location = new Location("Location 1.0", "First Location of Thing 1.", "application/vnd.geo+json",
            new Point(8, 52));
    location.getThings().add(things.get(0));
    service.create(location);
    locations.add(location);

    location = new Location("Location 1.1", "Second Location of Thing 1.", "application/vnd.geo+json",
            new Point(8, 52));
    location.getThings().add(things.get(0));
    service.create(location);
    locations.add(location);

    location = new Location("Location 2", "Location of Thing 2.", "application/vnd.geo+json", new Point(8, 53));
    location.getThings().add(things.get(1));
    service.create(location);
    locations.add(location);

    location = new Location("Location 3", "Location of Thing 3.", "application/vnd.geo+json", new Point(8, 54));
    location.getThings().add(things.get(2));
    service.create(location);
    locations.add(location);

    // Locations 4
    location = new Location("Location 4", "Location of Thing 4.", "application/vnd.geo+json", new Polygon(
            new LngLatAlt(8, 53), new LngLatAlt(7, 52), new LngLatAlt(7, 53), new LngLatAlt(8, 53)));
    location.getThings().add(things.get(3));
    service.create(location);
    locations.add(location);

    // Locations 5
    location = new Location("Location 5", "A line.", "application/vnd.geo+json",
            new LineString(new LngLatAlt(5, 52), new LngLatAlt(5, 53)));
    service.create(location);
    locations.add(location);

    // Locations 6
    location = new Location("Location 6", "A longer line.", "application/vnd.geo+json",
            new LineString(new LngLatAlt(5, 52), new LngLatAlt(6, 53)));
    service.create(location);
    locations.add(location);

    // Locations 7
    location = new Location("Location 7", "The longest line.", "application/vnd.geo+json",
            new LineString(new LngLatAlt(4, 52), new LngLatAlt(8, 52)));
    service.create(location);
    locations.add(location);

    Sensor sensor1 = new Sensor("Sensor 1", "The first sensor.", "text", "Some metadata.");
    service.create(sensor1);
    sensors.add(sensor1);

    Sensor sensor2 = new Sensor("Sensor 2", "The second sensor.", "text", "Some metadata.");
    service.create(sensor2);
    sensors.add(sensor2);

    ObservedProperty obsProp1 = new ObservedProperty("Temperature", new URI("http://ucom.org/temperature"),
            "The temperature of the thing.");
    service.create(obsProp1);
    oProps.add(obsProp1);

    ObservedProperty obsProp2 = new ObservedProperty("Humidity", new URI("http://ucom.org/humidity"),
            "The humidity of the thing.");
    service.create(obsProp2);
    oProps.add(obsProp2);

    thing = things.get(0);
    Datastream datastream1 = new Datastream("Datastream 1", "The temperature of thing 1, sensor 1.", "someType",
            new UnitOfMeasurement("degree celcius", "C", "ucum:T"));
    datastream1.setThing(thing);
    datastream1.setSensor(sensor1);
    datastream1.setObservedProperty(obsProp1);
    service.create(datastream1);
    datastreams.add(datastream1);

    Datastream datastream2 = new Datastream("Datastream 2", "The humidity of thing 1, sensor 2.", "someType",
            new UnitOfMeasurement("relative humidity", "%", "ucum:Humidity"));
    datastream2.setThing(thing);
    datastream2.setSensor(sensor2);
    datastream2.setObservedProperty(obsProp2);
    service.create(datastream2);
    datastreams.add(datastream2);

    Datastream datastream3 = new Datastream("Datastream 3", "The humidity of thing 2, sensor 2.", "someType",
            new UnitOfMeasurement("relative humidity", "%", "ucum:Humidity"));
    datastream3.setThing(things.get(1));
    datastream3.setSensor(sensor2);
    datastream3.setObservedProperty(obsProp2);
    service.create(datastream3);
    datastreams.add(datastream3);

    Observation o = new Observation(new int[] { 1, 2, 3, 4 }, datastream1);
    o.setPhenomenonTimeFrom(ZonedDateTime.parse("2016-01-01T01:01:01.000Z"));
    o.setValidTime(
            Interval.of(Instant.parse("2016-01-01T01:01:01.000Z"), Instant.parse("2016-01-01T23:59:59.999Z")));
    service.create(o);
    observations.add(o);

    o = new Observation(2, datastream1);
    o.setPhenomenonTimeFrom(ZonedDateTime.parse("2016-01-02T01:01:01.000Z"));
    o.setValidTime(
            Interval.of(Instant.parse("2016-01-02T01:01:01.000Z"), Instant.parse("2016-01-02T23:59:59.999Z")));
    service.create(o);
    observations.add(o);

    o = new Observation(3, datastream1);
    o.setPhenomenonTimeFrom(ZonedDateTime.parse("2016-01-03T01:01:01.000Z"));
    o.setValidTime(
            Interval.of(Instant.parse("2016-01-03T01:01:01.000Z"), Instant.parse("2016-01-03T23:59:59.999Z")));
    service.create(o);
    observations.add(o);

    o = new Observation(4, datastream1);
    o.setPhenomenonTimeFrom(ZonedDateTime.parse("2016-01-04T01:01:01.000Z"));
    o.setValidTime(
            Interval.of(Instant.parse("2016-01-04T01:01:01.000Z"), Instant.parse("2016-01-04T23:59:59.999Z")));
    service.create(o);
    observations.add(o);

    o = new Observation(5, datastream2);
    o.setPhenomenonTimeFrom(ZonedDateTime.parse("2016-01-04T01:01:01.000Z"));
    o.setValidTime(
            Interval.of(Instant.parse("2016-01-04T01:01:01.000Z"), Instant.parse("2016-01-04T23:59:59.999Z")));
    service.create(o);
    observations.add(o);

    {
        Map<String, Object> parameters = new HashMap<>();
        o = new Observation("bad", datastream1);
        parameters.put("int", generateString(0, 10));
        parameters.put("string", 0 % 2 == 0);
        parameters.put("boolean", 0);
        parameters.put("intArray", generateIntArray(0, 5));
        parameters.put("intIntArray", generateIntIntArray(0, 3));
        parameters.put("objArray", generateObjectList(0, 3));
        o.setParameters(parameters);
        service.create(o);
        observations.add(o);
    }

    ExecutorService pool = Executors.newFixedThreadPool(5);

    int totalCount = OBSERVATION_COUNT;
    int perTask = 10000;

    long startTime = Calendar.getInstance().getTimeInMillis();
    Duration delta = Duration.standardMinutes(1);
    DateTime dtStart = DateTime.now().minus(delta.multipliedBy(totalCount));

    int start = 0;
    while (start < totalCount) {
        if (start + perTask >= totalCount) {
            perTask = totalCount - start;
        }
        obsCreator obsCreator = new obsCreator(
                new SensorThingsService(new URL(Constants.BASE_URL)).setTokenManager(service.getTokenManager()),
                datastream1, start, perTask, dtStart, delta);
        pool.submit(obsCreator);
        LOGGER.info("Submitted task for {} observations starting at {}.", perTask, start);
        start += perTask;
    }
    try {
        pool.shutdown();
        pool.awaitTermination(1, TimeUnit.HOURS);
    } catch (InterruptedException ex) {
        LOGGER.info("Pool prepaturely interrupted.", ex);
    }

    long endTime = Calendar.getInstance().getTimeInMillis();
    long duration = endTime - startTime;
    double secs = duration / 1000.0;
    LOGGER.info("Created {} obs in {}ms, {}/s.", totalCount, duration, totalCount / secs);
}

From source file:domains.donuts.config.DonutsRegistryConfig.java

License:Open Source License

@Override
public Duration getSingletonCacheRefreshDuration() {
    return Duration.standardMinutes(10);
}

From source file:eu.aylett.skyscanner.logparse.LogGlobalAggregator.java

License:Apache License

@JsonProperty
public long getDurationInMinutes() {
    return new Duration(earliest, latest.plus(Duration.standardMinutes(1))).getStandardMinutes();
}

From source file:fi.hsl.parkandride.core.service.PredictionService.java

License:EUPL

@Scheduled(cron = "0 */5 * * * *") // every 5 minutes to match PredictionDao.PREDICTION_RESOLUTION
public void updatePredictions() {
    Optional<Lock> lock = Optional.empty();
    try {/*from   w w  w.  j a  v a2 s . co  m*/
        lock = Optional.of(lockRepository.acquireLock("update-predictions", Duration.standardMinutes(2)));
        doUpdatePredictions();
    } catch (LockException e) {
        log.debug("Failed to get lock for updating predictions - another node updates them.");
        return;
    } finally {
        lock.ifPresent(l -> lockRepository.releaseLock(l));
    }
}