Spring Integration-如何将直接通道中接收到的消息有效负载转换为临时文件

时间:2018-06-20 09:41:44

标签: spring-integration

要求是必须使用spring集成将在特定直接通道中接收到的消息有效负载转换为json并存储在临时文件中。

enter code here

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

    <int:channel id="requestChannel">
        <int:dispatcher task-executor="preValidationWorkers"/>
    </int:channel>

    <task:executor id="preValidationWorkers" pool-size="6" queue-capacity="6" rejection-policy="CALLER_RUNS"/>

    <int:channel id="validaitonSuccessChannel">
        <int:dispatcher task-executor="validationWorkers"/>
    </int:channel>

    <task:executor id="validationWorkers" pool-size="3" queue-capacity="3" rejection-policy="CALLER_RUNS"/>

    <int:channel id="transformationSuccessChanel">
        <int:dispatcher task-executor="transformationWorkers"/>
    </int:channel>

    <task:executor id="transformationWorkers" pool-size="3" queue-capacity="3" rejection-policy="CALLER_RUNS"/>


    <int:service-activator id="requestDistributor" input-channel="requestChannel"
                           ref="personValidatorService"
                           method="validate"
                           output-channel="validaitonSuccessChannel">
    </int:service-activator>

    <int:service-activator id="requestTransformer"
                           input-channel="validaitonSuccessChannel"
                           ref="personTransformationService"
                           method="transform"
                           output-channel="transformationSuccessChanel">
    </int:service-activator>

    <int:service-activator id="requestInjector"
                           input-channel="transformationSuccessChanel"
                           ref="personSqlService"
                           method="inject">
    </int:service-activator>

    <bean id="personValidatorService"
          class="com.naveendc.taskexecutor.service.validation.PersonValidatorServiceImpl" lazy-init="true">
    </bean>

    <bean id="personTransformationService"
          class="com.naveendc.taskexecutor.service.transformation.PersonTransformationServiceImpl" lazy-init="true">
    </bean>

    <bean id="personSqlService"
          class="com.naveendc.taskexecutor.service.sql.PersonSqlServiceImpl" lazy-init="true">
    </bean>

    <int:gateway id="personGateway" service-interface="com.naveendc.taskexecutor.service.integration.PersonGateway"></int:gateway>

    <context:annotation-config/>
    <int:annotation-config/>
    <context:component-scan base-package="com.naveendc.taskexecutor.service, com.naveendc.taskexecutor.service.integration"/>

</beans>


Service activators method is something like this.
 @Override
    public void inject(Person person) {
        System.out.println("Person injected ==>" + person.toString()));
        return;
 }

是否有一种方法可以使用spring集成将在conversionSuccessChannel中接收的有效载荷转换为json格式并将其存储在临时文件中?

1 个答案:

答案 0 :(得分:1)

首先,您的代码段中有错别字-transformationSuccessChanel。如此大的域特定代码很难执行搜索。

不确定您的目标是什么,但这绝对不是渠道责任。

出于转换目的,Spring Integration中有一个<transformer>https://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/messaging-transformation-chapter.html

因此,如果您的<int:service-activator id="requestInjector">希望使用带有JSON内容的tmp文件,则只需在<int:service-activator id="requestTransformer">之前和之后放置几个<transformer>:一个即可转换{{ 1}}转换为JSON(payload应该为您解决了问题),然后再创建一个包含该内容的文件。我认为对于文件,最好使用<object-to-json-transfrormer>将内容写入文件并获得<int-file:outbound-gateway>作为答复:https://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/files.html#file-writing

您还可以将所有逻辑组合到java.io.File中:https://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/messaging-routing-chapter.html#chain