Search in sources :

Example 1 with AbstractMessageSplitter

use of org.springframework.integration.splitter.AbstractMessageSplitter in project spring-integration by spring-projects.

the class SplitterAnnotationPostProcessor method createHandler.

@Override
protected MessageHandler createHandler(Object bean, Method method, List<Annotation> annotations) {
    String applySequence = MessagingAnnotationUtils.resolveAttribute(annotations, "applySequence", String.class);
    AbstractMessageSplitter splitter;
    if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
        Object target = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
        splitter = this.extractTypeIfPossible(target, AbstractMessageSplitter.class);
        if (splitter == null) {
            if (target instanceof MessageHandler) {
                Assert.hasText(applySequence, "'applySequence' can be applied to 'AbstractMessageSplitter', but " + "target handler is: " + target.getClass());
                return (MessageHandler) target;
            } else {
                splitter = new MethodInvokingSplitter(target);
            }
        } else {
            checkMessageHandlerAttributes(resolveTargetBeanName(method), annotations);
            return splitter;
        }
    } else {
        splitter = new MethodInvokingSplitter(bean, method);
    }
    if (StringUtils.hasText(applySequence)) {
        String applySequenceValue = this.beanFactory.resolveEmbeddedValue(applySequence);
        if (StringUtils.hasText(applySequenceValue)) {
            splitter.setApplySequence(Boolean.parseBoolean(applySequenceValue));
        }
    }
    this.setOutputChannelIfPresent(annotations, splitter);
    return splitter;
}
Also used : MethodInvokingSplitter(org.springframework.integration.splitter.MethodInvokingSplitter) MessageHandler(org.springframework.messaging.MessageHandler) AbstractMessageSplitter(org.springframework.integration.splitter.AbstractMessageSplitter) Bean(org.springframework.context.annotation.Bean)

Example 2 with AbstractMessageSplitter

use of org.springframework.integration.splitter.AbstractMessageSplitter in project spring-integration by spring-projects.

the class SplitterFactoryBean method postProcessReplyProducer.

@Override
protected void postProcessReplyProducer(AbstractMessageProducingHandler handler) {
    super.postProcessReplyProducer(handler);
    if (!(handler instanceof AbstractMessageSplitter)) {
        Assert.isNull(this.applySequence, "Cannot set applySequence if the referenced bean is " + "an AbstractReplyProducingMessageHandler, but not an AbstractMessageSplitter");
        Assert.isNull(this.delimiters, "Cannot set delimiters if the referenced bean is not an " + "an AbstractReplyProducingMessageHandler, but not an AbstractMessageSplitter");
    } else {
        AbstractMessageSplitter splitter = (AbstractMessageSplitter) handler;
        if (this.delimiters != null) {
            Assert.isInstanceOf(DefaultMessageSplitter.class, splitter, "The 'delimiters' property is only available for a Splitter definition where no 'ref', " + "'expression', or inner bean has been provided.");
            ((DefaultMessageSplitter) splitter).setDelimiters(this.delimiters);
        }
        if (this.applySequence != null) {
            splitter.setApplySequence(this.applySequence);
        }
    }
}
Also used : DefaultMessageSplitter(org.springframework.integration.splitter.DefaultMessageSplitter) AbstractMessageSplitter(org.springframework.integration.splitter.AbstractMessageSplitter)

Example 3 with AbstractMessageSplitter

use of org.springframework.integration.splitter.AbstractMessageSplitter in project spring-integration by spring-projects.

the class SplitterFactoryBean method createMethodInvokingHandler.

@Override
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
    Assert.notNull(targetObject, "targetObject must not be null");
    AbstractMessageSplitter splitter = this.extractTypeIfPossible(targetObject, AbstractMessageSplitter.class);
    if (splitter == null) {
        this.checkForIllegalTarget(targetObject, targetMethodName);
        splitter = this.createMethodInvokingSplitter(targetObject, targetMethodName);
        this.configureSplitter(splitter);
    } else {
        Assert.isTrue(!StringUtils.hasText(targetMethodName), "target method should not be provided when the target " + "object is an implementation of AbstractMessageSplitter");
        this.configureSplitter(splitter);
        if (targetObject instanceof MessageHandler) {
            return (MessageHandler) targetObject;
        }
    }
    return splitter;
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) AbstractMessageSplitter(org.springframework.integration.splitter.AbstractMessageSplitter)

Example 4 with AbstractMessageSplitter

use of org.springframework.integration.splitter.AbstractMessageSplitter in project spring-integration by spring-projects.

the class CollectionAndArrayTests method arrayWithSplittingHandler.

@Test
public void arrayWithSplittingHandler() {
    AbstractMessageSplitter handler = new AbstractMessageSplitter() {

        @Override
        protected Object splitMessage(Message<?> message) {
            return new String[] { "foo", "bar" };
        }
    };
    QueueChannel channel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("test").setReplyChannel(channel).build();
    handler.handleMessage(message);
    Message<?> reply1 = channel.receive(0);
    Message<?> reply2 = channel.receive(0);
    assertNotNull(reply1);
    assertNotNull(reply2);
    assertEquals(String.class, reply1.getPayload().getClass());
    assertEquals(String.class, reply2.getPayload().getClass());
    assertEquals("foo", reply1.getPayload());
    assertEquals("bar", reply2.getPayload());
}
Also used : Message(org.springframework.messaging.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) AbstractMessageSplitter(org.springframework.integration.splitter.AbstractMessageSplitter) Test(org.junit.Test)

Example 5 with AbstractMessageSplitter

use of org.springframework.integration.splitter.AbstractMessageSplitter in project spring-integration by spring-projects.

the class CollectionAndArrayTests method listWithSplittingHandler.

@Test
public void listWithSplittingHandler() {
    AbstractMessageSplitter handler = new AbstractMessageSplitter() {

        @Override
        protected Object splitMessage(Message<?> message) {
            return Arrays.asList(new String[] { "foo", "bar" });
        }
    };
    QueueChannel channel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("test").setReplyChannel(channel).build();
    handler.handleMessage(message);
    Message<?> reply1 = channel.receive(0);
    Message<?> reply2 = channel.receive(0);
    assertNotNull(reply1);
    assertNotNull(reply2);
    assertEquals(String.class, reply1.getPayload().getClass());
    assertEquals(String.class, reply2.getPayload().getClass());
    assertEquals("foo", reply1.getPayload());
    assertEquals("bar", reply2.getPayload());
}
Also used : Message(org.springframework.messaging.Message) QueueChannel(org.springframework.integration.channel.QueueChannel) AbstractMessageSplitter(org.springframework.integration.splitter.AbstractMessageSplitter) Test(org.junit.Test)

Aggregations

AbstractMessageSplitter (org.springframework.integration.splitter.AbstractMessageSplitter)6 Test (org.junit.Test)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 Message (org.springframework.messaging.Message)3 MessageHandler (org.springframework.messaging.MessageHandler)2 HashSet (java.util.HashSet)1 Bean (org.springframework.context.annotation.Bean)1 DefaultMessageSplitter (org.springframework.integration.splitter.DefaultMessageSplitter)1 MethodInvokingSplitter (org.springframework.integration.splitter.MethodInvokingSplitter)1