Source:
<code>public void testIdempotentReceiverInterceptor() { ConcurrentMetadataStore store = new SimpleMetadataStore(); ExpressionMetadataKeyStrategy idempotentKeyStrategy = new ExpressionMetadataKeyStrategy("payload"); BeanFactory beanFactory = Mockito.mock(BeanFactory.class); Mockito.doAnswer(invocation -> new DefaultMessageBuilderFactory()) .when(beanFactory) .getBean(IntegrationUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME, MessageBuilderFactory.class); idempotentKeyStrategy.setBeanFactory(beanFactory); IdempotentReceiverInterceptor idempotentReceiverInterceptor = new IdempotentReceiverInterceptor(new MetadataStoreSelector(idempotentKeyStrategy, store)); idempotentReceiverInterceptor.setBeanFactory(beanFactory); idempotentReceiverInterceptor.setThrowExceptionOnRejection(true); AtomicReference<Message<?>> handled = new AtomicReference<>(); <START> MessageHandler idempotentReceiver = handled::set; <END> 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()); }</code><technical_language>Java 8</technical_language>

Target:
public void testIdempotentReceiverInterceptor() { ConcurrentMetadataStore store = new SimpleMetadataStore(); ExpressionMetadataKeyStrategy idempotentKeyStrategy = new ExpressionMetadataKeyStrategy("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()); }