List of usage examples for org.joda.time Duration multipliedBy
public Duration multipliedBy(long multiplicand)
From source file:com.google.cloud.pubsub.PollingSubscriberConnection.java
License:Open Source License
private void pullMessages(final Duration backoff) { ListenableFuture<PullResponse> pullResult = stub .withDeadlineAfter(DEFAULT_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS) .pull(PullRequest.newBuilder().setSubscription(subscription).setMaxMessages(DEFAULT_MAX_MESSAGES) .setReturnImmediately(true).build()); Futures.addCallback(pullResult, new FutureCallback<PullResponse>() { @Override//from ww w . j av a2 s. c o m public void onSuccess(PullResponse pullResponse) { processReceivedMessages(pullResponse.getReceivedMessagesList()); if (pullResponse.getReceivedMessagesCount() == 0) { // No messages in response, possibly caught up in backlog, we backoff to avoid // slamming the server. executor.schedule(new Runnable() { @Override public void run() { Duration newBackoff = backoff.multipliedBy(2); if (newBackoff.isLongerThan(MAX_BACKOFF)) { newBackoff = MAX_BACKOFF; } pullMessages(newBackoff); } }, backoff.getMillis(), TimeUnit.MILLISECONDS); return; } pullMessages(INITIAL_BACKOFF); } @Override public void onFailure(Throwable cause) { if (!(cause instanceof StatusRuntimeException) || isRetryable(((StatusRuntimeException) cause).getStatus())) { logger.error("Failed to pull messages (recoverable): " + cause.getMessage(), cause); executor.schedule(new Runnable() { @Override public void run() { Duration newBackoff = backoff.multipliedBy(2); if (newBackoff.isLongerThan(MAX_BACKOFF)) { newBackoff = MAX_BACKOFF; } pullMessages(newBackoff); } }, backoff.getMillis(), TimeUnit.MILLISECONDS); return; } notifyFailed(cause); } }); }
From source file:com.google.cloud.pubsub.spi.v1.PollingSubscriberConnection.java
License:Open Source License
private void pullMessages(final Duration backoff) { ListenableFuture<PullResponse> pullResult = stub .withDeadlineAfter(DEFAULT_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS) .pull(PullRequest.newBuilder().setSubscription(subscription).setMaxMessages(DEFAULT_MAX_MESSAGES) .setReturnImmediately(true).build()); Futures.addCallback(pullResult, new FutureCallback<PullResponse>() { @Override/*from w w w . j a v a2 s . c o m*/ public void onSuccess(PullResponse pullResponse) { messageDispatcher.processReceivedMessages(pullResponse.getReceivedMessagesList()); if (pullResponse.getReceivedMessagesCount() == 0) { // No messages in response, possibly caught up in backlog, we backoff to avoid // slamming the server. executor.schedule(new Runnable() { @Override public void run() { Duration newBackoff = backoff.multipliedBy(2); if (newBackoff.isLongerThan(MAX_BACKOFF)) { newBackoff = MAX_BACKOFF; } pullMessages(newBackoff); } }, backoff.getMillis(), TimeUnit.MILLISECONDS); return; } pullMessages(INITIAL_BACKOFF); } @Override public void onFailure(Throwable cause) { if (!(cause instanceof StatusRuntimeException) || isRetryable(((StatusRuntimeException) cause).getStatus())) { logger.log(Level.SEVERE, "Failed to pull messages (recoverable): ", cause); executor.schedule(new Runnable() { @Override public void run() { Duration newBackoff = backoff.multipliedBy(2); if (newBackoff.isLongerThan(MAX_BACKOFF)) { newBackoff = MAX_BACKOFF; } pullMessages(newBackoff); } }, backoff.getMillis(), TimeUnit.MILLISECONDS); return; } notifyFailed(cause); } }); }
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 w w w . j ava2 s .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:julian.lylly.model.Prospect.java
public List<Pair<Duration, Duration>> getBudgets(LocalDate pointer, Duration timespent) { int relDay = Days.daysBetween(start, pointer).getDays(); if (pointer.isBefore(start) || !end.isAfter(pointer)) { throw new IllegalArgumentException("day is out of range:\t" + "start = " + start.toString() + "\t" + "pointer = " + pointer.toString() + "\t" + "end = " + end.toString()); }//from w w w.java 2 s . c o m List<Integer> subl = weights.subList(relDay, weights.size()); int sum = 0; for (int k : subl) { sum += k; } Duration minleft = Util.max(Duration.ZERO, min.minus(timespent)); Duration maxleft = Util.max(Duration.ZERO, max.minus(timespent)); List<Pair<Duration, Duration>> res = new ArrayList<>(); for (int k : subl) { Duration bMin = minleft.multipliedBy(k).dividedBy(sum); Duration bMax = maxleft.multipliedBy(k).dividedBy(sum); res.add(new Pair(bMin, bMax)); } return res; }