@Test
  public void testIdempotent() throws Exception {
    String uri = "file:target/test-classes/idempotent?idempotent=true&delay=10";
    context
        .getRouteDefinitions()
        .get(1)
        .adviceWith(
            context,
            new AdviceWithRouteBuilder() {
              @Override
              public void configure() throws Exception {
                replaceFromWith(uri);
                weaveByType(ToDefinition.class).selectFirst().replace().to("mock:endpoint");
              }
            });
    MockEndpoint end = context.getEndpoint("mock:endpoint", MockEndpoint.class);
    end.expectedMessageCount(1);

    producer.sendBodyAndHeader(
        "file://target/test-classes/idempotent", Exchange.FILE_NAME, "FCOO1.nc");

    end.assertIsSatisfied();

    String fileName =
        (String) end.getReceivedExchanges().get(0).getIn().getHeader(Exchange.FILE_NAME_ONLY);
    assertEquals(fileName, "FCOO1.nc");

    // reset the mock
    end.reset();
    end.expectedMessageCount(0);

    // move file back
    File file = new File("target/test-classes/idempotent/.camel/FCOO1.nc");
    File renamed = new File("target/test-classes/idempotent/FCOO1.nc");
    file.renameTo(renamed);

    producer.sendBodyAndHeader(
        "file://target/test-classes/idempotent", Exchange.FILE_NAME, "FCOO1.nc");

    // let some time pass to let the consumer try to consume even though it cannot
    Thread.sleep(100);
    end.assertIsSatisfied();

    FileEndpoint fe = context.getEndpoint(uri, FileEndpoint.class);
    assertNotNull(fe);

    // Make sure that there are no incoming messages
    MemoryIdempotentRepository repo = (MemoryIdempotentRepository) fe.getInProgressRepository();
    assertEquals("Should be no in-progress files", 0, repo.getCacheSize());
  }
Esempio n. 2
0
  @Override
  public RemoteFileConsumer<T> createConsumer(Processor processor) throws Exception {
    afterPropertiesSet();
    RemoteFileConsumer<T> consumer = buildConsumer(processor);

    if (isDelete() && getMove() != null) {
      throw new IllegalArgumentException("You cannot both set delete=true and move options");
    }

    // if noop=true then idempotent should also be configured
    if (isNoop() && !isIdempotentSet()) {
      log.info(
          "Endpoint is configured with noop=true so forcing endpoint to be idempotent as well");
      setIdempotent(true);
    }

    // if idempotent and no repository set then create a default one
    if (isIdempotentSet() && isIdempotent() && idempotentRepository == null) {
      log.info(
          "Using default memory based idempotent repository with cache max size: "
              + DEFAULT_IDEMPOTENT_CACHE_SIZE);
      idempotentRepository =
          MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
    }

    if (!getConfiguration().isUseList() && getFileName() == null) {
      throw new IllegalArgumentException(
          "Endpoint is configured with useList=false, then fileName must be configured also");
    }

    // set max messages per poll
    consumer.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
    consumer.setEagerLimitMaxMessagesPerPoll(isEagerMaxMessagesPerPoll());

    configureConsumer(consumer);
    return consumer;
  }
 private MyIdempotentRepo() {
   delegate = MemoryIdempotentRepository.memoryIdempotentRepository(200);
 }