一次使用spring集成一条消息来编写xml文件

时间:2016-09-14 18:10:14

标签: xml spring spring-integration

I am using Spring Integration to implement a solution where i have to read file and search some data in other file and then i need to write on xml file .

1)I have created integration flow like that :

    @Bean
    public IntegrationFlow fileIntegrationFlow(){
        return IntegrationFlows.from(readFilefromDirectory(), fileInboundPollingConsumer())
                .split(new FileSplitter())
                .transform(dataDataTransformer())
                .transform(Transformers.marshaller(getMarshaller(),resultTransformer(),true))
                .wireTap(logChannel())
                .handle(fileWriter()).get();

    }

2)File writer i have configured below like that :

    @Bean
    public FileWritingMessageHandler fileWriter() {
        FileWritingMessageHandler writer = new FileWritingMessageHandler(
                new File("D://example_tex//write"));
        writer.setExpectReply(false);
        writer.setFileExistsMode(FileExistsMode.APPEND);
        writer.setAppendNewLine(true);
        writer.setFileNameGenerator(fileNameGenerator());
        return writer;
    }

3)I have configured marshaller like that :

    @Bean
    public Marshaller getMarshaller(){
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan("com.springintegration.example");
        return marshaller;
    }

4)I have used Result transformer below like this :


    @Bean
    public ResultTransformer resultTransformer() {
        ResultToStringTransformer resultToStringTransformer= new ResultToStringTransformer();
        Properties p = new Properties();
        p.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        resultToStringTransformer.setOutputProperties(p);
        return resultToStringTransformer;

    }

So above is my configuration for Spring Integration flow .
My Question is Below :

1)After splitting one message flow goes further and it is processed and written in file .But in xml file while writing where should i configure header and footer so that it is written once and proper xml file is generated .

我有一个客户对象,它是在monitoringdata对象中列出的。我需要为这个对象创建xml文件。 @XmlRootElement 公共类MonitoringData {

List<Customer> customerList ;

public List<Customer> getCustomerList() {
    return customerList;
}

public void setCustomerList(List<Customer> customerList) {
    this.customerList = customerList;
}

}

Final Xml file after all customer object added in file Shoul look like this.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MonitoringData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">
<customer><cust-name>33334444</cust-name></customer>
<customer><cust-name>3333333</cust-name></customer>
</MonitoringData>

我能够编写客户对象部分并逐一编写但是如何在输出文件中添加Monitoringdata对象和xml头。

更新的代码流程: 我已经创建了这样的流程但是文件没有被创建:我是否想念一些东西。

@Bean
    public IntegrationFlow fileIntegrationFlow(){
        return IntegrationFlows.from(readFilefromDirectory(), fileInboundPollingConsumer())
                .split(new FileSplitter())
                .transform(dataDataTransformer())
                .wireTap(logChannel())
                .aggregate()
                .<List<Customer>>handle((p, h) -> new MonitoringData(p))
    .transform(Transformers.marshaller(getMarshaller(),resultTransformer(),true))
                .handle(fileWriter()).get();

    }

数据转换器:

@MessageEndpoint
public class DataTransformer {


    @Transformer
    public Customer transferData(String payload){
        Customer customer = new Customer();
        customer.setCustName(payload);
        return customer;
    }

日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.4.RELEASE)

2016-09-15 01:21:21.242  INFO 6672 --- [           main] c.s.e.FileIntegrationFlowConfiguration   : Starting FileIntegrationFlowConfiguration on Amar with PID 6672 (C:\Users\Amar.Singh\workspace\SpringIntegration\target\classes started by Amar.Singh in C:\Users\Amar.Singh\workspace\SpringIntegration)
2016-09-15 01:21:21.286  INFO 6672 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71a794e5: startup date [Thu Sep 15 01:21:21 CEST 2016]; root of context hierarchy
2016-09-15 01:21:22.194  INFO 6672 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:/C:/Users/Amar.Singh/.m2/repository/org/springframework/integration/spring-integration-core/4.3.1.RELEASE/spring-integration-core-4.3.1.RELEASE.jar!/META-INF/spring.integration.default.properties]
2016-09-15 01:21:22.199  INFO 6672 --- [           main] o.s.i.config.IntegrationRegistrar        : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2016-09-15 01:21:22.211  INFO 6672 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'dataTransformer' with a different definition: replacing [Generic bean: class [com.springintegration.example.DataTransformer]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\Amar.Singh\workspace\SpringIntegration\target\classes\com\springintegration\example\DataTransformer.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=serviceIntegrationFlowConfiguration; factoryMethodName=dataTransformer; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/springintegration/example/ServiceIntegrationFlowConfiguration.class]]
2016-09-15 01:21:22.231  INFO 6672 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'logFlow' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=serviceIntegrationFlowConfiguration; factoryMethodName=logFlow; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/springintegration/example/ServiceIntegrationFlowConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=fileIntegrationFlowConfiguration; factoryMethodName=logFlow; initMethodName=null; destroyMethodName=(inferred); defined in com.springintegration.example.FileIntegrationFlowConfiguration]
2016-09-15 01:21:22.233  INFO 6672 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'logChannel' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=serviceIntegrationFlowConfiguration; factoryMethodName=logChannel; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/springintegration/example/ServiceIntegrationFlowConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=fileIntegrationFlowConfiguration; factoryMethodName=logChannel; initMethodName=null; destroyMethodName=(inferred); defined in com.springintegration.example.FileIntegrationFlowConfiguration]
2016-09-15 01:21:22.328  INFO 6672 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-09-15 01:21:22.743  INFO 6672 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2016-09-15 01:21:22.747  INFO 6672 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2016-09-15 01:21:23.031  INFO 6672 --- [           main] o.s.b.f.config.PropertiesFactoryBean     : Loading properties file from URL [jar:file:/C:/Users/Amar.Singh/.m2/repository/org/springframework/integration/spring-integration-core/4.3.1.RELEASE/spring-integration-core-4.3.1.RELEASE.jar!/META-INF/spring.integration.default.properties]
2016-09-15 01:21:23.032  INFO 6672 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [class org.springframework.beans.factory.config.PropertiesFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-09-15 01:21:23.037  INFO 6672 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [class java.util.Properties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-09-15 01:21:23.581  INFO 6672 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-09-15 01:21:23.933  INFO 6672 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-09-15 01:21:23.935  INFO 6672 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.23
2016-09-15 01:21:24.095  INFO 6672 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-09-15 01:21:24.095  INFO 6672 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2811 ms
2016-09-15 01:21:24.377  INFO 6672 --- [ost-startStop-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService  'metricsExecutor'
2016-09-15 01:21:25.181  INFO 6672 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-15 01:21:25.185  INFO 6672 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'metricFilter' to: [/*]
2016-09-15 01:21:25.186  INFO 6672 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-09-15 01:21:25.186  INFO 6672 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-09-15 01:21:25.186  INFO 6672 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2016-09-15 01:21:25.187  INFO 6672 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'applicationContextIdFilter' to: [/*]
2016-09-15 01:21:25.324  INFO 6672 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService  'taskScheduler'
2016-09-15 01:21:25.649  INFO 6672 --- [           main] o.s.oxm.jaxb.Jaxb2Marshaller             : Creating JAXBContext by scanning packages [com.springintegration.example]
2016-09-15 01:21:26.207  INFO 6672 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@71a794e5: startup date [Thu Sep 15 01:21:21 CEST 2016]; root of context hierarchy
2016-09-15 01:21:26.284  INFO 6672 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-09-15 01:21:26.285  INFO 6672 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2016-09-15 01:21:26.326  INFO 6672 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-15 01:21:26.326  INFO 6672 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-15 01:21:26.368  INFO 6672 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-09-15 01:21:26.741  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/trace],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.742  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/mappings],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.742  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/configprops],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.742  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/autoconfig],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.743  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2016-09-15 01:21:26.743  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.744  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2016-09-15 01:21:26.744  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.744  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/dump],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.745  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/info],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.745  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/beans],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-09-15 01:21:26.745  INFO 6672 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/health],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2016-09-15 01:21:26.898  INFO 6672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-09-15 01:21:26.916  INFO 6672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'org.springframework.integration.channel.interceptor.WireTap#0' has been autodetected for JMX exposure
2016-09-15 01:21:26.917  INFO 6672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'org.springframework.integration.channel.interceptor.WireTap#1' has been autodetected for JMX exposure
2016-09-15 01:21:26.921  INFO 6672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'org.springframework.integration.channel.interceptor.WireTap#0': registering with JMX server as MBean [org.springframework.integration.channel.interceptor:name=org.springframework.integration.channel.interceptor.WireTap#0,type=WireTap]
2016-09-15 01:21:26.941  INFO 6672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'org.springframework.integration.channel.interceptor.WireTap#1': registering with JMX server as MBean [org.springframework.integration.channel.interceptor:name=org.springframework.integration.channel.interceptor.WireTap#1,type=WireTap]
2016-09-15 01:21:26.952  INFO 6672 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147483648
2016-09-15 01:21:26.952  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {service-activator:DBManager.handle.serviceActivator} as a subscriber to the 'dbchannel' channel
2016-09-15 01:21:26.952  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.dbchannel' has 1 subscriber(s).
2016-09-15 01:21:26.952  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started DBManager.handle.serviceActivator
2016-09-15 01:21:26.953  INFO 6672 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-09-15 01:21:26.953  INFO 6672 --- [           main] ProxyFactoryBean$MethodInvocationGateway : started serviceIntegrationFlowConfiguration$EntryPoint
2016-09-15 01:21:26.954  INFO 6672 --- [           main] GatewayCompletableFutureProxyFactoryBean : started serviceIntegrationFlowConfiguration$EntryPoint
2016-09-15 01:21:27.015  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2016-09-15 01:21:27.015  INFO 6672 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'application.errorChannel' has 1 subscriber(s).
2016-09-15 01:21:27.015  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started _org.springframework.integration.errorLogger
2016-09-15 01:21:27.016  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {bridge} as a subscriber to the 'outputchannel' channel
2016-09-15 01:21:27.016  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.outputchannel' has 1 subscriber(s).
2016-09-15 01:21:27.016  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#0
2016-09-15 01:21:27.016  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {bridge} as a subscriber to the 'fileOutboundChannel' channel
2016-09-15 01:21:27.016  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.fileOutboundChannel' has 1 subscriber(s).
2016-09-15 01:21:27.017  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#1
2016-09-15 01:21:27.017  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {transformer} as a subscriber to the 'inputchannel' channel
2016-09-15 01:21:27.017  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.inputchannel' has 1 subscriber(s).
2016-09-15 01:21:27.017  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#2
2016-09-15 01:21:27.018  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter} as a subscriber to the 'logChannel' channel
2016-09-15 01:21:27.018  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.logChannel' has 1 subscriber(s).
2016-09-15 01:21:27.018  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#3
2016-09-15 01:21:27.018  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {splitter} as a subscriber to the 'fileIntegrationFlow.channel#0' channel
2016-09-15 01:21:27.021  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.fileIntegrationFlow.channel#0' has 1 subscriber(s).
2016-09-15 01:21:27.021  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#4
2016-09-15 01:21:27.021  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {transformer} as a subscriber to the 'fileIntegrationFlow.channel#1' channel
2016-09-15 01:21:27.021  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.fileIntegrationFlow.channel#1' has 1 subscriber(s).
2016-09-15 01:21:27.022  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#5
2016-09-15 01:21:27.022  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {aggregator} as a subscriber to the 'fileIntegrationFlow.channel#2' channel
2016-09-15 01:21:27.022  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.fileIntegrationFlow.channel#2' has 1 subscriber(s).
2016-09-15 01:21:27.022  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#6
2016-09-15 01:21:27.023  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {service-activator} as a subscriber to the 'fileIntegrationFlow.channel#3' channel
2016-09-15 01:21:27.023  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.fileIntegrationFlow.channel#3' has 1 subscriber(s).
2016-09-15 01:21:27.023  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#7
2016-09-15 01:21:27.023  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {xml:marshalling-transformer} as a subscriber to the 'fileIntegrationFlow.channel#4' channel
2016-09-15 01:21:27.023  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.fileIntegrationFlow.channel#4' has 1 subscriber(s).
2016-09-15 01:21:27.024  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#8
2016-09-15 01:21:27.024  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {message-handler} as a subscriber to the 'fileIntegrationFlow.channel#5' channel
2016-09-15 01:21:27.024  INFO 6672 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'application.fileIntegrationFlow.channel#5' has 1 subscriber(s).
2016-09-15 01:21:27.024  INFO 6672 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started org.springframework.integration.config.ConsumerEndpointFactoryBean#9
2016-09-15 01:21:27.024  INFO 6672 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 1073741823
2016-09-15 01:21:27.027  INFO 6672 --- [           main] o.s.i.e.SourcePollingChannelAdapter      : started channelFileAdapter
2016-09-15 01:21:27.035  INFO 6672 --- [ask-scheduler-1] o.s.i.file.FileReadingMessageSource      : Created message: [GenericMessage [payload=D:\example_tex\in\dateeea.txt, headers={id=781992ab-70bb-4642-6954-4ca43b9101e2, timestamp=1473895287035}]]
2016-09-15 01:21:27.115  INFO 6672 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-09-15 01:21:27.120  INFO 6672 --- [           main] c.s.e.FileIntegrationFlowConfiguration   : Started FileIntegrationFlowConfiguration in 6.154 seconds (JVM running for 6.688)

1 个答案:

答案 0 :(得分:0)

好吧,我认为我们应该将页眉和页脚视为要追加的消息。

FileSplitter可以发出FileMarker s(new FileSplitter(true, true)),其中FileMarker.Mark.START您可以转换为header消息,因此FileMarker.Mark.ENDfooter

您的dataDataTransformer()应该关注FileMarker.Mark转换为适当的对象XML字符串以进行下游追加。

我想您为了这个目的需要.filter()来绕过.marshaller()转换,因为您没有从FileMarker.Mark获得适当的对象来编组有效的XML。

为此,我这样做:

@Bean
public IntegrationFlow fileIntegrationFlow() {
    return IntegrationFlows.from(readFilefromDirectory(), fileInboundPollingConsumer())
            .split(new FileSplitter(true, true))
            .filter(p -> !(p instanceof FileSplitter.FileMarker.Mark), 
                        f -> f.discardChannel("convertToHeaderOrFooterChannel"))
            .transform(dataDataTransformer())
            .transform(Transformers.marshaller(getMarshaller(),resultTransformer(),true))
            .wireTap(logChannel())
            .channel("appendToFile")
            .handle(fileWriter()).get();    
}

.filter()检查payload类型,然后放弃convertToHeaderOrFooterChannel Mark转换为所需的字符串。

convertToHeaderOrFooterChannel流程可能如下所示:

@Bean
public IntegrationFlow markTransformationFlow() {
    return IntegrationFlows.from("convertToHeaderOrFooterChannel")
            .transform(...)
            .channel("appendToFile");    
}

注意.channel(&#34; appendToFile&#34;)用法。此markTransformationFlow()将其结果发送到该主渠道中fileWriter()之前的那个渠道!

<强>更新

根据新收入

  

在文件中添加所有客户对象后的最终Xml文件。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MonitoringData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">
    <customer><cust-name>33334444</cust-name></customer>
    <customer><cust-name>3333333</cust-name></customer>
</MonitoringData>

33334444 3333333这样的数据我将解决方案视为.aggregate()来构建List<Customer>,然后MonitoringData作为结果。

只有在那之后,我才会完全整理并立即写入文件。