private void recordTemperature()

in pekko-sample-sharding-java/killrweather-fog/src/main/java/sample/killrweather/fog/WeatherStation.java [93:117]


  private void recordTemperature(long eventTime, double value) throws Exception {
    Data data = new Data(eventTime, "temperature", value);

    // FIXME no Java API in Apache Pekko HTTP to do this using the marshalling infra, see pekko-http#2128
    String json = objectMapper.writeValueAsString(data);
    CompletionStage<String> futureResponseBody =
        http.singleRequest(HttpRequest.POST(stationUrl).withEntity(ContentTypes.APPLICATION_JSON, json))
          .thenCompose(response ->
            Unmarshaller.entityToString().unmarshal(response.entity(), SystemMaterializer.get(getContext().getSystem()).materializer())
              .thenApply(body -> {
                if (response.status().isSuccess())
                  return body;
                else throw new RuntimeException("Failed to register data: " + body);
              })
          );

    getContext().pipeToSelf(futureResponseBody, (response, failure) -> {
      if (failure == null) {
        return new ProcessSuccess(response);
      } else {
        return new ProcessFailure(failure);
      }
    });

  }