Skip to content

Instantly share code, notes, and snippets.

@joost-de-vries
Last active July 5, 2016 12:32
Show Gist options
  • Select an option

  • Save joost-de-vries/cd32ceb0a0cc9cc685e559c770dec414 to your computer and use it in GitHub Desktop.

Select an option

Save joost-de-vries/cd32ceb0a0cc9cc685e559c770dec414 to your computer and use it in GitHub Desktop.
public Mono<Void> gitterSlackRelay() {
ObjectMapper mapper = new ObjectMapper();
return create()
.get(gitterStreamUrl, gitterStreamHandler())
.flatMap(replies -> replies
.receiveByteArray()
.filter(b -> b.length > 2) // ignore gitter keep-alives (\r)
.map(b -> {
try {
return mapper.readValue(b, Map.class);
}
catch (IOException e) {
throw Exceptions.propagate(e);
}
}) // ObjectMapper.readValue(Map
// .class)
.window(10, 1_000) // microbatch 10 items or 1s worth into individual streams (for reduce ops)
.flatMap(w -> postToSlack(
w.map(m -> formatLink(m) + ": " + formatText(m))
.reduce("", GitterSlackRelayApplication::appendLines))
)
)
.then(); // only complete when all windows have completed AND gitter GET connection has closed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment