繁体   English   中英

Spring集成与Spring DSL的“发布订阅频道”

[英]Spring Integration “Publish Subscribe Channel” with Spring DSL

我正在尝试构建一个简单的流程,在此流程中,我先通过HTTP入站通道适配器接收HTTP发布请求,然后将其发布到“ SubscribableChannel”。 可能有“ N”个用户订阅了此频道。 下图显示了流程。

在此处输入图片说明

我正在尝试使用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:没有可用的output-channel或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]

很明显,我在这里做错了什么。 我试图通过Spring Integration DSL或Java Configuration查找显示“发布订阅频道”的示例。 不幸的是,我找不到任何:-/。 如果有人可以给我提供一个例子,并帮助我找出我的工作流程出了什么问题,我将不胜感激。

我的另一观察结果是:“当我删除订户的'subscriber1'和'subscriber2'时,我仍然遇到相同的错误。 因此,这意味着在配置y HttpInboundAdapter时出现了问题。

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

.transform(new ObjectToStringTransformer())尝试将结果发送到某个地方,但它不知道在哪里-入站适配器不希望收到答复,并且转换器无处发送数据。

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

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

即将转换器的结果发送到发布/订阅通道。

DSL参考有几个示例。 例如在这里这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM