【问题标题】:Write xml file using spring integration one message at one time使用spring集成一次写入一条消息的xml文件
【发布时间】:2016-09-14 18:10:13
【问题描述】:
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 .

我有一个客户对象,它在监控数据对象中列出。我需要为这个对象创建 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>

我能够编写客户对象部分,它正在一个一个地写入但是如何在输出文件中添加监控数据对象和 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)

【问题讨论】:

  • @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(); }
  • 不要将代码放入 cmets - 它不可读 - 请改为编辑您的问题。
  • 请学习这个stackoverflow.com/help/how-to-ask。它的可读性不好。我会尝试调查你的问题,但这并不能保证我能够看到整个情况。
  • 我更新了问题
  • 已答复。干杯!

标签: xml spring spring-integration


【解决方案1】:

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

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

您的 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("appendToFile") 的用法。这个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&lt;Customer&gt; 然后MonitoringData 作为结果。

只有在那之后,我才会将它完全编组并立即写入文件。

【讨论】:

  • 但实际上标题不是来自 readFilefromDirectory() 文件,txt 文件中只有数据为 11111,22222 。我们在拆分后逐行读取,然后将对象创建为@XmlRootElement public class Customer { private Long custId;私有字符串 custName;私人字符串 custDept;
  • 对,这就是我建议依赖FileSplitter.FileMarker.Mark 功能的原因。
  • 但是页眉是对象本身的一部分,所以我应该为页眉和页脚硬编码它,我认为这不是一个好主意。当对象被编组然后生成 xml 标头时,我正在谈论但该对象为每条消息生成每次均值
  • ???这听起来像是一个不同的故事。即使它与您的问题有关,您的解释也并不明显。
  • OTOH,我不明白您将如何在没有root 元素的情况下将多个 XML 写入一个文件。这不符合 XML 规范。我的想法是为Marks 准确生成root 元素,其余的项目只是目标XML 中的常规兄弟。所以,让我们回到原来的任务!你必须做什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 2012-10-25
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 2011-12-19
相关资源
最近更新 更多