有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java camel是否对文件使用隐式读锁?

我正在使用Camel file组件使用一个文件,如下所示:

<from uri="file:myDir?noop=true&amp;filter=myFilter&amp;scheduler=quartz2&amp;scheduler.cron={{schedule}}/>

源位置为只读Camel File documentation表示默认情况下不会使用读锁

但是,在运行此代码时,我遇到了一个权限被拒绝的异常

Endpoint[file://myFile?filter=myFilter&idempotent=true&noop=true&scheduler=quartz2&scheduler.cron={{mySchedule}} cannot begin processing file: GenericFile[myDir\myFile] due to: Access is denied. Caused by: [java.io.IOException - Access is denied]

Stack Trace java.io.IOException: Permission denied at java.io.UnixFileSystem.createFileExclusively(Native Method)[:1.8.0_66] at java.io.File.createNewFile(File.java:1012)[:1.8.0_66] at org.apache.camel.util.FileUtil.createNewFile(FileUtil.java:587)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.component.file.strategy.MarkerFileExclusiveReadLockStrategy.acquireExclusiveReadLock(MarkerFileExclusiveReadLockStrategy.java:71)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.component.file.strategy.GenericFileProcessStrategySupport.begin(GenericFileProcessStrategySupport.java:49)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.component.file.strategy.GenericFileRenameProcessStrategy.begin(GenericFileRenameProcessStrategy.java:35)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:352)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:211)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:175)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101)[org.apache.camel:camel-core:2.15.1.redhat-621090 com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2] at org.apache.camel.pollconsumer.quartz2.QuartzScheduledPollConsumerJob.execute(QuartzScheduledPollConsumerJob.java:59)[org.apache.camel:camel-quartz2:2.15.1.redhat-621090] at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[org.quartz-scheduler:quartz:2.2.1] at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[org.quartz-scheduler:quartz:2.2.1]

从堆栈跟踪中可以看出,camel正在尝试创建一个readLock,并且由于缺乏权限而导致异常

所以,我的问题是

  • 为什么camel使用锁,即使读取锁的默认值为none
  • 若这是预期的行为,那个么如何从我并没有写权限的只读位置使用文件

更新

我试着按照其中一个答案中的建议将readLockMarkingFile设置为false,但这并没有解决问题。因此,我显式地将readLock设置为none。现在,它正在发挥作用。不知道为什么readLock不是文档中提到的默认值none


共 (3) 个答案

  1. # 1 楼答案

    我也遇到了这个问题,文档说明参数的默认值是none,但实际上使用了markerFile。要解决此问题,必须显式指定此参数readLock=none(camel版本2.20.2)

  2. # 2 楼答案

    虽然这并不能直接回答您的问题,但我使用了一个触发器文件(0字节),文件观察者会查找它。然后,您的路由将处理触发器所伴随的实际文件。触发器文件被移动到子目录/parent/。这样它就不会再加工了。尽管我们已经注意到,在一个分布式环境中,路由试图“提取”实际文件,其中有多台服务器运行路由

  3. # 3 楼答案

    readLock选项仅决定camel是否尝试获取文件的独占读锁。还有另一个名为“readLockMarkerFile”的选项,默认为true,将该选项设置为false,就可以了

    <from uri="file:myDir?noop=true&amp;filter=myFilter&amp;scheduler=quartz2&amp;scheduler.cron={{schedule}}&amp;readLockMarkerFile=false/>