【发布时间】:2023-03-25 06:53:01
【问题描述】:
我们设置了一个 Spring Batch 批处理电子邮件发件人作业,该作业执行以下操作:
- 从 JMS 队列中读取传入电子邮件的详细信息。
- 不做任何处理,直接通过...
- 将详细信息转换为电子邮件并将其“写入”到 SMTP
- 无限重复
这通常可以正常工作,但我们发现存在大量内存泄漏。 9 小时左右后,可以看到单个 FlowJob,但有 748 个 JobExecution(全部为 'STARTED'),每个拥有 778 个(!)StepExecution 实例。总而言之,900 MB 的东西。
这是 Spring 配置(Spring 3.1,Spring Batch 2.1.9):
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
</property>
</bean>
<bean id="jmsEmailFetcher" class="org.springframework.batch.item.jms.JmsItemReader">
<property name="jmsTemplate" ref="batchEmailJmsTemplate" />
</bean>
<bean id="passthroughProcessor" class="org.springframework.batch.item.support.PassThroughItemProcessor" />
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<!-- The Spring Batch *Limiter/Decider* -->
<bean id="ourLimitDecider" class="our.special.InfiniteThrottledExecutionDecider" />
<!-- The Spring Batch *Job* -->
<batch:job id="fetchEmailsJobBatch" restartable="true">
<batch:step id="fetchEmailsStep" next="limitDecision">
<batch:tasklet throttle-limit="10">
<batch:chunk reader="jmsEmailFetcher" processor="passthroughProcessor"
writer="batchEmailService.javaMailItemWriter" commit-interval="100" skip-limit="999999999"> <!-- Might gets *lots* of MQ-not-up-yet fails -->
<batch:skippable-exception-classes>
<batch:include class="org.springframework.jms.JmsException" /> <!-- Generally MQ-not-up-yet ConnectionException, or Session-closed (in tests) -->
<batch:include class="java.lang.IllegalStateException" /> <!-- Yuk, usually/presumably a test SMTP server isn't yet connected -->
<batch:include class="org.springframework.mail.MailSendException" /> <!-- SMTP down... -->
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:decision id="limitDecision" decider="ourLimitDecider">
<batch:next on="CONTINUE" to="fetchEmailsStep" />
<batch:end on="COMPLETED" />
</batch:decision>
</batch:job>
我们的InfiniteThrottledExecutionDecider 基本上每次都返回new FlowExecutionStatus("CONTINUE"),以确保fetchEmailsStep 在流程结束时执行,并且作业永远不会完成——至少在我们准备好自己停止它之前不会完成.
我们不使用数据库,因此我们希望将一些内容保存在内存中,但不是对曾经运行过的所有内容的完整记录...
我们的配置有问题吗?还是我们的方法?
这是我们的日志文件中的一些内容,我们被告知步骤 #778 以及应该是唯一且唯一的 Job 实例。
23:58:18,782 - INFO (org.springframework.batch.core.job.SimpleStepHandler) - Duplicate step [fetchEmailsStep] detected in execution of job=[fetchEmailsJobBatch]. If either step fails, both will be executed again on restart.
23:59:52,257 - INFO (org.springframework.batch.core.job.SimpleStepHandler) - Executing step: [fetchEmailsStep]
23:59:52,257 - DEBUG (org.springframework.batch.core.step.AbstractStep) - Executing: id=778
23:59:52,259 - DEBUG (org.springframework.batch.repeat.support.RepeatTemplate) - Starting repeat context.
23:59:52,259 - DEBUG (org.springframework.batch.repeat.support.RepeatTemplate) - Repeat operation about to start at count=1
23:59:52,259 - DEBUG (org.springframework.batch.core.scope.context.StepContextRepeatCallback) - Preparing chunk execution for StepContext: org.springframework.batch.core.scope.context.StepContext@1be1ee
23:59:52,259 - DEBUG (org.springframework.batch.core.scope.context.StepContextRepeatCallback) - Chunk execution starting: queue size=0
23:59:52,259 - DEBUG (org.springframework.batch.repeat.support.RepeatTemplate) - Starting repeat context.
23:59:52,259 - DEBUG (org.springframework.batch.repeat.support.RepeatTemplate) - Repeat operation about to start at count=1
... 5 second JMS timeout ...
23:59:57,716 - DEBUG (org.springframework.batch.repeat.support.RepeatTemplate) - Repeat is complete according to policy and result value.
23:59:57,716 - DEBUG (org.springframework.batch.core.step.item.ChunkOrientedTasklet) - Inputs not busy, ended: true
23:59:57,716 - DEBUG (org.springframework.batch.core.step.tasklet.TaskletStep) - Applying contribution: [StepContribution: read=0, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
23:59:57,719 - DEBUG (org.springframework.batch.core.step.tasklet.TaskletStep) - Saving step execution before commit: StepExecution: id=778, version=1, name=fetchEmailsStep, status=STARTED, exitStatus=EXECUTING, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
23:59:57,721 - DEBUG (org.springframework.batch.repeat.support.RepeatTemplate) - Repeat is complete according to policy and result value.
23:59:57,721 - DEBUG (org.springframework.batch.core.step.AbstractStep) - Step execution success: id=778
23:59:57,722 - DEBUG (org.springframework.batch.core.step.AbstractStep) - Step execution complete: StepExecution: id=778, version=3, name=fetchEmailsStep, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0
23:59:57,723 - DEBUG (org.springframework.batch.core.job.flow.support.SimpleFlow) - Completed state=fetchEmailsJobBatch.fetchEmailsStep with status=COMPLETED
23:59:57,723 - DEBUG (org.springframework.batch.core.job.flow.support.SimpleFlow) - Handling state=fetchEmailsJobBatch.limitDecision100
23:59:57,723 - DEBUG (org.springframework.batch.core.job.flow.support.SimpleFlow) - Completed state=fetchEmailsJobBatch.limitDecision100 with status=CONTINUE
23:59:57,723 - DEBUG (org.springframework.batch.core.job.flow.support.SimpleFlow) - Handling state=fetchEmailsJobBatch.fetchEmailsStep
问题是,堆转储显示 748 JobExecutions 和 581,911 StepExecutions。他们都是从哪里来的?
【问题讨论】:
标签: java spring jms spring-batch