Search in sources :

Example 1 with IdempotentRepository

use of org.apache.camel.spi.IdempotentRepository in project camel by apache.

the class IdempotentConsumerEagerTest method testEager.

public void testEager() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            final IdempotentRepository<String> repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
            from("direct:start").idempotentConsumer(header("messageId"), repo).eager(true).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String id = exchange.getIn().getHeader("messageId", String.class);
                    // should contain
                    assertTrue("Should eager add to repo", repo.contains(id));
                }
            }).to("mock:result");
        }
    });
    context.start();
    resultEndpoint.expectedBodiesReceived("one", "two", "three");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("3", "three");
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MemoryIdempotentRepository(org.apache.camel.processor.idempotent.MemoryIdempotentRepository) IdempotentRepository(org.apache.camel.spi.IdempotentRepository)

Example 2 with IdempotentRepository

use of org.apache.camel.spi.IdempotentRepository in project camel by apache.

the class IdempotentConsumerEagerTest method testNotEager.

public void testNotEager() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            final IdempotentRepository<String> repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
            from("direct:start").idempotentConsumer(header("messageId"), repo).eager(false).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String id = exchange.getIn().getHeader("messageId", String.class);
                    // should not contain
                    assertFalse("Should not eager add to repo", repo.contains(id));
                }
            }).to("mock:result");
        }
    });
    context.start();
    resultEndpoint.expectedBodiesReceived("one", "two", "three");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("3", "three");
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MemoryIdempotentRepository(org.apache.camel.processor.idempotent.MemoryIdempotentRepository) IdempotentRepository(org.apache.camel.spi.IdempotentRepository)

Example 3 with IdempotentRepository

use of org.apache.camel.spi.IdempotentRepository in project camel by apache.

the class IdempotentConsumerTest method testNotSkiDuplicateWithFilter.

public void testNotSkiDuplicateWithFilter() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            IdempotentRepository<String> repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
            // START SNIPPET: e1
            from("direct:start").idempotentConsumer(header("messageId")).messageIdRepository(repo).skipDuplicate(false).filter(property(Exchange.DUPLICATE_MESSAGE).isEqualTo(true)).to("mock:duplicate").stop().end().to("mock:result");
        // END SNIPPET: e1
        }
    });
    context.start();
    resultEndpoint.expectedBodiesReceived("one", "two", "three");
    getMockEndpoint("mock:duplicate").expectedBodiesReceived("one", "two", "one");
    getMockEndpoint("mock:duplicate").allMessages().exchangeProperty(Exchange.DUPLICATE_MESSAGE).isEqualTo(Boolean.TRUE);
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("3", "three");
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MemoryIdempotentRepository(org.apache.camel.processor.idempotent.MemoryIdempotentRepository) IdempotentRepository(org.apache.camel.spi.IdempotentRepository)

Example 4 with IdempotentRepository

use of org.apache.camel.spi.IdempotentRepository in project camel by apache.

the class IdempotentConsumerTest method testNotSkiDuplicate.

public void testNotSkiDuplicate() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            IdempotentRepository<String> repo = MemoryIdempotentRepository.memoryIdempotentRepository(200);
            from("direct:start").idempotentConsumer(header("messageId")).messageIdRepository(repo).skipDuplicate(false).to("mock:result");
        }
    });
    context.start();
    resultEndpoint.expectedBodiesReceived("one", "two", "one", "two", "one", "three");
    resultEndpoint.message(0).exchangeProperty(Exchange.DUPLICATE_MESSAGE).isNull();
    resultEndpoint.message(1).exchangeProperty(Exchange.DUPLICATE_MESSAGE).isNull();
    resultEndpoint.message(2).exchangeProperty(Exchange.DUPLICATE_MESSAGE).isEqualTo(Boolean.TRUE);
    resultEndpoint.message(3).exchangeProperty(Exchange.DUPLICATE_MESSAGE).isEqualTo(Boolean.TRUE);
    resultEndpoint.message(4).exchangeProperty(Exchange.DUPLICATE_MESSAGE).isEqualTo(Boolean.TRUE);
    resultEndpoint.message(5).exchangeProperty(Exchange.DUPLICATE_MESSAGE).isNull();
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("3", "three");
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MemoryIdempotentRepository(org.apache.camel.processor.idempotent.MemoryIdempotentRepository) IdempotentRepository(org.apache.camel.spi.IdempotentRepository)

Example 5 with IdempotentRepository

use of org.apache.camel.spi.IdempotentRepository in project camel by apache.

the class FileProcessStrategyFactory method getExclusiveReadLockStrategy.

@SuppressWarnings("unchecked")
private static GenericFileExclusiveReadLockStrategy<File> getExclusiveReadLockStrategy(Map<String, Object> params) {
    GenericFileExclusiveReadLockStrategy<File> strategy = (GenericFileExclusiveReadLockStrategy<File>) params.get("exclusiveReadLockStrategy");
    if (strategy != null) {
        return strategy;
    }
    // no explicit strategy set then fallback to readLock option
    String readLock = (String) params.get("readLock");
    if (ObjectHelper.isNotEmpty(readLock)) {
        if ("none".equals(readLock) || "false".equals(readLock)) {
            return null;
        } else if ("markerFile".equals(readLock)) {
            strategy = new MarkerFileExclusiveReadLockStrategy();
        } else if ("fileLock".equals(readLock)) {
            strategy = new FileLockExclusiveReadLockStrategy();
        } else if ("rename".equals(readLock)) {
            strategy = new FileRenameExclusiveReadLockStrategy();
        } else if ("changed".equals(readLock)) {
            FileChangedExclusiveReadLockStrategy readLockStrategy = new FileChangedExclusiveReadLockStrategy();
            Long minLength = (Long) params.get("readLockMinLength");
            if (minLength != null) {
                readLockStrategy.setMinLength(minLength);
            }
            Long minAge = (Long) params.get("readLockMinAge");
            if (null != minAge) {
                readLockStrategy.setMinAge(minAge);
            }
            strategy = readLockStrategy;
        } else if ("idempotent".equals(readLock)) {
            FileIdempotentRepositoryReadLockStrategy readLockStrategy = new FileIdempotentRepositoryReadLockStrategy();
            Boolean readLockRemoveOnRollback = (Boolean) params.get("readLockRemoveOnRollback");
            if (readLockRemoveOnRollback != null) {
                readLockStrategy.setRemoveOnRollback(readLockRemoveOnRollback);
            }
            Boolean readLockRemoveOnCommit = (Boolean) params.get("readLockRemoveOnCommit");
            if (readLockRemoveOnCommit != null) {
                readLockStrategy.setRemoveOnCommit(readLockRemoveOnCommit);
            }
            IdempotentRepository repo = (IdempotentRepository) params.get("readLockIdempotentRepository");
            if (repo != null) {
                readLockStrategy.setIdempotentRepository(repo);
            }
            strategy = readLockStrategy;
        } else if ("idempotent-changed".equals(readLock)) {
            FileIdempotentChangedRepositoryReadLockStrategy readLockStrategy = new FileIdempotentChangedRepositoryReadLockStrategy();
            Boolean readLockRemoveOnRollback = (Boolean) params.get("readLockRemoveOnRollback");
            if (readLockRemoveOnRollback != null) {
                readLockStrategy.setRemoveOnRollback(readLockRemoveOnRollback);
            }
            Boolean readLockRemoveOnCommit = (Boolean) params.get("readLockRemoveOnCommit");
            if (readLockRemoveOnCommit != null) {
                readLockStrategy.setRemoveOnCommit(readLockRemoveOnCommit);
            }
            IdempotentRepository repo = (IdempotentRepository) params.get("readLockIdempotentRepository");
            if (repo != null) {
                readLockStrategy.setIdempotentRepository(repo);
            }
            Long minLength = (Long) params.get("readLockMinLength");
            if (minLength != null) {
                readLockStrategy.setMinLength(minLength);
            }
            Long minAge = (Long) params.get("readLockMinAge");
            if (null != minAge) {
                readLockStrategy.setMinAge(minAge);
            }
            strategy = readLockStrategy;
        } else if ("idempotent-rename".equals(readLock)) {
            FileIdempotentRenameRepositoryReadLockStrategy readLockStrategy = new FileIdempotentRenameRepositoryReadLockStrategy();
            Boolean readLockRemoveOnRollback = (Boolean) params.get("readLockRemoveOnRollback");
            if (readLockRemoveOnRollback != null) {
                readLockStrategy.setRemoveOnRollback(readLockRemoveOnRollback);
            }
            Boolean readLockRemoveOnCommit = (Boolean) params.get("readLockRemoveOnCommit");
            if (readLockRemoveOnCommit != null) {
                readLockStrategy.setRemoveOnCommit(readLockRemoveOnCommit);
            }
            IdempotentRepository repo = (IdempotentRepository) params.get("readLockIdempotentRepository");
            if (repo != null) {
                readLockStrategy.setIdempotentRepository(repo);
            }
            strategy = readLockStrategy;
        }
        if (strategy != null) {
            Long timeout = (Long) params.get("readLockTimeout");
            if (timeout != null) {
                strategy.setTimeout(timeout);
            }
            Long checkInterval = (Long) params.get("readLockCheckInterval");
            if (checkInterval != null) {
                strategy.setCheckInterval(checkInterval);
            }
            LoggingLevel readLockLoggingLevel = (LoggingLevel) params.get("readLockLoggingLevel");
            if (readLockLoggingLevel != null) {
                strategy.setReadLockLoggingLevel(readLockLoggingLevel);
            }
            Boolean readLockMarkerFile = (Boolean) params.get("readLockMarkerFile");
            if (readLockMarkerFile != null) {
                strategy.setMarkerFiler(readLockMarkerFile);
            }
            Boolean readLockDeleteOrphanLockFiles = (Boolean) params.get("readLockDeleteOrphanLockFiles");
            if (readLockDeleteOrphanLockFiles != null) {
                strategy.setDeleteOrphanLockFiles(readLockDeleteOrphanLockFiles);
            }
        }
    }
    return strategy;
}
Also used : GenericFileExclusiveReadLockStrategy(org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy) LoggingLevel(org.apache.camel.LoggingLevel) IdempotentRepository(org.apache.camel.spi.IdempotentRepository) File(java.io.File)

Aggregations

IdempotentRepository (org.apache.camel.spi.IdempotentRepository)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 MemoryIdempotentRepository (org.apache.camel.processor.idempotent.MemoryIdempotentRepository)4 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 File (java.io.File)1 LoggingLevel (org.apache.camel.LoggingLevel)1 GenericFileExclusiveReadLockStrategy (org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy)1