[JAVA] Tips for setting the initial delay for Poller in XML

Operation check environment

Define Poller in XML

Spring Integration has a mechanism called Poller that executes regularly, but when defining it in XML (<int: poller> ), the execution interval such as fixed-delay or fixed-rate is set. There are attributes that can be set, but there is no attribute for initial value delay.

The following is a description of the Poller in the 4.3.x xsd file.

xml:spring-integration-4.3.xsd


<xsd:element name="poller">
	<xsd:annotation>
		<xsd:documentation>
			Defines a top-level poller - 'org.springframework.integration.scheduling.PollerMetadata'.
		</xsd:documentation>
	</xsd:annotation>
	<xsd:complexType>
		<xsd:complexContent>
			<xsd:extension base="basePollerType">
				<xsd:attribute name="id" type="xsd:string" />
				<xsd:attribute name="default" type="xsd:boolean" default="false" />
			</xsd:extension>
		</xsd:complexContent>
	</xsd:complexType>
</xsd:element>

<xsd:complexType name="basePollerType">
	<xsd:annotation>
		<xsd:documentation>
			Defines the configuration metadata for a poller.
		</xsd:documentation>
	</xsd:annotation>
	<xsd:sequence>
		<!--Abbreviation-->
	</xsd:sequence>
	<xsd:attribute name="fixed-delay" type="xsd:string">
		<xsd:annotation>
			<xsd:documentation>Fixed delay trigger (in milliseconds).</xsd:documentation>
		</xsd:annotation>
	</xsd:attribute>
	<xsd:attribute name="ref" type="xsd:string" use="optional">
		<xsd:annotation>
			<xsd:documentation>
				Allows this poller to reference another instance of a top-level poller.
				[IMPORTANT] - This attribute is only allowed on inner poller definitions.
				Defining this attribute on a top-level poller definition will result in a configuration exception.
			</xsd:documentation>
		</xsd:annotation>
	</xsd:attribute>
	<xsd:attribute name="fixed-rate" type="xsd:string">
		<xsd:annotation>
			<xsd:documentation>Fixed rate trigger (in milliseconds).</xsd:documentation>
		</xsd:annotation>
	</xsd:attribute>
	<!--Abbreviation-->
</xsd:complexType>

Therefore, for example, one minute after starting the application, it is not possible to meet the specification that you want to periodically send a monitoring message to see if there is a problem with the connection.

Solved with Periodic Trigger

So, as a result of research, there was a person who asked a similar question on the forum in 2011.

According to the answer, you can set the initial delay for the periodically running trigger class called PeriodicTrigger and set it to the trigger attribute of Poller. So, when I defined PeriodicTrigger as a bean and set it to the trigger attribute of Poller as shown below, I confirmed that a monitoring message was thrown every 60 seconds 60 seconds after the application started. It's done.

<!--Adapter that throws surveillance messages-->
<int:inbound-channel-adapter id="monitoringAdapter"
    auto-startup="true" expression="'monitor'.bytes"
    channel="inboundMonitoringChannel">
    <int:poller trigger="monitoringTrigger" />
</int:inbound-channel-adapter>

<!--Interval to throw surveillance messages-->
<!--Fires every 60 seconds after an initial delay of 60 seconds-->
<bean id="monitoringTrigger" class="org.springframework.scheduling.support.PeriodicTrigger"
    c:period="60" c:timeUnit="SECONDS" p:initialDelay="60" />

in conclusion

The difference between is becoming vague: rolling_eyes :.

This sample code is available on GitHub (I changed the content a little: sweat :).

neriudon-TriggerSample-

Recommended Posts

Tips for setting the initial delay for Poller in XML
Tips for handling enums in thymeleaf
Tips for handling pseudo-elements in Selenium
must not return in the for statement
Tips for gRPC error handling in Ruby
ChatWork4j for using the ChatWork API in Java
Tips for using the Spotify app on Ubuntu
SourceMapPathOverrides setting memo in VSCode Debugger for Chrome
[Ruby] Setting values ​​and memorandum for the table
Tips for generating files for eclipse projects in Gradle