GH-3241: MetadataStoreSelector - compare old/new (#3242)

Resolves: https://github.com/spring-projects/spring-integration/issues/3241

 * Docs and XML namespace support.
This commit is contained in:
Gary Russell
2020-04-06 18:12:36 -04:00
committed by GitHub
parent 1f2a33437d
commit 7d7c273515
10 changed files with 244 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2019 the original author or authors.
* Copyright 2015-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,6 +49,10 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.splitter.FileSplitter.FileMarker;
import org.springframework.integration.handler.advice.IdempotentReceiverInterceptor;
import org.springframework.integration.metadata.ConcurrentMetadataStore;
import org.springframework.integration.metadata.SimpleMetadataStore;
import org.springframework.integration.selector.MetadataStoreSelector;
import org.springframework.integration.support.json.JsonObjectMapper;
import org.springframework.integration.support.json.JsonObjectMapperProvider;
import org.springframework.messaging.Message;
@@ -90,6 +94,12 @@ public class FileSplitterTests {
@Autowired
private PollableChannel output;
@Autowired
private MetadataStoreSelector selector;
@Autowired
private ConcurrentMetadataStore store;
@BeforeAll
static void setup(@TempDir File tempDir) throws IOException {
file = new File(tempDir, "foo.txt");
@@ -104,12 +114,19 @@ public class FileSplitterTests {
assertThat(receive).isNotNull(); //HelloWorld
assertThat(receive.getPayload()).isEqualTo("HelloWorld");
assertThat(receive.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE)).isEqualTo(2);
assertThat(receive.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER)).isEqualTo(1);
assertThat(this.selector.accept(receive)).isTrue();
assertThat(this.store.get(this.file.getAbsolutePath())).isEqualTo("1");
receive = this.output.receive(10000);
assertThat(receive).isNotNull(); //äöüß
assertThat(receive.getPayload()).isEqualTo("äöüß");
assertThat(receive.getHeaders().get(FileHeaders.ORIGINAL_FILE)).isEqualTo(file);
assertThat(receive.getHeaders().get(FileHeaders.FILENAME)).isEqualTo(file.getName());
assertThat(receive.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER)).isEqualTo(2);
assertThat(this.output.receive(1)).isNull();
assertThat(this.selector.accept(receive)).isTrue();
assertThat(this.store.get(this.file.getAbsolutePath())).isEqualTo("2");
assertThat(this.selector.accept(receive)).isFalse();
this.input1.send(new GenericMessage<>(file.getAbsolutePath()));
receive = this.output.receive(10000);
@@ -396,6 +413,28 @@ public class FileSplitterTests {
return fileSplitter;
}
@Bean
public IdempotentReceiverInterceptor idempotentReceiverInterceptor() {
return new IdempotentReceiverInterceptor(selector());
}
@Bean
public MetadataStoreSelector selector() {
return new MetadataStoreSelector(
message -> message.getHeaders().get(FileHeaders.ORIGINAL_FILE, File.class)
.getAbsolutePath(),
message -> message.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER)
.toString(),
store())
.compareValues(
(oldVal, newVal) -> Integer.parseInt(oldVal) < Integer.parseInt(newVal));
}
@Bean
public ConcurrentMetadataStore store() {
return new SimpleMetadataStore();
}
}
}