具有唯一jobParamIncrementer的Spring批处理作业

时间:2013-06-04 09:25:44

标签: spring-batch

我为简单查询编写了一个Spring批处理应用程序。第一次使用某个作业ID时,这样可以正常工作,但是对于下一次执行,它需要新的作业ID。对于这个写的jobParamIncrementer类,仍然得到bean defn未找到异常。我不知道发生了什么事。请帮帮我。

下面是堆栈跟踪。

Job Paramater {run.id=1370337260531}
Jun 4, 2013 2:44:20 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@10b9d04: startup date [Tue Jun 04 14:44:20 IST 2013]; root of context hierarchy
Jun 4, 2013 2:44:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Jun 4, 2013 2:44:20 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'uniqueJob': replacing [Generic bean: class [org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.batch.core.configuration.xml.JobParserJobFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
Jun 4, 2013 2:44:21 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ccc588: defining beans [dataSource,transactionManager,jdbcTemplate,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,jobRepository,jobLauncher,org.springframework.batch.core.scope.internalStepScope,org.springframework.beans.factory.config.CustomEditorConfigurer,org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor,step1,uniqueJob,e4XCountriesRowMapper,jobParamIncrementer,cursorReader,flatFileWriter]; root of factory hierarchy
Jun 4, 2013 2:44:21 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Jun 4, 2013 2:44:21 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
Jun 4, 2013 2:44:21 PM org.springframework.batch.core.launch.support.CommandLineJobRunner start
SEVERE: Job Terminated in error: No bean named '{run.id=1370337260531}' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '{run.id=1370337260531}' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:568)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1045)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:339)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574)
    at com.JobRunner.main(JobRunner.java:179)
Jun 4, 2013 2:44:21 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@10b9d04: startup date [Tue Jun 04 14:44:20 IST 2013]; root of context hierarchy
Jun 4, 2013 2:44:21 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ccc588: defining beans [dataSource,transactionManager,jdbcTemplate,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,jobRepository,jobLauncher,org.springframework.batch.core.scope.internalStepScope,org.springframework.beans.factory.config.CustomEditorConfigurer,org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor,step1,uniqueJob,e4XCountriesRowMapper,jobParamIncrementer,cursorReader,flatFileWriter]; root of factory hierarchy
下面的

是applicationContext.xml

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



    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://<ip>/test" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> 
        <property name="databaseType" value="MYSQL"/>
         <property name="dataSource" ref="dataSource"/> 
         <property name="transactionManager" ref="transactionManager"/> </bean>


    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>


        <!-- BD to CSV -->

    <!-- <batch:job id="sampleJob" incrementer="jobParamIncrementer"> -->
    <batch:job id="uniqueJob" incrementer="jobParamIncrementer">
        <batch:step id="step1">
            <batch:tasklet>
                <batch:chunk reader="cursorReader" writer="flatFileWriter"
                    commit-interval="1000" />
            </batch:tasklet>
        </batch:step>
    </batch:job>
    <bean id="e4XCountriesRowMapper" class="com.E4XCountriesRowMapper">
    </bean>
    <bean name="jobParamIncrementer" class="com.JobParamIncrementer">
    </bean>



    <bean id="cursorReader"
        class="org.springframework.batch.item.database.JdbcCursorItemReader">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="select * from e4x_countries" />
        <property name="rowMapper" ref="e4XCountriesRowMapper" />
    </bean>


    <bean id="flatFileWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
        <property name="resource"
            value="file:fileNme.txt" />

        <property name="lineAggregator">
            <bean
                class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="delimiter" value="," /> <!-- default -->

                <property name="fieldExtractor">
                    <bean
                        class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names"
                            value="countryCode,countryName,currencyCode,currencyName,currencySymbol" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>


</beans>

JobParameterIncrementer.java

public class JobParamIncrementer implements JobParametersIncrementer {
    public JobParameters getNext(JobParameters parameters) {
        System.out.println("got job parameters: " + parameters);
        /*if (parameters==null || parameters.isEmpty()) {
            return new JobParametersBuilder().addLong("run.id", 1L).toJobParameters();
        }*/
        return new JobParametersBuilder().addLong("run.id", Calendar.getInstance().getTimeInMillis()).toJobParameters();
        /*long id = parameters.getLong("run.id",Calendar.getInstance().getTimeInMillis()) + 1;
        JobParametersBuilder jobParamBuilder  = new JobParametersBuilder().addLong("run.id", id);
        return  jobParamBuilder.toJobParameters();*/

    }
}

JobRunner(主要课程)

public class JobRunner {

    public static void main(String[] args) {
        JobParamIncrementer jobName = new JobParamIncrementer();
        try {
            String id=jobName.getNext(null)+"";
            System.out.println("Job Paramater "+id+"");
            CommandLineJobRunner.main(new String[] { "applicationContext.xml", id});
            System.out.println("DONE");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

看起来您没有将作业名称传递给CommandLineJobRunner。 尝试将JobRunner.main()方法中的CommandLineJobRunner.main调用更改为:

CommandLineJobRunner.main(new String[] { "applicationContext.xml", "uniqueJob", id});

答案 1 :(得分:0)

解决了:)在应用程序上下文中添加了以下代码行

<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
 <property name="dataSource" ref="dataSource"/>
</bean> 

和JobRunner.java

CommandLineJobRunner.main(new String[] { "applicationContext.xml", "uniqueJob","-next"});

感谢所有人的时间和帮助...