Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:使用循环发送多条消息_Java_Spring_Spring Integration_Spring Xd_Spring Messaging - Fatal编程技术网

Java Spring:使用循环发送多条消息

Java Spring:使用循环发送多条消息,java,spring,spring-integration,spring-xd,spring-messaging,Java,Spring,Spring Integration,Spring Xd,Spring Messaging,假设我在SpringXD中有一个自定义模块,我使用SpringXD+SpringIntegration+hibernate。模块基本上从数据库中获取一些东西,比如说我使用hibernate实体存储它,所以我使用一个名为DataFromDB的对象。DataFromDB是一个列表,然后我从列表中获取每个元素,并希望使用如下方式发送它: String payload = convertDataFromDBToJson(record); return MessageBuilder.createMessa

假设我在SpringXD中有一个自定义模块,我使用SpringXD+SpringIntegration+hibernate。模块基本上从数据库中获取一些东西,比如说我使用hibernate实体存储它,所以我使用一个名为DataFromDB的对象。DataFromDB是一个列表,然后我从列表中获取每个元素,并希望使用如下方式发送它:

String payload = convertDataFromDBToJson(record);
return MessageBuilder.createMessage(payload, message.getHeaders());
问题是每次我必须发送消息时,我必须返回消息。 因此,我想循环DataFromDB列表,并将每个元素作为消息发送。 有没有办法发送多条消息

编辑:

我只是根据注释创建了一个小示例,试图复制该场景。这就是我所拥有的:

我的变压器等级:

public class TransformerClass {
public Collection<Message<?>> transformerMethod(Message<?> message) {
    List<Message<?>> messages = new ArrayList<Message<?>>();
    messages.add(new GenericMessage<>("foo"));
    messages.add(new GenericMessage<>("bar"));
    return messages;
}
我的xml配置:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd">

    <tx:annotation-driven />
    <int:channel id="input" />

    <bean id="transFormerClass" class="myModule.TransformerClass">
    </bean>

    <int:transformer input-channel="input" output-channel="output" ref="transFormerClass" method="transformerMethod"/>

    <int:channel id="output"/>

</beans>
我的测试班:

    @ModuleName(value = "someModule", type = ModuleType.processor)
public class TransformerClassTest extends xDTest {
    private String streamName = "myStream";
    private String chainTest = "someModule";

    @SuppressWarnings("unchecked")
    @Test
    public void testPartnerNotification() throws IOException {

        this.chain = SingleNodeProcessingChainSupport.chain(application, streamName, chainTest);
        //Just to send something to the module as a input.
        Message<String> input = MessageBuilder.createMessage("hello world", buildHeaders());
        this.chain.send(input);
        //Receiving a Single message
        Message<String> result = (Message<String>) chain.receive(5000);
        System.out.println("Result: " + result);
    }

    private MessageHeaders buildHeaders() {
        Map<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("test", "testing");
        MessageHeaders headers = new MessageHeaders(hashMap);
        return headers;
    }    
}
输出为:

结果:GenericMessage[payload=[GenericMessage[payload=foo,headers={timestamp=1475072951345,id=7b6c79a2-db85-563e-c238-262a31141456}],GenericMessage[payload=bar,headers={timestamp=1475072951345,id=31c8ef0e-3513-b95e-3a25-4fd3550f2fea}],headers={timestamp=1475072951347,id=f90d94c4-e323-70EE-62B8B,test=CE814D]


我使用的是Spring Integration 4.2.2.RELEASE。

如果您返回一个集合如果您返回一个集合,您是否尝试过只使用一个循环?说实话。但是这样做对我来说没有意义,因为为了发送消息,我必须返回MessageBuilder.createMessagepayload,message.getHeaders;因此,在这种情况下,只有一条消息,那么列表中只有一个元素将被发送。您是否尝试过使用循环?不诚实。但是这样做对我来说没有意义,因为为了发送消息,我必须返回MessageBuilder.createMessagepayload,message.getHeaders;因此,在这种情况下,只发送一条消息,然后列表中只发送一个元素。因此,在返回CollectionI后,我感到困惑-您说您已经有一个自定义模块,并且希望为一条输入消息创建多条输出消息。这正是将要发生的事情;你不需要拆分器。如果你还想收藏,你会的,但是收藏哦,太棒了!。非常感谢您的澄清。这正是我想要的。那么,我只需要使用CollectionHey@Gary Russell。我刚刚创建了一个Junit测试来检查与我实现的内容相关的输出。我硬编码了我的逻辑来填充我返回的带有2条消息的集合。我注意到我收到了一条包含两条消息的消息,而不是单独的两条消息。消息输入=MessageBuilder.createMessagealertDetail,buildHeaders;this.chain.sendinput;消息结果=chain.receive5000;System.out.printlnResult:+result;啊-变压器是特殊的-框架假设变压器对输出承担全部责任。将其更改为a,它将执行您想要的操作。在这种情况下,您不是在转换消息,而是在调用某个服务,因此service activator是正确的使用项。因此,在返回CollectionI后,我感到困惑-您说您已经有一个自定义模块,希望为一个输入消息创建多个输出消息。这正是将要发生的事情;你不需要拆分器。如果你还想收藏,你会的,但是收藏哦,太棒了!。非常感谢您的澄清。这正是我想要的。那么,我只需要使用CollectionHey@Gary Russell。我刚刚创建了一个Junit测试来检查与我实现的内容相关的输出。我硬编码了我的逻辑来填充我返回的带有2条消息的集合。我注意到我收到了一条包含两条消息的消息,而不是单独的两条消息。消息输入=MessageBuilder.createMessagealertDetail,buildHeaders;this.chain.sendinput;消息结果=chain.receive5000;System.out.printlnResult:+result;啊-变压器是特殊的-框架假设变压器对输出承担全部责任。将其更改为a,它将执行您想要的操作。在这种情况下,您不是在转换消息,而是在调用某个服务,因此要使用的正确项是服务激活器。
@EnableIntegration
@Configuration
public class So39708474Application {

    @Bean
    public DirectChannel input() {
        return new DirectChannel();
    }

    @Bean
    public DirectChannel output() {
        return new DirectChannel();
    }

    @Bean
    public Foo foo() {
        return new Foo();
    }

    public static class Foo {

        @ServiceActivator(inputChannel = "input", outputChannel = "output")
        public Collection<Message<?>> handle(Message<?> in) {
            List<Message<?>> messages = new ArrayList<Message<?>>();
            messages.add(new GenericMessage<>("foo"));
            messages.add(new GenericMessage<>("bar"));
            return messages;
        }

    }

}
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = So39708474Application.class)
public class So39708474ApplicationTests {

    @Autowired
    private MessageChannel input;

    @Autowired
    private SubscribableChannel output;

    @Test
    public void contextLoads() {
        AtomicInteger count = new AtomicInteger();
        this.output.subscribe(m -> {
            count.incrementAndGet();
            System.out.println(m);
        });
        this.input.send(new GenericMessage<>("test"));
        assertEquals(2, count.get());
    }

}