在spring batch admin中读取外部属性文件

前提:我是 Spring 季批次新手 .
我'm trying to customize the spring batch admin but , till now, i'我无法让我的类从外部文件中读取一些属性 .
我的作业从第三方数据库中提取数据以打印报告,因此我需要两个数据源:一个用于收集报告信息,另一个用于存储作业的状态和元数据 .
我已经阅读了教程:http://docs.spring.io/spring-batch-admin/reference/customization.html
和许多其他教程和以下帖子load external config file in spring batch admin .
然而,我无法启动该应用程序 . 它必须在Tomcat 6上运行 .
这是我的项目树的屏幕截图,由官方示例改编:

enter image description here

这是我的配置类:

@Configuration
@EnableBatchProcessing
public class RegistroGiornalieroConfiguration extends DefaultBatchConfigurer {

    private final static Logger log = LoggerFactory.getLogger(RegistroGiornalieroConfiguration.class);

    private final static Date data = null;

    @Autowired
    public JobBuilderFactory jobFactory;

    @Autowired
    public StepBuilderFactory stepFactory;


    @Autowired
    VolumiPropertySource volumiPropertySource;

    @Value("${batch.jdbc.driver}")
    private String driverName; 

    /**
     * datasource di default, viene resitituito dall'annotazione Autowired
     * @return
     * @throws SQLException
     */
    @Bean(name="springBatchDatasource")
    @Primary
    public DataSource springBatchDatasource() throws SQLException {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverName);//"com.mysql.jdbc.Driver");
//      dataSource.setUrl(env.getProperty("springBatchUrl"));//"jdbc:mysql://localhost/spring_batch_annotations");
//      dataSource.setUsername(env.getProperty("springBatchUser"));//"root");
//      dataSource.setPassword(env.getProperty("springBatchPassword"));//"root");
        return dataSource;
    }


    /**
     * Datasource secondario, viene restituito da getDatasource
     * @return
     * @throws SQLException
     */
    @Bean(name="estarDatasource")
    public DataSource estarDatasource() throws SQLException {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverName);//"com.mysql.jdbc.Driver");
//      dataSource.setUrl(env.getProperty("url"));//"jdbc:mysql://localhost/spring_batch_annotations");
//      dataSource.setUsername(env.getProperty("user"));//"root");
//      dataSource.setPassword(env.getProperty("password"));//"root");
        return dataSource;
    }


//  @PostConstruct
//  public void init()
//  {
//      Properties p = volumiPropertySource.getIniProperties();
//      MutablePropertySources sources = env.getPropertySources();
//      sources.addFirst(new PropertiesPropertySource("volumi",p));
//      env.getActiveProfiles();
//  }



    @Bean
    @StepScope
    public JdbcCursorItemReader<RegistroGiornaliero> reader( 
            @Value("#{jobParameters[data]}")
            Date data)
        {
        System.out.println("reader");

//       select     protocol.nprotoc,protocol.dataprot,protocol.arrpar,protocol.numdoc,
//          protocol.datadoc,protocol.OGGETTO,protocol.ANNULLATO,protocol.USERINS as protuser,
//          protimg.DOCID,protimg.USERINS imguser ,protimg.PRINCIPALE,
//          assegna.livello,assegna.possesso,assegna.SEQUENZA,
//          organigramma.LIVELLO,organigramma.DESCRIZIONE,
//          protente.ente
//          from 
//         protocol left outer join protimg on protocol.NPROTOC = protimg.NPROTOC 
//         left outer join protente on protocol.NPROTOC = protente.NPROTOC
//         left outer join assegna on protocol.NPROTOC = assegna.NPROTOC
//         left outer join organigramma on assegna.LIVELLO = organigramma.LIVELLO
//          where 
//              protocol.dataprot = 20160616 and protocol.NPROTOC = '201600014709'
//          order by protocol.nprotoc,assegna.SEQUENZA;     


         StringBuilder sb = new StringBuilder("select")
            .append(" protocol.nprotoc,protocol.dataprot,protocol.arrpar,protocol.numdoc,")
            .append(" protocol.datadoc,protocol.OGGETTO,protocol.ANNULLATO,protocol.USERINS as protuser,")
            .append(" protimg.DOCID,protimg.USERINS imguser ,protimg.PRINCIPALE,")
            .append(" assegna.possesso,assegna.SEQUENZA,")
            .append(" organigramma.LIVELLO,organigramma.DESCRIZIONE,")
            .append(" protente.ente ")
            .append(" from protocol left outer join protimg on protocol.NPROTOC = protimg.NPROTOC  ")
            .append(" left outer join protente on protocol.NPROTOC = protente.NPROTOC  ")
            .append(" left outer join assegna on protocol.NPROTOC = assegna.NPROTOC  ")
            .append(" left outer join organigramma on assegna.LIVELLO = organigramma.LIVELLO  ")
            .append(" where ")
            .append(" protocol.dataprot = ? ")
            .append(" order by protocol.nprotoc,assegna.SEQUENZA "); 


//      StringBuilder sb = new StringBuilder("select")
//          .append(" protocol.nprotoc,protocol.dataprot,protocol.arrpar,protocol.numdoc,")
//          .append(" protocol.datadoc,protocol.OGGETTO,protocol.ANNULLATO,protocol.USERINS as protuser, ")
//          .append(" protimg.DOCID,protimg.USERINS imguser ,protimg.PRINCIPALE, ")
//          .append(" assegna.livello,assegna.possesso,assegna.SEQUENZA, ")
//          .append(" organigramma.LIVELLO as livelloOrg,organigramma.DESCRIZIONE, ")
//          .append(" protente.ente ")
//          .append(" from protocol,protimg,protente,assegna,operatori,organigramma ")
//          .append(" where ")
//          .append(" protocol.NPROTOC = protimg.NPROTOC and protocol.NPROTOC = assegna.NPROTOC ")
//          .append(" and protocol.NPROTOC = protente.NPROTOC and assegna.LIVELLO = organigramma.LIVELLO ")
//          .append(" and protocol.dataprot = ? ");
        JdbcCursorItemReader<RegistroGiornaliero> reader = new JdbcCursorItemReader<RegistroGiornaliero>();
        try {
            reader.setDataSource(estarDatasource());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        reader.setPreparedStatementSetter(new ParameterSetter(data));
        reader.setSql(sb.toString());
        reader.setRowMapper(estarRowMapper());
        reader.setVerifyCursorPosition(false);
        return reader;
    }

    @Bean
    public RegistroGiornalieroProcessor processorGenerazioneRegistro() {
        return new RegistroGiornalieroProcessor();
    }

    @Bean
    public ItemWriter<RegistroGiornaliero> pdfwriter() {
        return new PDFItemWriter<RegistroGiornaliero>();
    }


    @Bean
    public JobExecutionListener listener() {
        return new JobCompletionNotificationListener();
    }

    @Bean
    public RegistroGiornalieroRowMapper estarRowMapper() {
        return new RegistroGiornalieroRowMapper();
    }



    @Bean 
    public Job generaRegistroGiornaliero()
    {
          return jobFactory
                    .get("generaRegistroGiornaliero")
                    .incrementer(new RunIdIncrementer())
                    .flow(leggiDocumentiProtocollo())
                    .end()
                    .build(); 
    }


    @Bean 
    public Step leggiDocumentiProtocollo()
    {
        return stepFactory.get("leggiDocumentiProtocollo")
                .<RegistroGiornaliero, RegistroGiornaliero> chunk(10)
                .reader(reader(data))
                .processor(processorGenerazioneRegistro())
                .writer(pdfwriter())
                .build();
    }



    @Override
    public JobRepository getJobRepository() {


        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        // use the autowired data source
        try {
            factory.setDataSource(springBatchDatasource());
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        factory.setTransactionManager(getTransactionManager());

        try {
            factory.afterPropertiesSet();
            return factory.getObject();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }
        return null;
    }



}

这是我的工作配置(registrogiornaliero.xml):

<?xml version="1.0" encoding="utf-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">        
    <context:annotation-config/>
    <context:component-scan base-package="it.infogroup.estav.registrogiornaliero"/>
</beans>

最后这是我的env.xml,如stackoverflow帖子:

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

    <!-- Use this to set additional properties on beans at run time -->
    <bean id="placeholderProperties"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                <value>classpath:batch-default.properties</value>
                <!-- <value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>-->
                <value>classpath:batch-${ENVIRONMENT:mysql}.properties</value>
                <!-- here we load properties from external config folder -->
                <value>file:${app.conf}</value>
             </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="false" />
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="order" value="1" />
    </bean>
</beans>

任何帮助将不胜感激

回答(1)

2 years ago

免责声明:我是 Spring 季批次的新手,因此这不是最好的解决方案:

  • 我在配置类上使用了propertysource注释,我删除了它并更改了env-context.xml,如下所示:

http://www.springframework.org/schema/beans/spring-beans.xsd“>

<!-- Use this to set additional properties on beans at run time -->
<bean id="placeholderProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
            <value>${app.conf}/batch-default.properties</value>
            <!-- <value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>-->
            <value>${app.conf}/batch-${ENVIRONMENT}.properties</value>
            <!-- here we load properties from external config folder -->
            <value>${app.conf}/estardatasource.properties</value>
         </list>
    </property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="false" />
    <property name="ignoreUnresolvablePlaceholders" value="false" />
    <property name="order" value="1" />
</bean>

$ 是一个jvm参数 .
2.我已经用占位符变量es替换了Environment类的使用 . :

@Value("${volumi}")
private String volumi;

问题2:多个数据源,有很多帖子描述了不同的解决方案,包括重写DefaultBatchConfigurer类及其getJobRepository方法 .
对我有用的唯一解决方案是添加

@ComponentScan(basePackageClasses = DefaultBatchConfigurer.class)

Use of multiple DataSources in Spring Batch中描述(并清楚解释)的配置类

并覆盖data-source-context.xml文件:我把它放在/ src / main / resources / META-INF / spring / batch / override /下 .
其内容是:

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

    <bean id="estarDatasource" name="estarDatasource" primary="false"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${batch.jdbc.driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
        <property name="testWhileIdle" value="${batch.jdbc.testWhileIdle}"/>
        <property name="validationQuery" value="${batch.jdbc.validationQuery}"/>
    </bean>

    <!--  Initialise the database if enabled: -->
    <jdbc:initialize-database data-source="estarDatasource" enabled="${batch.data.source.init}" ignore-failures="DROPS">
        <jdbc:script location="${batch.drop.script}"/>
        <jdbc:script location="${batch.schema.script}"/>
        <jdbc:script location="${batch.business.schema.script}"/>
    </jdbc:initialize-database>


    <bean id="dataSource" autowire-candidate="true" primary="true" 
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${batch.jdbc.driver}" />
        <property name="url" value="${batch.jdbc.url}" />
        <property name="username" value="${batch.jdbc.user}" />
        <property name="password" value="${batch.jdbc.password}" />
        <property name="testWhileIdle" value="${batch.jdbc.testWhileIdle}"/>
        <property name="validationQuery" value="${batch.jdbc.validationQuery}"/>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--  Initialise the database if enabled: -->
    <jdbc:initialize-database data-source="dataSource" enabled="${batch.data.source.init}" ignore-failures="DROPS">
        <jdbc:script location="${batch.drop.script}"/>
        <jdbc:script location="${batch.schema.script}"/>
        <jdbc:script location="${batch.business.schema.script}"/>
    </jdbc:initialize-database>

</beans>

希望这有助于其他一些初学者,我很高兴知道这是否是一个合适的解决方案,并最终如何将属性读入Environment类 .