Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Criando um endpoint de streaming

Criando um endpoint de streaming

Como criar um endpoint stream utilizando Spring Boot.

Felipe Mamud

October 19, 2017
Tweet

More Decks by Felipe Mamud

Other Decks in Programming

Transcript

  1. DESAFIOS • Qual implementação utilizar? ◦ Java? outra linguagem? ◦

    Server-Sent Events ◦ StreamingResponseBody* ◦ Custom • ES SearchScroll (com shards) • Sized stream
  2. CONTROLLER @RequestMapping(value = "/{index}/stream", method = GET) public StreamingResponseBody stream(

    FilterableApiRequest request, HttpServletResponse httpServletResponse) { httpServletResponse .setContentType("application/x-ndjson;charset=UTF-8"); return out -> searchService.stream(request, out); }
  3. @Component public class ElasticSearchStream { public void stream(OutputStream stream) {

    ResponseStream.iterate(stream, new SearchApiIterator<>(esClient, requestBuilder(), scroll -> scroll.setScroll(keepAlive) .actionGet(streamTimeout), count), searchHit -> toBytes(searchHit.getSourceRef())); } } ELASTICSEARCH STREAM
  4. RESPONSE STREAM public static <T> void iterate(OutputStream stream, Iterator<T[]> iterator,

    Function<T, byte[]> byteFn) { while (iterator.hasNext()) { for (T hit: iterator.next()) { stream.write(byteFn.apply(hit)); stream.write('\n'); } stream.flush(); } }
  5. SEARCH ITERATOR public class SearchApiIterator<T> implements Iterator<T[]> { @Override public

    boolean hasNext() { return hits() > 0 && count <= size; } @Override public T[] next() { T[] result = (T[]) response.getHits().getHits(); response = loop.apply(client.prepareSearchScroll(response.getScrollId())); this.count += hits(); return result; } }|