批处理作业失败后如何停止或暂停轮询?

时间:2018-05-19 19:00:27

标签: spring-integration spring-batch

我们使用spring-batch-integration来处理目录中的.json文件。有必要在发生故障后停止处理,找出问题(更改有问题或其他解决方案的文件),然后继续。当前配置在错误后继续轮询。怎么改呢?或者这种情况可能有不同的方法。

@Configuration
@IntegrationComponentScan
@EnableIntegration
public class IntegrationConfig {

private @Autowired Job job;

@Bean
@ServiceActivator(inputChannel = "jobChannel", 
   outputChannel = "errorChannel")
protected JobLaunchingMessageHandler launcher(JobLauncher jobLauncher) {
    return new JobLaunchingMessageHandler(jobLauncher);
}

@Bean
public MessageChannel fileInputChannel() {
    return new DirectChannel();
}

@Bean
@InboundChannelAdapter(value = "fileInputChannel",
                       poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> fileReadingMessageSource() {
    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setDirectory(new File("C:/Temp/myfiles/"));
    source.setFilter(new SimplePatternFileListFilter("*.json"));
    source.setScanEachPoll(true);
    source.setUseWatchService(true);
    return source;
}

@Transformer(inputChannel = "fileInputChannel",
             outputChannel = "jobChannel")
public JobLaunchRequest transform(File aFile) {
    String fileName = aFile.getAbsolutePath();
    JobParameters jobParameters =
    new JobParametersBuilder().addString("input.file.name", fileName)
            .addDate("dateTime", new Date()).toJobParameters();
    JobLaunchRequest request = new JobLaunchRequest(job, jobParameters);
    return request;
}
}

example was from this article

既没有outputChannel =“nullChannel”也没有outputChannel =“errorChannel”帮助

2 个答案:

答案 0 :(得分:0)

您需要停止入站通道适配器。

您可以自动装配由SourcePollingChannelAdapter注释注册的@InboundChannelAdapter

当您检测到故障时,请拨打适配器上的stop()

答案 1 :(得分:0)

我添加了

@Bean
@DependsOn("fileInputChannel")
@ServiceActivator(inputChannel = "errorChannel", 
  outputChannel = "nullChanel")
protected ErrorLogger errorLogger(JobLauncher jobLauncher) {
    return new ErrorLogger();
}

public class ErrorLogger {
private static final Logger logger = 
LoggerFactory.getLogger(ErrorLogger.class);

@Autowired
private SourcePollingChannelAdapter fileInputChannel;


@ServiceActivator
public void logError(Message<JobExecution> message) {
    JobExecution msgex=message.getPayload();
     if (msgex.getStatus() == BatchStatus.FAILED) {
         logger.error("Exception " + 
         msgex.getExitStatus().getExitDescription());
         fileInputChannel.stop();
     }
}
}

但是我在ErrorLogger

中遇到了自动装配错误
Unsatisfied dependency expressed through field 'fileInputChannel'; nested 
exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 
'org.springframework.integration.endpoint.SourcePollingChannelAdapter' 
available:

尽管 @DependsOn(“fileInputChannel”),似乎是初始化顺序问题,因为我可以在单独的控制器中自动将其自动保存而不会出现错误。

仅适用于

 @Autowired(required = false)
 private SourcePollingChannelAdapter fileInputChannel;