spring-boot - Spring Boot Quartz,集成入站通道适配器和触发器绑定
问题描述
我们有使用 XML 配置的 Quartz Job + File Integration 适配器并且工作正常,但是自从尝试转移到 Spring Boot 并将这些配置转移到注释
下面是我正在寻找等效注释和绑定的 XML 配置
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="schedulerContextAsMap">
<map>
<entry key="inboundadapterendpoint"><ref bean="incomingfiles" /></entry>
<entry key="inboundendpointtrigger"><ref bean="incomingfiles-trigger"/></entry>
</map>
</property>
</bean>
<bean id="inboundendpointtrigger" class="abc.xyz.Trigger" />
<file:inbound-channel-adapter id="incomingfiles" channel="xyz" directory="" scanner="recursiveScanner" auto-startup="false" prevent-duplicates="false">
<integration:poller task-executor="fileChannelTaskExecutor" trigger="incomingfiles-trigger" max-messages-per-poll="1">
</integration:poller>
</file:inbound-channel-adapter>
我们如何获得使用下面的注释创建的入站适配器 Bean 和轮询触发器在 Spring Boot 石英配置中的调度程序工厂创建期间注入
@Bean
@InboundChannelAdapter(poller = @Poller(trigger="customTrigger")
public MessageSource<File> fileReadingMessageSource() {
}
在此先感谢您提供有关相同内容的任何帮助或建议
Artem Bilan,非常感谢您的回复。
跟进问题帖子,尝试响应中提供的代码
@Configuration
public class QuartzConfig {
@Autowired
private CustomTrigger inboundEndPointTrigger;
@Autowired
private AbstractEndpoint incomingFiles;
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
System.out.println("incomingFiles value is " + incomingFiles);
}
}
@Bean(name = "incomingFiles")
@InboundChannelAdapter(autoStartup = "false", value = "xyz", poller = @Poller(trigger = "inboundEndPointTrigger", maxMessagesPerPoll = "2", errorChannel = "abc"))
public MessageSource<File> fileReadingMessageSource() {
}
上面的输出是 errorLogger 而不是 Inbound Channel Adapter 的参考。
incomingFiles 值为 bean '_org.springframework.integration.errorLogger'
如何将名称为incomingFiles 的确切入站适配器绑定到调度程序工厂?
尝试使用@EndPointId 后更新...
@Bean
@EndPointId("incomingFiles")
@InboundChannelAdapter(autoStartup = "false", value = "xyz", poller = @Poller(trigger = "inboundEndPointTrigger", maxMessagesPerPoll = "2", errorChannel = "abc"))
public MessageSource<File> fileReadingMessageSource() {
}
System.out.println("incomingFiles value is " + incomingFiles); print in QuartzConfig class above is still giving a reference to Logger object
incomingFiles value is bean '_org.springframework.integration.errorLogger'
在下面的 SO( Spring Cloud Stream + Quartz)中找到了关于如何为入站通道适配器创建 bean的响应。
Since the AbstractEndPoint is returning the logger reference instead of InboundChannelAdapter ,
is it ok to get the Inbound Adapter channel bean reference via application context in scheduler factory ? are there any issues with this ?
try {
ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
AbstractEndpoint abstractEndPoint = (AbstractEndpoint) applicationContext
.getBean("fileConfig.fileReadingMessageSource.inboundChannelAdapter");
} catch(SchedulerException ex) {
}
fileConfig is the Spring File integration configuration class name where InboundChannelAdapter is defined..
解决方案
我不确定您为什么关闭了旧问题 - 您可以正确编辑它,我们会有指向其他问题的链接。但无论如何,答案是这样的:
@Bean
任何 XML bean 定义都可以使用方法定义在 Java 和注释配置中声明。
所以,<bean id="inboundendpointtrigger" class="abc.xyz.Trigger" />
在 Java 中应该是这样的:
@Bean
Trigger customTrigger() {
return new abc.xyz.Trigger();
}
您已经使用它的 bean 名称作为@Poller
.
您对它执行相同的操作并在该方法中SchedulerFactoryBean
对其进行配置。您已经有一个bean 供参考,并且可以基于访问端点,您需要注入一个方法。有点像这样:setSchedulerContextAsMap()
@Bean
customTrigger()
SourcePollingChannelAdapter
@InboundChannelAdapter
@Bean
@Bean
SchedulerFactoryBean schedulerFactory(Trigger trigger, SourcePollingChannelAdapter endpoint) {
}
不要忘记@InboundChannelAdapter(autoStartup = "false")
在其他 SO 问题中使用 Gary 推荐的方法。
推荐阅读
- flutter - 如何将数据从一个小部件传递到其兄弟小部件?
- java - EJB - 无法注册 EJB 接收器以连接到主机
- php - 如何修复“无法将图像数据写入路径”Laravel 6.X
- asp.net-core - 为什么“ASPNETCORE_ENVIRONMENT:开发”不起作用?
- c# - 将服务注入 FileProvider
- mongodb - MongoDb 中的 .find() 打印同一文档的多次出现
- javascript - 递归调用的异步函数将未定义返回给被调用函数
- javascript - Flask-SocketIO 广播到客户端
- python - 使用 Cython 加速 Spacy
- angular - 离子模态:未找到组件工厂