簡體   English   中英

春季:使用循環發送多條消息

[英]Spring: Send multiple messages using a loop

假設我在Spring XD中有一個自定義模塊(我正在使用Spring XD + Spring Integration + 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,標頭= {timestamp = 1475072951345,id = 7b6c79a2-db85-563e-c238-262a31141456}]],GenericMessage [payload = bar,標頭= {timestamp = 1475072951345,id = 31c8ef0e -3513-b95e-3a25-4fd3550f2fea}]],標題= {timestamp = 1475072951347,id = f90d94c4-e323-70ed-62ee-4b8bce64814d,test = testing}]

我正在使用Spring Integration 4.2.2.RELEASE。

如果返回Collection<Message<?>> ,它們將作為單獨的消息發送。

編輯

@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());
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM