Spring XD 内部数据库日志记录

Spring XD internal database logging

我希望减少保存在 Spring-XD 内部数据库中的内容,因为在总共 500 000 个条目之后它会使服务器的容量过载;每小时、每分钟或每秒安排的许多作业执行的日志记录中的 IO 太多

因此,我必须每 4 天截断一次,如下所示:

truncate springxd.batch_step_execution_context;
truncate springxd.batch_step_execution;
truncate springxd.batch_job_execution_context;
truncate springxd.batch_job_execution_params;
truncate springxd.batch_job_execution;

有什么建议吗?是否有任何 springxd 配置只能持续 2 天?

谢谢

你可能想写一个这样的工作。有一些可用的 tasklet,您可能希望稍加修改即可重用它们。您可以查看 here

以下代码来自 article - 免责声明我不是作者,但我们做过类似的事情。

    import javax.sql.DataSource;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.apache.commons.lang3.time.DateUtils;
    import org.apache.log4j.Logger;
    import org.springframework.batch.core.StepContribution;
    import org.springframework.batch.core.scope.context.ChunkContext;
    import org.springframework.batch.core.step.tasklet.Tasklet;
    import org.springframework.batch.repeat.RepeatStatus;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.jdbc.core.JdbcTemplate;

    public class DeleteAllArchives implements Tasklet{

        private static Logger log = Logger.getLogger(DeleteAllArchives.class);

        private static final String SQL_DELETE_BATCH_STEP_EXECUTION_CONTEXT = “DELETE FROM BATCH_STEP_EXECUTION_CONTEXT WHERE STEP_EXECUTION_ID IN (SELECT STEP_EXECUTION_ID FROM BATCH_STEP_EXECUTION WHERE JOB_EXECUTION_ID IN (SELECT JOB_EXECUTION_ID FROM  BATCH_JOB_EXECUTION where CREATE_TIME < ?))”;
        private static final String SQL_DELETE_BATCH_STEP_EXECUTION = “DELETE FROM BATCH_STEP_EXECUTION WHERE JOB_EXECUTION_ID IN (SELECT JOB_EXECUTION_ID FROM BATCH_JOB_EXECUTION where CREATE_TIME < ?)”;
        private static final String SQL_DELETE_BATCH_JOB_EXECUTION_CONTEXT = “DELETE FROM BATCH_JOB_EXECUTION_CONTEXT WHERE JOB_EXECUTION_ID IN (SELECT JOB_EXECUTION_ID FROM  BATCH_JOB_EXECUTION where CREATE_TIME < ?)”;
        private static final String SQL_DELETE_BATCH_JOB_EXECUTION_PARAMS = “DELETE FROM BATCH_JOB_EXECUTION_PARAMS WHERE JOB_EXECUTION_ID IN (SELECT JOB_EXECUTION_ID FROM BATCH_JOB_EXECUTION where CREATE_TIME < ?)”;
        private static final String SQL_DELETE_BATCH_JOB_EXECUTION = “DELETE FROM BATCH_JOB_EXECUTION where CREATE_TIME < ?”;
        private static final String SQL_DELETE_BATCH_JOB_INSTANCE = “DELETE FROM BATCH_JOB_INSTANCE WHERE JOB_INSTANCE_ID NOT IN (SELECT JOB_INSTANCE_ID FROM BATCH_JOB_EXECUTION)”;
        private static final Integer DEFAULT_RETENTION_MONTH = 1;
        private Integer historicRetentionMonth = DEFAULT_RETENTION_MONTH;

        @Autowired
        @Qualifier(“dataSource”)
        private DataSource dataSource;

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext context)
        throws Exception {

            deleteEmailLetterArchive(contribution, new JdbcTemplate(dataSource));
            return RepeatStatus.FINISHED;
        }

        private void deleteEmailLetterArchive(StepContribution contribution, JdbcTemplate template) {

            int totalCount = 0;
            Date date = DateUtils.addMonths(new Date(), -historicRetentionMonth);
            DateFormat df = new SimpleDateFormat();
            log.info(“Remove the Spring Batch history before the { ” + df.format(date));

            int rowCount = template.update(SQL_DELETE_BATCH_STEP_EXECUTION_CONTEXT, date);
            log.info(“Deleted rows number from the BATCH_STEP_EXECUTION_CONTEXT table: { ” + rowCount + ” }”);
            totalCount += rowCount;

            rowCount = template.update(SQL_DELETE_BATCH_STEP_EXECUTION, date);
            log.info(“Deleted rows number from the BATCH_STEP_EXECUTION table: { ” + rowCount + ” }”);
            totalCount += rowCount;

            rowCount = template.update(SQL_DELETE_BATCH_JOB_EXECUTION_CONTEXT, date);
            log.info(“Deleted rows number from the BATCH_JOB_EXECUTION_CONTEXT table: { ” + rowCount + ” }”);
            totalCount += rowCount;

            rowCount = template.update(SQL_DELETE_BATCH_JOB_EXECUTION_PARAMS, date);
            log.info(“Deleted rows number from the BATCH_JOB_EXECUTION_PARAMS table: { ” + rowCount + ” }”);
            totalCount += rowCount;

            rowCount = template.update(SQL_DELETE_BATCH_JOB_EXECUTION, date);
            log.info(“Deleted rows number from the BATCH_JOB_EXECUTION table: { ” + rowCount + ” }”);
            totalCount += rowCount;

            rowCount = template.update(SQL_DELETE_BATCH_JOB_INSTANCE);
            log.info(“Deleted rows number from the BATCH_JOB_INSTANCE table: { ” + rowCount + ” }”);
            totalCount += rowCount;

            contribution.incrementWriteCount(totalCount);
        }
    }