gpt4 book ai didi

error-handling - 错误处理 : Spring Integration

转载 作者:行者123 更新时间:2023-12-03 08:56:23 25 4
gpt4 key购买 nike

我对 Spring Integration 完全陌生。以下是我的要求:

我必须轮询文件的给定位置(例如间隔 5 秒的目录)(具有给定模式 - 由正则表达式定义。)。如果找到文件,我必须对文件执行某些操作(比如记录消息“找到文件”,将文件保存到另一个位置等),如果找不到文件,则程序需要记录一条消息说 - “轮询间隔 5 秒......找不到文件”。该程序还需要轮询 9 小时。之后它应该停止轮询,即程序应该退出。

我想处理各种错误情况,例如:

1)文件夹不存在:如果正在轮询文件的文件夹(目录)不存在会怎样?我的最新代码如下:

<int-event:inbound-channel-adapter
channel="contextStartedEventChannelChannel" event- types="org.springframework.context.event.ContextRefreshedEvent"
payload-expression="new java.io.File('${myapp.incomingFile.path}').exists()" />

<int:router input-channel="contextStartedEventChannelChannel"
expression="payload">
<int:mapping value="true" channel="trueChannel" />
<int:mapping value="false" channel="falseChannel" />
</int:router>

<int:outbound-channel-adapter id="trueChannel"
expression="@'incomingFiles.adapter'.start()" />

<int:outbound-channel-adapter id="falseChannel"
ref="errorTracker" method="processError" />

入站 channel 适配器的代码:
<int-file:inbound-channel-adapter id="incomingFiles"
directory="file:${myapp.incomingFile.path}" prevent-duplicates="true"
filename-regex="${ETD.filenameRegex}" auto-create-directory="false"
auto-startup="false">

问题:

1)但是现在发生的事情是,当我有目的地删除轮询目录时,正如预期的那样,我们得到了一个异常,但它作为控制台上的堆栈跟踪出现并且它没有进入错误 channel 。

2)目前我们只处理一种错误情况,即如果轮询目录不存在。我还想实现其他错误场景,例如网络故障等,并且只想在记录器上显示一条消息(即通过错误 channel )并优雅地退出程序。

请提出问题所在。

以下是我得到的异常(exception):

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'incomingFiles.adapter': Cannot resolve reference to bean 'incomingFiles.adapter.source' while setting bean property 'source'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'incomingFiles.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [C:\TEMP\incomingFile] does not exist. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:681) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at com.jefferies.fileutility.App.main(App.java:31) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'incomingFiles.adapter.source': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Source directory [C:\TEMP\incomingFile] does not exist. at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:151) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1514) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:252) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:320) ... 15 more Caused by: java.lang.IllegalArgumentException: Source directory [C:\TEMP\incomingFile] does not exist. at org.springframework.util.Assert.isTrue(Assert.java:65) at org.springframework.integration.file.FileReadingMessageSource.onInit(FileReadingMessageSource.java:237) at org.springframework.integration.context.IntegrationObjectSupport.afterPropertiesSet(IntegrationObjectSupport.java:133) at org.springframework.integration.file.config.FileReadingMessageSourceFactoryBean.initSource(FileReadingMessageSourceFactoryBean.java:170) at org.springframework.integration.file.config.FileReadingMessageSourceFactoryBean.getObject(FileReadingMessageSourceFactoryBean.java:111) at org.springframework.integration.file.config.FileReadingMessageSourceFactoryBean.getObject(FileReadingMessageSourceFactoryBean.java:40) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144) ... 20 more

最佳答案

某些错误被认为是硬错误(在 bean 初始化期间检测到)并阻止初始化上下文。在这种情况下,为防止出现此错误,您需要设置自动创建目录。

需要的条件在onInit() ...

    if (!this.directory.exists() && this.autoCreateDirectory) {
this.directory.mkdirs();
}
Assert.isTrue(this.directory.exists(),
"Source directory [" + directory + "] does not exist.");
Assert.isTrue(this.directory.isDirectory(),
"Source path [" + this.directory + "] does not point to a directory.");
Assert.isTrue(this.directory.canRead(),
"Source directory [" + this.directory + "] is not readable.");

其他,运行时,错误确实会转到轮询器的配置 error-channel (或 errorChannel 默认)。

关于error-handling - 错误处理 : Spring Integration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23939301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com