Spring集成如何在XML中使用轮询Cron表达式适配器

huangapple go评论66阅读模式
英文:

Spring integration how to poll cron expression adapter in xml

问题

I'm trying to migrate cron scheduler that takes in 2 cron expressions (Expression and Timezone).
我正在尝试迁移一个需要两个cron表达式(Expression和Timezone)的定时任务调度器。

I checked the spring integration docs and there seems to be a constructor that takes in exactly what I need https://docs.spring.io/spring-integration/api/org/springframework/integration/dsl/PollerFactory.html#cron(java.lang.String,java.util.TimeZone) PollerSpec cron(String cronExpression, TimeZone timeZone).
我查看了Spring Integration文档,似乎有一个构造函数正好符合我的需求:https://docs.spring.io/spring-integration/api/org/springframework/integration/dsl/PollerFactory.html#cron(java.lang.String,java.util.TimeZone) PollerSpec cron(String cronExpression, TimeZone timeZone)。

But, I'm not sure how to implement it in xml.
但是,我不确定如何在XML中实现它。

<int:inbound-channel-adapter channel="cronInput">
<int:integration:PollerSpec cronExpression=${bless.cron.expression}" timeZone= "${bless.cron.timezone}"/>
</int:inbound-channel-adapter>

<int:channel id="cronInput" />

Here's how I implemented it and my understanding is it's taking the message payload from executing the cron expression and putting it in the pipe/channel cronInput, so If I create another endpoint like activator, I can create an input channel and use the payload.
这是我实现的方式,我的理解是它从执行cron表达式中获取消息有效负载,并将其放入管道/通道cronInput中,因此,如果我创建另一个类似激活器的端点,我可以创建一个输入通道并使用有效负载。

英文:

I'm trying to migrate cron scheduler that takes in 2 cron expressions (Expression and Timezone).
I checked the spring integration docs and there seems to be a constructor that takes in exactly what I need https://docs.spring.io/spring-integration/api/org/springframework/integration/dsl/PollerFactory.html#cron(java.lang.String,java.util.TimeZone) PollerSpec cron(String cronExpression, TimeZone timeZone). But, I'm not sure how to implement it in xml.

<int:inbound-channel-adapter channel="cronInput">
<int:integration:PollerSpec cronExpression=${bless.cron.expression}" timeZone= "${bless.cron.timezone}"/>
</int:inbound-channel-adapter>

<int:channel id="cronInput" />

Here's how I implemented it and my understanding is it's taking the message payload from executing the cron expression and putting it in the pipe/channel cronInput, so If I create another endpoint like activator, I can create an input channel and use the payload.

答案1

得分: 1

The PollerSpec 是用于 Java DSL 的。它与 XML 无关。确实没有 PollerSpec 自定义 XML 标签。

<int:inbound-channel-adapter> 包含一个 <int:poller> 子元素:

<xsd:element name="poller" type="basePollerType"/>

poller 自定义标签带有以下属性:

<xsd:attribute name="cron" type="xsd:string">
<xsd:annotation>
<xsd:documentation>Cron 触发器。</xsd:documentation>
</xsd:annotation>
</xsd:attribute>

然而,没有 TimeZone 属性来涵盖您的其他用例。

不过,<poller> 可以配置一个 trigger 属性:

<xsd:attribute name="trigger" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.scheduling.Trigger"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>

因此,对于您的用例,您可以提供额外的 CronTrigger 并在提到的引用中使用其 Bean 名称:

<int:inbound-channel-adapter channel="cronInput">
<int:poller trigget="cronTrigger"/>
</int:inbound-channel-adapter>

<beans:bean id="cronTrigger" class="org.springframework.scheduling.support.CronTrigger">
<beans:constructor-arg value="${bless.cron.expression}"/>
<beans:constructor-arg value="${bless.cron.timezone}"/>
</beans:bean>

从这里,我提出一个问题:为什么您要尝试使用 XML 来实现这些内容呢?为什么不直接使用 Java DSL,或者至少使用注解配置呢?

英文:

The PollerSpec is for Java DSL. It has nothing to do with an XML. And there is indeed no PollerSpec custom XML tag.

The <int:inbound-channel-adapter> comes with a <int:poller> sub-element:

<xsd:element name="poller" type="basePollerType"/>

This poller custom tag comes with this attribute:

	<xsd:attribute name="cron" type="xsd:string">
		<xsd:annotation>
			<xsd:documentation>Cron trigger.</xsd:documentation>
		</xsd:annotation>
	</xsd:attribute>

However there is no TimeZone attribute to cover your other use-case.

Nevertheless the <poller> can be configured with a trigger attribute:

	<xsd:attribute name="trigger" type="xsd:string">
		<xsd:annotation>
			<xsd:appinfo>
				<tool:annotation kind="ref">
					<tool:expected-type type="org.springframework.scheduling.Trigger"/>
				</tool:annotation>
			</xsd:appinfo>
		</xsd:annotation>
	</xsd:attribute>

So, for your use case you can provide an extra CronTrigger and use its bean name in the mentioned reference:

<int:inbound-channel-adapter channel="cronInput">
    <int:poller trigget="cronTrigger"/>
</int:inbound-channel-adapter>

<beans:bean id="cronTrigger" class="org.springframework.scheduling.support.CronTrigger">
	<beans:constructor-arg value="${bless.cron.expression}"/>
	<beans:constructor-arg value="${bless.cron.timezone}"/>
</beans:bean>

From here the question from me: why do you try to implement this stuff with an XML?
Why just don't go ahead and just deal with Java DSL or at least an Annotations configuration?

huangapple
  • 本文由 发表于 2023年5月8日 02:02:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定