【问题标题】:Spring Batch Integration JobLaunchRequest from Controller来自控制器的 Spring Batch Integration JobLaunchRequest
【发布时间】:2015-08-01 18:00:20
【问题描述】:

我有一个控制器,我想从中运行 Spring Batch 作业。 FilePoller 充当按计划运行的轮询器,但我们也希望手动运行它。 FilePoller 正在执行 cron 计划,而 JobLaunchRequest 以这种方式工作。但是当我们使用从控制器调用的 JobLaunchRequest 时,什么都没有发生——Spring Batch 作业没有启动。这是控制器:

@Controller
public class PollerController {

@Autowired
FilePoller FilePoller;

@Autowired
private ApplicationContext appContext;

@RequestMapping(value = "ui/manualPoll.action", method = RequestMethod.GET)
public void manualPollRequest() {

    Message<File> message = filePoller.fileMessageSource().receive();

    filePoller.setFileParameterName(message.getPayload().getName());        
    filePoller.setJob((Job)appContext.getBean("myJob"));

    filePoller.toRequest(message);
}

消息有效负载具有文件名,我让 Spring Batch 作业从应用程序上下文运行。我已经调试并逐步执行代码,并确保消息有效负载中的文件名不为空,并且 Spring Batch 作业也不为空。在 FilePoller 类中,我有这个:

@Configuration
@PropertySource("classpath:my.properties") 
@EnableIntegration
@IntegrationComponentScan
public class FilePoller {

private Job job;

private String fileParameterName;

@Autowired
MyProperty myProperty;

public void setFileParameterName(String fileParameterName) {
    this.fileParameterName = fileParameterName;
}

public void setJob(Job job) {
    this.job = job;
}

@Bean
@InboundChannelAdapter(value = "inboundFileChannel", poller = @Poller(cron="${my/POLLER}"))
public MessageSource<File> fileMessageSource() {
    FileReadingMessageSource source = initialSetUp();
    source.setDirectory(new File(myProperty.getProperty(MyConstants.WORKING_DIR)))
    return source;
}

private FileReadingMessageSource initialSetUp() {
    FileReadingMessageSource source = new FileReadingMessageSource();
    CompositeFileListFilter<File> compositeFileListFilter = new CompositeFileListFilter<File>(); 
    SimplePatternFileListFilter simplePatternFileListFilter = new SimplePatternFileListFilter("*.done");
    AcceptOnceFileListFilter<File> acceptOnceFileListFilter = new AcceptOnceFileListFilter<File>();

    compositeFileListFilter.addFilter(simplePatternFileListFilter);
    compositeFileListFilter.addFilter(acceptOnceFileListFilter);

    source.setFilter(compositeFileListFilter);
    return source;
}

@Transformer
public JobLaunchRequest toRequest(Message<File> message) {
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addString(fileParameterName, message.getPayload().getAbsolutePath());
    return new JobLaunchRequest(job, jobParametersBuilder.toJobParameters());
}

JobLaunchRequest 似乎没有做任何事情。我从来没有进入我的第一步,在 XML 配置中显示:

<int:annotation-config />           
<int:channel id="inboundFileChannel" />
<int:channel id="outboundJobRequestChannel" />
<int:channel id="jobLaunchReplyChannel" />  

<int:transformer input-channel="inboundFileChannel"
    output-channel="outboundJobRequestChannel">
    <bean
        class="org.my.poller.FilePoller">
        <property name="job" ref="myJob" />
        <property name="fileParameterName" value="input.file.name" />
    </bean>
</int:transformer>

<batch-int:job-launching-gateway request-channel="outboundJobRequestChannel" reply-channel="jobLaunchReplyChannel" />
<int:logging-channel-adapter channel="jobLaunchReplyChannel" />



<job id="myJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="Step1" next="Step2">
            <tasklet ref="checkifFileinLogTbl"/>

更新 谢谢,加里。现在可以了。如果其他人有兴趣:

@Autowired
MessageChannel outboundJobRequestChannel;

@Autowired
MessagingTemplate template;

@RequestMapping(value = "ui/manualPoll.action", method = RequestMethod.GET)
public void manualPollRequest() {

Message<File> message = filePoller.fileMessageSource().receive();

    //if message !=null, there is a file present on inboundFileChannel
    if(message !=null){
        filePoller.setFileParameterName("input.file.name");
        filePoller.setJob((Job) appContext.getBean("myJob"));

        template.convertAndSend(outboundJobRequestChannel, filePoller.toRequest(message));
    }

在我添加的 XML 中:

<bean class="org.springframework.integration.core.MessagingTemplate" />

【问题讨论】:

    标签: spring spring-batch spring-integration


    【解决方案1】:
    filePoller.toRequest(message);
    

    只构建请求对象。

    您需要将其发送至outboundJobRequestChannel

    使用MessagingTemplate (convertSendAndReceive()) 或MessagingGateway 来执行此操作。

    【讨论】:

      猜你喜欢
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2019-05-09
      • 1970-01-01
      • 2018-09-19
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多