Exemplo n.º 1
0
  @Test
  public void testRouteFileToFile() throws Exception {
    deleteDirectory("target/file2file");
    NotifyBuilder notify =
        new NotifyBuilder(context).from("activemq:queue:hello").whenDone(1).create();

    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);

    template.sendBodyAndHeader(
        "file://target/file2file/in", "Hello World", Exchange.FILE_NAME, "hello.txt");

    assertMockEndpointsSatisfied();

    notify.matchesMockWaitTime();

    File file = new File("./target/file2file/out/hello.txt");
    file = file.getAbsoluteFile();
    assertTrue("The file should exists", file.exists());
  }
  private void doSendMessages(int files, int poolSize) throws Exception {
    Mailbox.clearAll();

    NotifyBuilder builder = new NotifyBuilder(context).whenDone(files).create();

    getMockEndpoint("mock:result").expectedMessageCount(files);
    getMockEndpoint("mock:result").expectsNoDuplicates(body());

    final CountDownLatch latch = new CountDownLatch(files);
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    for (int i = 0; i < files; i++) {
      final int index = i;
      executor.submit(
          new Callable<Object>() {
            public Object call() throws Exception {
              template.sendBodyAndHeader(
                  "direct:start", "Message " + index, "To", "someone@localhost");
              latch.countDown();
              return null;
            }
          });
    }

    // wait first for all the exchanges above to be thoroughly sent asynchronously
    assertTrue(latch.await(5, TimeUnit.SECONDS));

    assertMockEndpointsSatisfied();
    assertTrue(builder.matchesMockWaitTime());

    Mailbox box = Mailbox.get("someone@localhost");
    assertEquals(files, box.size());

    // as we use concurrent producers the mails can arrive out of order
    Set<Object> bodies = new HashSet<Object>();
    for (int i = 0; i < files; i++) {
      bodies.add(box.get(i).getContent());
    }

    assertEquals("There should be " + files + " unique mails", files, bodies.size());
    executor.shutdownNow();
  }