Search in sources :

Example 51 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class ServiceActivatorDefaultFrameworkMethodTests method testReplyingMessageHandlerWithStandardMethod.

@Test
public void testReplyingMessageHandlerWithStandardMethod() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build();
    this.replyingHandlerWithStandardMethodTestInputChannel.send(message);
    Message<?> reply = replyChannel.receive(0);
    assertEquals("TEST", reply.getPayload());
    assertEquals("replyingHandlerWithStandardMethodTestInputChannel,replyingHandlerWithStandardMethodTestService", reply.getHeaders().get("history").toString());
    StackTraceElement[] st = (StackTraceElement[]) reply.getHeaders().get("callStack");
    assertTrue(StackTraceUtils.isFrameContainingXBeforeFrameContainingY("AbstractSubscribableChannel", "MethodInvokerHelper", // close to the metal
    st));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 52 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method filterDiscardWithinAdvice.

@Test
public void filterDiscardWithinAdvice() {
    MessageFilter filter = new MessageFilter(message -> 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.setBeanFactory(mock(BeanFactory.class));
    filter.afterPropertiesSet();
    filter.handleMessage(new GenericMessage<String>("foo"));
    assertNotNull(discardedWithinAdvice.get());
    assertNull(discardChannel.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) BeanFactory(org.springframework.beans.factory.BeanFactory) Advice(org.aopalliance.aop.Advice) MessageFilter(org.springframework.integration.filter.MessageFilter) Test(org.junit.Test)

Example 53 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method propagateOnFailureExpressionFailures.

@Test
public void propagateOnFailureExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean(true);
    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 failure
    try {
        handler.handleMessage(message);
        fail("Expected exception");
    } catch (Exception e) {
        assertEquals("qux", e.getCause().getMessage());
    }
    Message<?> reply = replies.receive(1);
    assertNull(reply);
    Message<?> failure = failureChannel.receive(10000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
    assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage());
    // propagate failing advice with failure; expect original exception
    advice.setPropagateEvaluationFailures(true);
    try {
        handler.handleMessage(message);
        fail("Expected Exception");
    } catch (MessageHandlingException e) {
        assertEquals("qux", e.getCause().getMessage());
    }
    reply = replies.receive(1);
    assertNull(reply);
    failure = failureChannel.receive(10000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
    assertEquals("qux", ((Exception) failure.getPayload()).getCause().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) 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 54 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class ObjectToJsonTransformerParserTests method testNodeResultType.

@Test
public void testNodeResultType() {
    TestPerson person = new TestPerson();
    person.setFirstName("John");
    person.setLastName("Doe");
    person.setAge(42);
    QueueChannel replyChannel = new QueueChannel();
    Message<TestPerson> message = MessageBuilder.withPayload(person).setReplyChannel(replyChannel).build();
    this.jsonNodeInput.send(message);
    Message<?> reply = replyChannel.receive(0);
    assertNotNull(reply);
    Object payload = reply.getPayload();
    assertThat(payload, Matchers.instanceOf(JsonNode.class));
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new JsonPropertyAccessor());
    Expression expression = new SpelExpressionParser().parseExpression("firstName.toString() == 'John' and age.toString() == '42'");
    assertTrue(expression.getValue(evaluationContext, payload, Boolean.class));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) QueueChannel(org.springframework.integration.channel.QueueChannel) Expression(org.springframework.expression.Expression) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 55 with QueueChannel

use of org.springframework.integration.channel.QueueChannel in project spring-integration by spring-projects.

the class ObjectToJsonTransformerParserTests method testInt2831CustomJsonObjectMapper.

@Test
public void testInt2831CustomJsonObjectMapper() {
    TestPerson person = new TestPerson();
    person.setFirstName("John");
    person.setLastName("Doe");
    person.setAge(42);
    QueueChannel replyChannel = new QueueChannel();
    Message<TestPerson> message = MessageBuilder.withPayload(person).setReplyChannel(replyChannel).build();
    this.customJsonObjectMapperInput.send(message);
    Message<?> reply = replyChannel.receive(0);
    assertNotNull(reply);
    assertNotNull(reply.getPayload());
    assertEquals(String.class, reply.getPayload().getClass());
    String resultString = (String) reply.getPayload();
    assertEquals("{" + person.toString() + "}", resultString);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Aggregations

QueueChannel (org.springframework.integration.channel.QueueChannel)709 Test (org.junit.Test)669 GenericMessage (org.springframework.messaging.support.GenericMessage)186 Message (org.springframework.messaging.Message)173 BeanFactory (org.springframework.beans.factory.BeanFactory)162 MessageChannel (org.springframework.messaging.MessageChannel)100 Matchers.containsString (org.hamcrest.Matchers.containsString)66 CountDownLatch (java.util.concurrent.CountDownLatch)59 DirectChannel (org.springframework.integration.channel.DirectChannel)57 ArrayList (java.util.ArrayList)55 PollableChannel (org.springframework.messaging.PollableChannel)55 MessagingException (org.springframework.messaging.MessagingException)53 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)51 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 Socket (java.net.Socket)44 ErrorMessage (org.springframework.messaging.support.ErrorMessage)42 ServerSocket (java.net.ServerSocket)41 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)39 IOException (java.io.IOException)35 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)35