// private void multiThreadedMarshal(PrintWriter writer, List<Result> results,
  private String multiThreadedMarshal(
      List<Result> results, String recordSchema, final Map<String, Serializable> arguments)
      throws CatalogTransformerException {

    CompletionService<BinaryContent> completionService =
        new ExecutorCompletionService<>(queryExecutor);

    try {
      final MetacardTransformer transformer =
          metacardTransformerManager.getTransformerBySchema(recordSchema);
      if (transformer == null) {
        throw new CatalogTransformerException(
            "Cannot find transformer for schema: " + recordSchema);
      }

      Map<Future<BinaryContent>, Result> futures = new HashMap<>(results.size());
      for (Result result : results) {
        final Metacard mc = result.getMetacard();

        // the "current" thread will run submitted task when queueSize exceeded; effectively
        // blocking enqueue of more tasks.
        futures.put(
            completionService.submit(
                () -> {
                  BinaryContent content = transformer.transform(mc, arguments);
                  return content;
                }),
            result);
      }

      // TODO - really? I can't use a list of some sort?
      InputStream[] contents = new InputStream[results.size()];

      while (!futures.isEmpty()) {
        Future<BinaryContent> completedFuture = completionService.take();
        int index = results.indexOf(futures.get(completedFuture));
        contents[index] = completedFuture.get().getInputStream();
        futures.remove(completedFuture);
      }

      CharArrayWriter accum = new CharArrayWriter(ACCUM_INITIAL_SIZE);
      for (InputStream is : contents) {
        IOUtils.copy(is, accum);
      }

      return accum.toString();

    } catch (IOException | InterruptedException | ExecutionException xe) {
      throw new CatalogTransformerException(xe);
    }
  } // end multiThreadedMarshal()
Beispiel #2
0
  @Test(expected = PluginExecutionException.class)
  public void testCreateBadTransform()
      throws PluginExecutionException, CatalogTransformerException, IOException, IngestException,
          SourceUnavailableException {
    // given
    when(transformer.transform(isA(Metacard.class), isNull(Map.class)))
        .thenThrow(CatalogTransformerException.class);
    CreateResponse createResponse =
        new CreateResponseImpl(new CreateRequestImpl(metacard), null, Arrays.asList(metacard));

    // when
    plugin.process(createResponse);
  }
Beispiel #3
0
  @Before
  public void setup() {
    // given
    plugin = new RestReplicatorPlugin(ENDPOINT_ADDRESS);
    transformer = mock(MetacardTransformer.class);
    BinaryContent bc = mock(BinaryContent.class);
    byte[] bytes = {86};
    try {
      when(bc.getByteArray()).thenReturn(bytes);
      when(transformer.transform(isA(Metacard.class), isNull(Map.class))).thenReturn(bc);
    } catch (Exception e) {
      Assert.fail(e.getLocalizedMessage());
    }

    plugin.setTransformer(transformer);
    metacard = getMockMetacard();
  }