轮询目录时如何防止重复的Spring Integration Service激活 [英] How to prevent duplicate Spring Integration service activations when polling directory

查看:90
本文介绍了轮询目录时如何防止重复的Spring Integration Service激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Integration目录轮询器:

I have a Spring Integration directory poller:

<task:executor id="filePollingExecutor" pool-size="1" />
<int:channel id="inboundFilesChannel" datatype="java.io.File" />
<int-file:inbound-channel-adapter id="inboundFilesAdapter" 
      channel="inboundFilesChannel"
      directory="/my/files/queue"
      prevent-duplicates="true">
  <int:poller id="poller" fixed-delay="1000" 
              max-messages-per-poll="1" 
              task-executor="filePollingExecutor" />
</int-file:inbound-channel-adapter>

为了响应目录中出现的文件,我有一个服务激活器,该服务激活器调用服务上的方法:

In response to files appearing in the directory, I have a service activator which invokes a method on a service:

不幸的是,我发现文件到达时,该服务始终被调用两次.最初,我认为这是由于具有多个执行程序线程所致,但是您可能会在上面注意到,我试图通过将轮询程序与池大小为1的taskExecutor绑定来解决此问题.

Unfortunately I'm finding that the service is consistently being called twice when a file arrives. Originally I thought this was due to having multiple executor threads, but you may notice above, I attempted to resolve that by tying the poller to a taskExecutor with a pool size of 1.

我发现,我可以通过增加两次轮询之间的延迟来解决此问题.我认为关键是它比处理文件所需的时间更长.

What I have been finding is that I can workaround the issue by increasing the delay between polls. I think the key is that it's longer than the time it takes to process a file.

<int:poller id="poller" fixed-delay="10000" 
            max-messages-per-poll="100" 
            task-executor="filePollingExecutor" />

但是,那感觉就像是在跳动,而不是解决.

However, that feels like a kludge rather than a fix.

我是否缺少一些我应该用来防止重复的配置?

Am I missing some configuration that I should be using to prevent duplicates?

可能值得注意的是,我确实尝试过使用nio-locker,但是问题在于处理的一部分涉及发送带有附件的电子邮件.文件锁定阻止了此操作的完成,因为在锁定期间,文件不再可读.

It's possibly worth noting that I did try using a nio-locker, but the issue there is that part of the processing involves sending an email with the file attached to it. File locks prevented that from being done, as the file ceased to be readable for the duration of the lock.

推荐答案

此答案基于上述评论中加里·罗素(Gary Russell)的技巧.

This answer is based on the tip from Gary Russell in the comments above.

对文件进行双重处理的原因是,根目录和Web配置都初始化了文件系统侦听器,因此每个文件都要处理两次.

The reason for double-processing of files is that the root and web configs were both initialising file system listeners, and therefore processing each file twice.

我避免在多个上下文中使用文件侦听器的方法如下.

My approach to avoiding having the file listeners in multiple contexts was as follows.

首先定义一个Web配置,该配置仅选择"web"程序包下的类.

First define a web config which only picks up classes under the "web" package.

@Configuration
@ComponentScan(basePackages = { "com.myapp.web" })
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

创建单独的根配置,这些根配置仅加载不在"web"程序包中的bean.即

Create separate root configs which only load beans which are not in the "web" package. i.e.

@Configuration
@ComponentScan(basePackages = { "com.myapp.services" })
public class ServicesConfig {
}

需要花费一些时间才能完成的配置中的另一个因素是,我的servlet过滤器和Web安全配置需要位于"root"上下文中,而不是在Web上下文中.

An additional factor in the configuration that took a little while to work out, was that my servlet filters and web security config needed to be in the 'root' context rather than the web context.

这篇关于轮询目录时如何防止重复的Spring Integration Service激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆