Skip to content

Instantly share code, notes, and snippets.

@iainporter
iainporter / config.yml
Last active September 23, 2020 15:41
build config for circleci and native image
# Install GraalVM and native-image, needed for a native Quarkus build
# ########################
- run:
name: Install GraalVM
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
curl https://github.com/oracle/graal/releases/download/vm-19.1.1/graalvm-ce-linux-amd64-19.1.1.tar.gz -O -J -L && tar xfz graalvm-ce-linux-amd64-19.1.1.tar.gz && mv graalvm-ce-19.1.1 .graalvm && rm graalvm-ce-linux-amd64-19.1.1.tar.gz
fi
- run:
name: Install native-image
@iainporter
iainporter / gist:2ac43c8419e8143ea4a21d3d4e4536e4
Created August 29, 2020 10:09
RollbackMovesFileToFailed
@Test
public void rollbackMovesFileToFailed() throws Exception {
final CountDownLatch stopLatch = new CountDownLatch(1);
filePollingChannel.addInterceptor(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
stopLatch.countDown();
throw new RuntimeException("Forcing an Exception to trigger rollback");
}
});
@Test
public void pollIgnoresFileAlreadySeen() throws Exception {
final CountDownLatch stopLatch = new CountDownLatch(1);
filePollingChannel.addInterceptor(new ChannelInterceptor() {
@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
stopLatch.countDown();
}
});
copy(TestUtils.locateClasspathResource(TestUtils.FILE_FIXTURE_PATH), new File(inboundReadDirectory, TestUtils.FILE_FIXTURE_NAME ));
@iainporter
iainporter / PollIgnoresInvalidFile.java
Last active August 29, 2020 10:04
test that invalid file is ignored
@Test
public void pollIgnoresInvalidFile() throws Exception {
copy(TestUtils.locateClasspathResource(TestUtils.FILE_FIXTURE_PATH), new File(inboundReadDirectory, TestUtils.FILE_FIXTURE_NAME + ".tmp" ));
Thread.sleep(2000);
assertThatDirectoryIsEmpty(inboundProcessedDirectory);
assertThatDirectoryIsEmpty(inboundFailedDirectory);
assertThatDirectoryIsEmpty(inboundOutDirectory);
assertThatDirectoryHasFiles(inboundReadDirectory, 1);
}
@iainporter
iainporter / PollFindsValidFile.java
Last active August 29, 2020 09:57
test for validating file was read
@Autowired
@Qualifier(ApplicationConfiguration.INBOUND_CHANNEL)
public DirectChannel filePollingChannel;
@Test
public void pollFindsValidFile() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
filePollingChannel.addInterceptor(new ChannelInterceptor() {
@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
@iainporter
iainporter / MessageProcessingIntegrationFlow.java
Created August 29, 2020 09:24
Handle file contents and write out to a file
@Bean
public IntegrationFlow writeToFile(@Qualifier("fileWritingMessageHandler") MessageHandler fileWritingMessageHandler) {
return IntegrationFlows.from(ApplicationConfiguration.INBOUND_CHANNEL)
.transform(m -> new StringBuilder((String)m).reverse().toString())
.handle(fileWritingMessageHandler)
.log(LoggingHandler.Level.INFO)
.get();
}
@iainporter
iainporter / InboundFileIntegration.java
Created August 28, 2020 17:57
DSL spring integration flow for polling for files
@Bean
public IntegrationFlow inboundFileIntegration(@Value("${inbound.file.poller.fixed.delay}") long period,
@Value("${inbound.file.poller.max.messages.per.poll}") int maxMessagesPerPoll,
TaskExecutor taskExecutor,
MessageSource<File> fileReadingMessageSource) {
return IntegrationFlows.from(fileReadingMessageSource,
c -> c.poller(Pollers.fixedDelay(period)
.taskExecutor(taskExecutor)
.maxMessagesPerPoll(maxMessagesPerPoll)
.transactionSynchronizationFactory(transactionSynchronizationFactory())
@iainporter
iainporter / TransactionSynchronizationFactory.java
Created August 28, 2020 17:48
transaction classes for file poller
@Bean
PseudoTransactionManager transactionManager() {
return new PseudoTransactionManager();
}
@Bean
TransactionSynchronizationFactory transactionSynchronizationFactory() {
ExpressionParser parser = new SpelExpressionParser();
ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor =
new ExpressionEvaluatingTransactionSynchronizationProcessor();
@iainporter
iainporter / FileReadingMessageSource.java
Created August 28, 2020 17:36
message source for file polling
@Bean
public FileReadingMessageSource fileReadingMessageSource(DirectoryScanner directoryScanner) {
FileReadingMessageSource source = new FileReadingMessageSource();
source.setDirectory(this.inboundReadDirectory);
source.setScanner(directoryScanner);
source.setAutoCreateDirectory(true);
return source;
}
@iainporter
iainporter / DirectoryScanner.java
Created August 28, 2020 17:30
scanner for file poller
@Bean
public DirectoryScanner directoryScanner(@Value("${inbound.filename.regex}") String regex) {
DirectoryScanner scanner = new RecursiveDirectoryScanner();
CompositeFileListFilter<File> filter = new CompositeFileListFilter<>(
Arrays.asList(new AcceptOnceFileListFilter<>(),
new RegexPatternFileListFilter(regex))
);
scanner.setFilter(filter);
return scanner;
}