Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql spring批处理-如何在应用程序运行时将动态列表传递给ListItemReader并将其存储到数据库中_Mysql_Spring_Spring Batch_Spring Data Jpa_C3p0 - Fatal编程技术网

Mysql spring批处理-如何在应用程序运行时将动态列表传递给ListItemReader并将其存储到数据库中

Mysql spring批处理-如何在应用程序运行时将动态列表传递给ListItemReader并将其存储到数据库中,mysql,spring,spring-batch,spring-data-jpa,c3p0,Mysql,Spring,Spring Batch,Spring Data Jpa,C3p0,我是这个Spring批处理技术的新手,请帮助我了解如何在应用程序运行时将动态列表传递给ListItemReader并将其存储到MySql数据库 我从数据库中获取一些值,并对获取的数据进行一些计算,然后准备一个列表和这个新列表,将其传递给ListItemReader并存储到数据库中 感谢您的帮助。下面是ListItemWriter和ListItemReader的自定义实现,可用于定义name属性。此属性用作在JobExecutionContext中存储列表的键 在您的情况下,您可以有3个步骤: J

我是这个Spring批处理技术的新手,请帮助我了解如何在应用程序运行时将动态列表传递给ListItemReader并将其存储到MySql数据库

我从数据库中获取一些值,并对获取的数据进行一些计算,然后准备一个列表和这个新列表,将其传递给ListItemReader并存储到数据库中


感谢您的帮助。

下面是ListItemWriter和ListItemReader的自定义实现,可用于定义name属性。此属性用作在JobExecutionContext中存储列表的键

在您的情况下,您可以有3个步骤:

JDBCReader>ListItemWriter 计算小任务 ListItemReader>JDBCWriter 如果您的tasklet需要获取列表,您可以使用与下面相同的方法,即读/写JobExecutionContext

读者:

public class CustomListItemReader<T> implements ItemReader<T>, StepExecutionListener {

    private String name;
    private List<T> list;

    @Override
    public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

        if (list != null && !list.isEmpty()) {
            return list.remove(0);
        }
        return null;
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        list = (List<T>) stepExecution.getJobExecution().getExecutionContext().get(name);
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }

    public void setName(String name) {  
        this.name = name;
    }
}
作者:

public class CustomListItemWriter<T> implements ItemWriter<T>, StepExecutionListener {

    private String name;
    private List<T> list = new ArrayList<T>();

    @Override
    public void write(List<? extends T> items) throws Exception {
        for (T item : items) {
            list.add(item);
        }
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {}

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        stepExecution.getJobExecution().getExecutionContext().put(name, list);
        return null;
    }

    public void setName(String name) {  
        this.name = name;
    }
}

谢谢你的回复。我将尝试此解决方案并让您知道结果,还有一件事我想知道的是,我是否应该在spring配置xml文件中定义这些类?@AmitKammar否,您不需要在spring中声明这些类。那么如何启动此作业?@AmitKammar您可以像使用JobLauncher或CommandLineJobRunner的任何其他作业一样启动此作业。本文中的代码只是添加到项目中的两个类,然后您将在步骤中以读写器的身份在module-context.xml中调用它们。