Search in sources :

Example 1 with MetadataStoreSelector

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());
}
Also used : ExpressionEvaluatingMessageProcessor(org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) ConcurrentMetadataStore(org.springframework.integration.metadata.ConcurrentMetadataStore) MessageRejectedException(org.springframework.integration.MessageRejectedException) MetadataStoreSelector(org.springframework.integration.selector.MetadataStoreSelector) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 MessageRejectedException (org.springframework.integration.MessageRejectedException)1 ExpressionEvaluatingMessageProcessor (org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor)1 ConcurrentMetadataStore (org.springframework.integration.metadata.ConcurrentMetadataStore)1 SimpleMetadataStore (org.springframework.integration.metadata.SimpleMetadataStore)1 MetadataStoreSelector (org.springframework.integration.selector.MetadataStoreSelector)1 Message (org.springframework.messaging.Message)1 MessageHandler (org.springframework.messaging.MessageHandler)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1