简体   繁体   English

启动 Spring 批处理作业

[英]Launching Spring batch job

I have a problem where in I need to receive a series of messages from an MQ queue and write this to a file and initiate a spring batch job with the file as input.我有一个问题,我需要从 MQ 队列接收一系列消息并将其写入文件并以文件作为输入启动 spring 批处理作业。 Right now I'm thinking of launching the job with wired @Autowired JobLauncher jobLauncher and @Autowired Job job;现在我正在考虑使用有线@Autowired JobLauncher jobLauncher and @Autowired Job job; from the MDB itself.But I feel this is not a good approach as spring batch may create a series of threads and EJB as such doesnt support multi threading.来自 MDB 本身。但我觉得这不是一个好方法,因为 spring 批处理可能会创建一系列线程,而 EJB 不支持多线程。

Is there any other effective way to do this?有没有其他有效的方法来做到这一点? I dont want to use quartz scheduler or anything else since it adds complexity.我不想使用石英调度程序或其他任何东西,因为它增加了复杂性。 Is there any interface in spring batch itself which launches a job soon after a file comes in a directory? spring 批处理本身是否有任何接口在文件进入目录后立即启动作业? Any leads in doing this better would be appreciated.任何能更好地做到这一点的线索将不胜感激。

Thanks.谢谢。

  • I have a problem where in I need to receive a series of messages from an MQ queue and write this to a file and initiate a spring batch job with the file as input我有一个问题,我需要从 MQ 队列接收一系列消息并将其写入文件并以文件作为输入启动 spring 批处理作业

One way to do that would be engage a bit of Spring Integration, where you would have a file poller, that would poll for a new file:一种方法是使用一点 Spring 集成,您将有一个文件轮询器,它将轮询新文件:

<file:inbound-channel-adapter id="filePoller"
                              channel="filesAreComing" 
                              directory="file:${input.directory}"
                              filename-pattern="test*" />

Adapt a file message ( java.io.File ) to a file name ( String ), since that is what Spring Batch needs.file message ( java.io.File ) 修改为file name ( String ),因为这是 Spring Batch 需要的。 This can be done with a JobLauncher adapter, that is already available from Spring Batch Admin here :这可以使用 JobLauncher 适配器来完成,该适配器已从Spring Batch Admin 获得:

@ServiceActivator
public JobLaunchRequest adapt(File file) throws NoSuchJobException {

    JobParameters jobParameters = new JobParametersBuilder().addString(
            "input.file", file.getAbsolutePath()).toJobParameters();

    return new JobLaunchRequest(job, jobParameters);
}

wrap it to a JobLaunchRequest ( which is just a holder for a Job and JobParameters ) and send this request [as a message] to JobLaunchingMessageHandler :将其包装到JobLaunchRequest (它只是JobJobParameters的持有者)并将此请求 [作为消息] 发送到JobLaunchingMessageHandler

<service-activator input-channel="jobLauncher">
    <beans:bean class="org.springframework.batch.integration.launch.JobLaunchingMessageHandler">
        <beans:constructor-arg ref="jobLauncher" />
    </beans:bean>
</service-activator>

that would launch the job.那将启动这项工作。

"input.file" is a parameter that is bound at runtime ( hence #{...} ): “input.file”是在运行时绑定的参数(因此是 #{...} ):

<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <property name="resource" value="#{jobParameters[input.file]}" />
    ... line mapper and other props
</bean>

I'm not sure I understand why you have a message queue, a message-driven POJO/EJB, AND a batch job.我不确定我理解为什么你有一个消息队列、一个消息驱动的 POJO/EJB 和一个批处理作业。

One way to do it is to have the message driven POJO/EJB do the work.一种方法是让消息驱动的 POJO/EJB 完成这项工作。 It's already an asynch process.这已经是一个异步过程。 You can pool the message driven beans so there are sufficient workers to handle the load.您可以汇集消息驱动的 bean,以便有足够的工作人员来处理负载。 Why add complexity?为什么要增加复杂性?

If you'd rather not do that, forget the queue and use Spring Batch on its own.如果您不想这样做,请忘记队列并单独使用 Spring Batch。 I wouldn't do both.我不会两者都做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM