Search in sources :

Example 51 with AbstractReplyProducingMessageHandler

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

the class HttpRequestHandlingControllerTests method shutDown.

@Test
public void shutDown() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            try {
                latch2.countDown();
                // hold up an active thread so we can verify the count and that it completes ok
                latch1.await(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return requestMessage.getPayload().toString().toUpperCase();
        }
    };
    requestChannel.subscribe(handler);
    final HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.afterPropertiesSet();
    controller.start();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".getBytes());
    // request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
    // Instead do:
    request.addHeader("Content-Type", "text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    final AtomicInteger active = new AtomicInteger();
    final AtomicBoolean expected503 = new AtomicBoolean();
    Executors.newSingleThreadExecutor().execute(() -> {
        try {
            // wait for the active thread
            latch2.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        // start the shutdown
        active.set(controller.beforeShutdown());
        try {
            MockHttpServletResponse response1 = new MockHttpServletResponse();
            controller.handleRequest(request, response1);
            expected503.set(response1.getStatus() == HttpStatus.SERVICE_UNAVAILABLE.value());
            latch1.countDown();
        } catch (Exception e) {
            LogFactory.getLog(getClass()).error("Async handleRequest failed", e);
        }
    });
    ModelAndView modelAndView = controller.handleRequest(request, response);
    // verify we get a 503 after shutdown starts
    assertEquals(1, active.get());
    assertTrue(expected503.get());
    // verify the active request still processed ok
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("HELLO", reply);
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 52 with AbstractReplyProducingMessageHandler

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

the class HttpRequestHandlingControllerTests method requestReplyViewExpressionString.

@Test
public void requestReplyViewExpressionString() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Message<String> handleRequestMessage(Message<?> requestMessage) {
            return MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']");
    controller.setViewExpression(viewExpression);
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".getBytes());
    request.setContentType("text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("baz", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("foo", reply);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) Expression(org.springframework.expression.Expression) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) ModelAndView(org.springframework.web.servlet.ModelAndView) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

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