Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Kotlin 这是通过Spring集成流处理结果的正确方法吗?_Kotlin_Spring Integration_Reactive - Fatal编程技术网

Kotlin 这是通过Spring集成流处理结果的正确方法吗?

Kotlin 这是通过Spring集成流处理结果的正确方法吗?,kotlin,spring-integration,reactive,Kotlin,Spring Integration,Reactive,我目前正在从事一个个人项目——在这个项目中,我需要我的Spring应用程序从EMQX(MQTT服务器)获取查询并查询其数据以获得相应的结果,然后将结果推送到带有查询UUID的主题 这是可行的——在理解Spring集成框架的工作原理很多小时之后。但是我认为处理程序使用“block”的方式是不正确的——并且与集成流的操作方式不一致。虽然这项工作正在进行中,但出于对工作的尊重,我确实希望确保这项工作正在正确进行,并避免将来出现问题 下面的代码片段应该足以理解我试图实现的目标,以及潜在的问题所在

我目前正在从事一个个人项目——在这个项目中,我需要我的Spring应用程序从EMQX(MQTT服务器)获取查询并查询其数据以获得相应的结果,然后将结果推送到带有查询UUID的主题

这是可行的——在理解Spring集成框架的工作原理很多小时之后。但是我认为处理程序使用“block”的方式是不正确的——并且与集成流的操作方式不一致。虽然这项工作正在进行中,但出于对工作的尊重,我确实希望确保这项工作正在正确进行,并避免将来出现问题

下面的代码片段应该足以理解我试图实现的目标,以及潜在的问题所在

    @Bean
fun mqttInFlow() : Publisher<Message<String>> {
    return IntegrationFlows.from(inbound())
        .handle<String> { payload, headers ->
            val emotionalOutput: EmotionalOutput = gson.fromJson(payload, EmotionalOutput::class.java)
            emotionalPrintService.populateEmotionalOutput(emotionalOutput).map {
                MessageBuilder.withPayload(gson.toJson(it))
                    .copyHeaders(headers)
                    .setHeader(MqttHeaders.TOPIC, "query/" + it.query_uuid).build()
            }.block()
        }
        .channel(outgoingChannel())
        .toReactivePublisher()
}
@Bean
乐趣mqttInFlow():发布服务器{
返回IntegrationFlows.from(inbound())
.handle{有效负载,头->
val-emotionalOutput:emotionalOutput=gson.fromJson(有效负载,emotionalOutput::class.java)
emotionalPrintService.PopulateMotionOutput(情绪输出).map{
MessageBuilder.withPayload(gson.toJson(it))
.CopyHeader(标题)
.setHeader(MqttHeaders.TOPIC,“query/”+it.query\u uuid).build()
}.block()
}
.channel(outgoingChannel())
.toReactivePublisher()
}
编辑-感谢您的建议-这是我理解的Kotlin DSL解决方案的潜在编辑-这现在产生了一个错误-抱怨输出通道或replyChannel不可用-此功能之外的任何内容都未更改

    @Bean
fun newMqttInFlow() =
    integrationFlow (inbound()) {
       wireTap {
            handle<String> { payload, headers ->
                gson.fromJson<EmotionalOutput>(payload, EmotionalOutput::class.java).let { emotionalOutput ->
                    emotionalPrintService.populateEmotionalOutput(emotionalOutput).map { populatedEmotionalOutput ->
                        MessageBuilder.withPayload(gson.toJson(populatedEmotionalOutput))
                            .copyHeaders(headers)
                            .setHeader(MqttHeaders.TOPIC, populatedEmotionalOutput.query_uuid)
                    }
                }
            }
        }
        channel("outgoingChannel")
    }
@Bean
有趣的newMqttInFlow()=
integrationFlow(入站()){
窃听{
句柄{有效负载,头->
fromJson(负载,情感输出::class.java).let{EmotionalOutput->
emotionalPrintService.PopulateMotionAlOutput(情绪输出).map{populatedEmotionalOutput->
MessageBuilder.withPayload(gson.toJson(PopulatedOutput))
.CopyHeader(标题)
.setHeader(MqttHeaders.TOPIC,populatedEmotionalOutput.query_uuid)
}
}
}
}
渠道(“外流渠道”)
}
例外情况是:

例外情况为org.springframework.messaging.core.DestinationResolutionException:没有可用的输出通道或replyChannel标头

虽然我有多年的Java经验,但这种方法是新的,所以非常感谢您的帮助。非常感谢。如果整堂课都有用的话,我可以把它贴出来

编辑

这是配置文件-它可以更好地了解导致此次要错误的原因-

021-03-28 21:59:48.008错误84492---[T Call:divnrin]o.s.integration.handler.LoggingHandler:org.springframework.messaging.MessageHandlingException:消息处理程序[bean'mqttOutbound'中发生错误;在以下内容中定义:“类路径资源[io/divnr/appserver/configuration/MQTTConfiguration.class]’;来源:org.springframework.core.type.classreading。SimpleMethodMetadata@4a9419d7']; 嵌套异常为java.lang.IllegalArgumentException:此默认转换器只能处理“byte[]”或“String”有效负载;考虑将转换器添加到流定义中,或者提供一个ByTestMasgEMAP,或为ReaTr.C..PusiSig.MyAppFuffAsHealStices,ReaveDebug=GuangICMeMe[有效载荷= MaMaPoFultEnable,Healths={mqtt_receivedRetained=false,mqtt_id=0,mqtt_duplicate=false,id=c5a75283-c0fe-ebac-4168-DABDD989DA9,mqtt_receivedTopic=source/d9e50e8f-67e0-4505-7ca2-4d05b1242207,mqtt_receivedQos=0,时间戳=161696158804}] 位于org.springframework.integration.support.utils.IntegrationUtils.wrapinHandlingExceptionIfNeeded(IntegrationUtils.java:192) 位于org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:65) 在

这里提供了完整的课程

@Configuration
@EnableIntegration
@IntegrationComponentScan
class MQTTConfiguration(val emotionalPrintService: EmotionalPrintService,
                    val gson: Gson,
                    val applicationConfiguration: ApplicationConfiguration) {

@Bean
fun mqttServiceFactory() : MqttPahoClientFactory {
    return DefaultMqttPahoClientFactory().apply {
        connectionOptions = MqttConnectOptions().apply {
            serverURIs = arrayOf<String>(applicationConfiguration.mqttServerAddress)
        }
    }
}

@Bean
fun newMqttInFlow() =
    integrationFlow (inbound()) {
        handle<String> { payload, headers ->
            gson.fromJson<EmotionalOutput>(payload, EmotionalOutput::class.java).let { emotionalOutput ->
                emotionalPrintService.populateEmotionalOutput(emotionalOutput).map { populatedEmotionalOutput ->
                    MessageBuilder.withPayload(gson.toJson(populatedEmotionalOutput))
                        .copyHeaders(headers)
                        .setHeader(MqttHeaders.TOPIC, populatedEmotionalOutput.query_uuid).build()
                }
            }
        }
        channel(outgoingChannel())
    }

@Bean
@ServiceActivator(requiresReply = "false", inputChannel = "outgoingChannel")
fun mqttOutbound(): MessageHandler {
    val messageHandler = MqttPahoMessageHandler("divnrout", mqttServiceFactory())
    messageHandler.setAsync(true)
    return messageHandler
}

@Bean
fun outgoingChannel() : FluxMessageChannel {
    return FluxMessageChannel()
}

@Bean
fun inbound(): MessageProducerSupport {
    return MqttPahoMessageDrivenChannelAdapter("divnrin", mqttServiceFactory(),
        "source/" + applicationConfiguration.sourceUuid).apply {
        setConverter(DefaultPahoMessageConverter())
        setQos(1)
    }
}
}
@配置
@使能集成
@集成组件扫描
类MQTTConfiguration(val-emotionalPrintService:emotionalPrintService,
瓦尔·格森:格森,
val应用程序配置:应用程序配置){
@豆子
乐趣mqttServiceFactory():MqttPahoClientFactory{
返回DefaultMqttPahoClientFactory().apply{
connectionOptions=MqttConnectOptions().apply{
serverURIs=arrayOf(applicationConfiguration.mqttServerAddress)
}
}
}
@豆子
有趣的newMqttInFlow()=
integrationFlow(入站()){
句柄{有效负载,头->
fromJson(负载,情感输出::class.java).let{EmotionalOutput->
emotionalPrintService.PopulateMotionAlOutput(情绪输出).map{populatedEmotionalOutput->
MessageBuilder.withPayload(gson.toJson(PopulatedOutput))
.CopyHeader(标题)
.setHeader(MqttHeaders.TOPIC,populatedEmotionalOutput.query_uuid).build()
}
}
}
通道(outgoingChannel())
}
@豆子
@ServiceActivator(requiresReply=“false”,inputChannel=“outgoingChannel”)
fun mqttOutbound():MessageHandler{
val messageHandler=MqttPahoMessageHandler(“divnrout”,mqttServiceFactory())
messageHandler.setAsync(true)
返回消息处理程序
}
@豆子
娱乐频道():FluxMessageChannel{
返回FluxMessageChannel()
}
@豆子
fun inbound():MessageProducerSupport{
返回MQTTPAHomeMessageDrivenChannelAdapter(“divnrin”,mqttServiceFactory(),
“source/”+applicationConfiguration.sourceUuid)。应用{
setConverter(DefaultPaHomeMessageConverter())
setQos(1)
}
}
}

你最终确实不需要这个
block()
handle<String>( { payload, headers ->
        gson.fromJson<EmotionalOutput>(payload, EmotionalOutput::class.java).let { emotionalOutput ->
            emotionalPrintService.populateEmotionalOutput(emotionalOutput).map { populatedEmotionalOutput ->
                MessageBuilder.withPayload(gson.toJson(populatedEmotionalOutput))
                    .copyHeaders(headers)
                    .setHeader(MqttHeaders.TOPIC, populatedEmotionalOutput.query_uuid).build()
            }
        }
    }) { async(true) }