Search code examples
xmlspringcronspring-integration

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). 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.


Solution

  • 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?