Example usage for org.springframework.integration.channel QueueChannel QueueChannel

List of usage examples for org.springframework.integration.channel QueueChannel QueueChannel

Introduction

In this page you can find the example usage for org.springframework.integration.channel QueueChannel QueueChannel.

Prototype

public QueueChannel() 

Source Link

Document

Create a channel with "unbounded" queue capacity.

Usage

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void testInappropriateAdvice() throws Exception {
    final AtomicBoolean called = new AtomicBoolean(false);
    Advice advice = new AbstractRequestHandlerAdvice() {
        @Override// ww  w  .  jav a  2  s  .co m
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message)
                throws Exception {
            called.set(true);
            return callback.execute();
        }
    };
    PollableChannel inputChannel = new QueueChannel();
    PollingConsumer consumer = new PollingConsumer(inputChannel, new MessageHandler() {
        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
        }
    });
    consumer.setAdviceChain(Collections.singletonList(advice));
    consumer.setTaskExecutor(
            new ErrorHandlingTaskExecutor(Executors.newSingleThreadExecutor(), new ErrorHandler() {
                @Override
                public void handleError(Throwable t) {
                }
            }));
    consumer.afterPropertiesSet();

    Callable<?> pollingTask = TestUtils.getPropertyValue(consumer, "poller.pollingTask", Callable.class);
    assertTrue(AopUtils.isAopProxy(pollingTask));
    Log logger = TestUtils.getPropertyValue(advice, "logger", Log.class);
    logger = spy(logger);
    when(logger.isWarnEnabled()).thenReturn(Boolean.TRUE);
    final AtomicReference<String> logMessage = new AtomicReference<String>();
    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            logMessage.set((String) invocation.getArguments()[0]);
            return null;
        }
    }).when(logger).warn(Mockito.anyString());
    DirectFieldAccessor accessor = new DirectFieldAccessor(advice);
    accessor.setPropertyValue("logger", logger);

    pollingTask.call();
    assertFalse(called.get());
    assertNotNull(logMessage.get());
    assertTrue(logMessage.get()
            .endsWith("can only be used for MessageHandlers; " + "an attempt to advise method 'call' in "
                    + "'org.springframework.integration.endpoint.AbstractPollingEndpoint$1' is ignored"));
}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

public void filterDiscardNoAdvice() {
    MessageFilter filter = new MessageFilter(new MessageSelector() {
        @Override//from   www  . ja va  2  s  . c o m
        public boolean accept(Message<?> message) {
            return false;
        }
    });
    QueueChannel discardChannel = new QueueChannel();
    filter.setDiscardChannel(discardChannel);
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertNotNull(discardChannel.receive(0));
}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void filterDiscardWithinAdvice() {
    MessageFilter filter = new MessageFilter(new MessageSelector() {
        @Override/*from   www  . j  a  v a  2s . c  o m*/
        public boolean accept(Message<?> message) {
            return false;
        }
    });
    final QueueChannel discardChannel = new QueueChannel();
    filter.setDiscardChannel(discardChannel);
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicReference<Message<?>> discardedWithinAdvice = new AtomicReference<Message<?>>();
    adviceChain.add(new AbstractRequestHandlerAdvice() {
        @Override
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message)
                throws Exception {
            Object result = callback.execute();
            discardedWithinAdvice.set(discardChannel.receive(0));
            return result;
        }
    });
    filter.setAdviceChain(adviceChain);
    filter.afterPropertiesSet();
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertNotNull(discardedWithinAdvice.get());
    assertNull(discardChannel.receive(0));
}

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void filterDiscardOutsideAdvice() {
    MessageFilter filter = new MessageFilter(new MessageSelector() {
        @Override//from   ww w  .  ja v  a  2 s .com
        public boolean accept(Message<?> message) {
            return false;
        }
    });
    final QueueChannel discardChannel = new QueueChannel();
    filter.setDiscardChannel(discardChannel);
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicReference<Message<?>> discardedWithinAdvice = new AtomicReference<Message<?>>();
    final AtomicBoolean adviceCalled = new AtomicBoolean();
    adviceChain.add(new AbstractRequestHandlerAdvice() {
        @Override
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message)
                throws Exception {
            Object result = callback.execute();
            discardedWithinAdvice.set(discardChannel.receive(0));
            adviceCalled.set(true);
            return result;
        }
    });
    filter.setAdviceChain(adviceChain);
    filter.setDiscardWithinAdvice(false);
    filter.afterPropertiesSet();
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertTrue(adviceCalled.get());
    assertNull(discardedWithinAdvice.get());
    assertNotNull(discardChannel.receive(0));
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testGoodResultWithReplyChannelHeader() {
    this.whichTest = 0;
    this.handler.setOutputChannel(null);
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("foo").setReplyChannel(replyChannel).build();
    this.handler.handleMessage(message);
    assertNull(replyChannel.receive(0));
    this.latch.countDown();
    Message<?> received = replyChannel.receive(10000);
    assertNotNull(received);//w  w  w .  ja v  a 2s  .  com
    assertEquals("reply", received.getPayload());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testGoodResultWithNoReplyChannelHeaderNoOutput() throws Exception {
    this.whichTest = 0;
    this.handler.setOutputChannel(null);
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.latch.countDown();
    Message<?> errorMessage = errorChannel.receive(1000);
    assertNotNull(errorMessage);/*from w  w w. j a va  2 s. c om*/
    assertThat(errorMessage.getPayload(), instanceOf(DestinationResolutionException.class));
    assertEquals("no output-channel or replyChannel header available",
            ((Throwable) errorMessage.getPayload()).getMessage());
    assertNull(((MessagingException) errorMessage.getPayload()).getFailedMessage());
    assertNotNull(this.failedCallbackException);
    assertThat(this.failedCallbackException.getMessage(), containsString("or replyChannel header"));
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testRuntimeException() {
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.whichTest = 1;
    this.latch.countDown();
    Message<?> received = errorChannel.receive(10000);
    assertNotNull(received);//  w w w .  ja  v a  2  s.  c  om
    assertThat(received.getPayload(), instanceOf(MessageHandlingException.class));
    assertEquals("foo", ((Throwable) received.getPayload()).getCause().getMessage());
    assertSame(message, ((MessagingException) received.getPayload()).getFailedMessage());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testMessagingException() {
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.whichTest = 2;
    this.latch.countDown();
    Message<?> received = errorChannel.receive(10000);
    assertNotNull(received);/*  ww  w.  j  a v a  2 s  . co  m*/
    assertThat(received.getPayload(), instanceOf(MessagingException.class));
    assertSame(message, ((MessagingException) received.getPayload()).getFailedMessage());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.http.inbound.HttpRequestHandlingControllerTests.java

@Test
public void sendOnly() throws Exception {
    QueueChannel requestChannel = new QueueChannel();
    HttpRequestHandlingController controller = new HttpRequestHandlingController(false);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.afterPropertiesSet();/*from  w w  w. j ava  2 s  . c  o m*/
    controller.start();

    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();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(0, modelAndView.getModel().size());
    Message<?> requestMessage = requestChannel.receive(0);
    assertNotNull(requestMessage);
    assertEquals("hello", requestMessage.getPayload());
}

From source file:org.springframework.integration.http.inbound.HttpRequestHandlingControllerTests.java

@Test
public void sendOnlyViewExpression() throws Exception {
    QueueChannel requestChannel = new QueueChannel();
    HttpRequestHandlingController controller = new HttpRequestHandlingController(false);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    Expression viewExpression = new SpelExpressionParser().parseExpression("'baz'");
    controller.setViewExpression(viewExpression);
    controller.afterPropertiesSet();//  w  w w.  j ava 2s.  com
    controller.start();

    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();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("baz", modelAndView.getViewName());
    assertEquals(0, modelAndView.getModel().size());
    Message<?> requestMessage = requestChannel.receive(0);
    assertNotNull(requestMessage);
    assertEquals("hello", requestMessage.getPayload());
}