use of org.springframework.integration.selector.MetadataStoreSelector in project spring-integration by spring-projects.
the class IdempotentReceiverTests method testIdempotentReceiverInterceptor.
@Test
public void testIdempotentReceiverInterceptor() {
ConcurrentMetadataStore store = new SimpleMetadataStore();
ExpressionEvaluatingMessageProcessor<String> idempotentKeyStrategy = new ExpressionEvaluatingMessageProcessor<>(new SpelExpressionParser().parseExpression("payload"));
BeanFactory beanFactory = Mockito.mock(BeanFactory.class);
idempotentKeyStrategy.setBeanFactory(beanFactory);
IdempotentReceiverInterceptor idempotentReceiverInterceptor = new IdempotentReceiverInterceptor(new MetadataStoreSelector(idempotentKeyStrategy, store));
idempotentReceiverInterceptor.setThrowExceptionOnRejection(true);
AtomicReference<Message<?>> handled = new AtomicReference<>();
MessageHandler idempotentReceiver = handled::set;
ProxyFactory proxyFactory = new ProxyFactory(idempotentReceiver);
proxyFactory.addAdvice(idempotentReceiverInterceptor);
idempotentReceiver = (MessageHandler) proxyFactory.getProxy();
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
assertNotNull(store.get("foo"));
try {
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
fail("MessageRejectedException expected");
} catch (Exception e) {
assertThat(e, instanceOf(MessageRejectedException.class));
}
idempotentReceiverInterceptor.setThrowExceptionOnRejection(false);
idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
assertTrue(handled.get().getHeaders().get(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, Boolean.class));
assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
}
Aggregations