使用Spring DSL进行Spring集成“发布订阅频道”

时间:2017-01-15 05:27:43

标签: spring spring-integration publish-subscribe spring-dsl

我正在尝试通过HTTP入站通道适配器接收HTTP Post Request并将其发布到'SubscribableChannel'来构建我的流程开始的简单流程。可能有'N'个消费者订阅了这个频道。下图显示了流程。

enter image description here

我正在尝试使用Spring DSL配置此流程,并且一直无法使其正常工作。以下是我的代码。

@Bean
public IntegrationFlow receiveHttpPost() {
    return IntegrationFlows.from(Http.inboundChannelAdapter("/receive")
            .mappedRequestHeaders("*")
            .requestChannel(httpInAdapterPubSubChannel()))
            .transform(new ObjectToStringTransformer())
            .get();
}

@Bean
public SubscribableChannel  httpInAdapterPubSubChannel()
{
    return MessageChannels.publishSubscribe("httpInAdapterPubSubChannel")
    .get();
}

@Bean
public IntegrationFlow subscriber1() {
    return IntegrationFlows.from(httpInAdapterPubSubChannel())
            .handle( message -> System.out.println("Enrich Headers based on Payload...."))
            .get();
}

@Bean
public IntegrationFlow subscriber2() {
     return IntegrationFlows.from(httpInAdapterPubSubChannel())
                .handle( message -> System.out.println("Save Payload to Audit Table..."))
                .get();
}

当我运行此流时,我得到“无法处理消息;嵌套异常是org.springframework.messaging.core.DestinationResolutionException:没有输出通道或replyChannel标头可用”

o.s.i.channel.PublishSubscribeChannel    : preSend on channel 'httpInAdapterPubSubChannel', message: GenericMessage [payload=Test, headers={content-length=4, http_requestMethod=POST, accept-language=en-US,en;q=0.8, accept=*/*, host=localhost:8080, http_requestUrl=http://localhost:8080/receive, connection=keep-alive, content-type=text/plain;charset=UTF-8, id=2c6ee729-96ee-1ae5-be31-a9bc56092758, cache-control=no-cache, accept-encoding=gzip, deflate, br, user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36, timestamp=1484457726393}]
o.s.i.t.MessageTransformingHandler       : org.springframework.integration.transformer.MessageTransformingHandler#0 received message: GenericMessage [payload=Test, headers={content-length=4, http_requestMethod=POST, accept-language=en-US,en;q=0.8, accept=*/*, host=localhost:8080, http_requestUrl=http://localhost:8080/receive, connection=keep-alive, content-type=text/plain;charset=UTF-8, id=2c6ee729-96ee-1ae5-be31-a9bc56092758, cache-control=no-cache, accept-encoding=gzip, deflate, br, user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36, timestamp=1484457726393}]
o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.messaging.MessagingException: Failed to handle Message; nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available] with root cause

org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:287) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:212) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:129) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.dispatcher.BroadcastingDispatcher.invokeHandler(BroadcastingDispatcher.java:236) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.dispatcher.BroadcastingDispatcher.dispatch(BroadcastingDispatcher.java:185) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423) ~[spring-integration-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) ~[spring-messaging-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) ~[spring-messaging-4.3.5.RELEASE.jar:4.3.5.RELEASE]

很明显,我在做一些非常错误的事情。我试图找到显示“发布订阅频道”VIA Spring Integration DSL或Java配置的示例。不幸的是,我找不到任何: - /。如果有人能给我一个例子并帮助我找出流程的错误,我会非常感激。

我做的另一个观察是,“当我删除订阅者'subscriber1'和'subscriber2'”时,我仍然得到相同的错误。这意味着,在配置y HttpInboundAdapter时我正在做错事。

另外,如果我将'httpInAdapterPubSubChannel'切换为直接,并且只有一个路由流(没有分支),一切正常。

1 个答案:

答案 0 :(得分:1)

.transform(new ObjectToStringTransformer())试图在某个地方发送结果,但它不知道在哪里 - 入站适配器不期望回复,变换器无处发送数据。

也许你的意思是这样......

@Bean
public IntegrationFlow receiveHttpPost() {
    return IntegrationFlows.from(Http.inboundChannelAdapter("/receive")
        .mappedRequestHeaders("*"))
        .transform(new ObjectToStringTransformer())
        .channel(httpInAdapterPubSubChannel())
        .get();
}

即。将变压器的结果发送到发布/子信道。

DSL参考有几个例子;例如herehere