Search in sources :

Example 46 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method defaultRetrySucceedOnThirdTry.

@Test
public void defaultRetrySucceedOnThirdTry() {
    final AtomicInteger counter = new AtomicInteger(2);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (counter.getAndDecrement() > 0) {
                throw new RuntimeException("foo");
            }
            return "bar";
        }
    };
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    Message<String> message = new GenericMessage<String>("Hello, world!");
    handler.handleMessage(message);
    assertTrue(counter.get() == -1);
    Message<?> reply = replies.receive(10000);
    assertNotNull(reply);
    assertEquals("bar", reply.getPayload());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 47 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method circuitBreakerTests.

@Test
@SuppressWarnings("rawtypes")
public void circuitBreakerTests() throws Exception {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("foo");
            }
            return "bar";
        }
    };
    handler.setBeanName("baz");
    handler.setOutputChannel(new QueueChannel());
    RequestHandlerCircuitBreakerAdvice advice = new RequestHandlerCircuitBreakerAdvice();
    /*
		 * Circuit breaker opens after 2 failures; allows a new attempt after 100ms and
		 * immediately opens again if that attempt fails. After a successful attempt,
		 * we reset the failure counter.
		 */
    advice.setThreshold(2);
    advice.setHalfOpenAfter(1000);
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    doFail.set(true);
    Message<String> message = new GenericMessage<String>("Hello, world!");
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getMessage());
        assertSame(message, ((MessagingException) e).getFailedMessage());
    }
    Map metadataMap = TestUtils.getPropertyValue(advice, "metadataMap", Map.class);
    Object metadata = metadataMap.values().iterator().next();
    DirectFieldAccessor metadataDfa = new DirectFieldAccessor(metadata);
    // Simulate some timeout in between requests
    metadataDfa.setPropertyValue("lastFailure", System.currentTimeMillis() - 10000);
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getMessage());
    }
    // Simulate some timeout in between requests
    metadataDfa.setPropertyValue("lastFailure", System.currentTimeMillis() - 10000);
    doFail.set(false);
    handler.handleMessage(message);
    doFail.set(true);
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getMessage());
    }
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 48 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method propagateOnSuccessExpressionFailures.

@Test
public void propagateOnSuccessExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("qux");
            }
            return "baz";
        }
    };
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    Message<String> message = new GenericMessage<String>("Hello, world!");
    PollableChannel successChannel = new QueueChannel();
    PollableChannel failureChannel = new QueueChannel();
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setBeanFactory(mock(BeanFactory.class));
    advice.setSuccessChannel(successChannel);
    advice.setFailureChannel(failureChannel);
    advice.setOnSuccessExpressionString("1/0");
    advice.setOnFailureExpressionString("1/0");
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    // failing advice with success
    handler.handleMessage(message);
    Message<?> reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());
    Message<?> success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
    assertEquals(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());
    // propagate failing advice with success
    advice.setPropagateEvaluationFailures(true);
    try {
        handler.handleMessage(message);
        fail("Expected Exception");
    } catch (MessageHandlingException e) {
        assertEquals("/ by zero", e.getCause().getMessage());
    }
    reply = replies.receive(1);
    assertNull(reply);
    success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
    assertEquals(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) AdviceMessage(org.springframework.integration.message.AdviceMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GenericMessage(org.springframework.messaging.support.GenericMessage) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 49 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class HttpRequestHandlingMessagingGatewayTests method noAcceptHeaderOnRequest.

// INT-1767
@Test
public void noAcceptHeaderOnRequest() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload().toString().toUpperCase();
        }
    });
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setRequestPayloadTypeClass(String.class);
    gateway.setRequestChannel(requestChannel);
    gateway.afterPropertiesSet();
    gateway.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("text/plain");
    request.setContent("hello".getBytes());
    MockHttpServletResponse response = new MockHttpServletResponse();
    gateway.handleRequest(request, response);
    assertEquals("HELLO", response.getContentAsString());
    // INT-3120
    assertNull(response.getHeader("Accept-Charset"));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 50 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class HttpRequestHandlingMessagingGatewayTests method stringExpectedWithReplyGuts.

private void stringExpectedWithReplyGuts(boolean contentType) throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload().toString().toUpperCase();
        }
    });
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setStatusCodeExpression(new LiteralExpression("foo"));
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setRequestPayloadTypeClass(String.class);
    gateway.setRequestChannel(requestChannel);
    gateway.afterPropertiesSet();
    gateway.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.addHeader("Accept", "x-application/octet-stream");
    if (contentType) {
        request.setContentType("text/plain");
    }
    request.setContent("hello".getBytes());
    MockHttpServletResponse response = new MockHttpServletResponse();
    gateway.handleRequest(request, response);
    assertEquals("HELLO", response.getContentAsString());
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) LiteralExpression(org.springframework.expression.common.LiteralExpression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)52 Test (org.junit.Test)46 BeanFactory (org.springframework.beans.factory.BeanFactory)37 QueueChannel (org.springframework.integration.channel.QueueChannel)29 Message (org.springframework.messaging.Message)28 DirectChannel (org.springframework.integration.channel.DirectChannel)23 GenericMessage (org.springframework.messaging.support.GenericMessage)20 Matchers.containsString (org.hamcrest.Matchers.containsString)19 ErrorMessage (org.springframework.messaging.support.ErrorMessage)18 ArrayList (java.util.ArrayList)14 AdviceMessage (org.springframework.integration.message.AdviceMessage)14 Advice (org.aopalliance.aop.Advice)13 HashMap (java.util.HashMap)12 MessageHandlingException (org.springframework.messaging.MessageHandlingException)12 Expression (org.springframework.expression.Expression)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 MessageHandlingExpressionEvaluatingAdviceException (org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException)9 MessagingException (org.springframework.messaging.MessagingException)9