Flow not exiting like expected - spring-integration

new to SI. I have this SI Flow below. I would expect the Java program to exit when done, after the outbound channel, but it continues waiting, and I'm confused as to why. Thanks in advance!
<int-file:inbound-channel-adapter id="filesIn"
directory="file:${com.cld.creditexpenses.inputdirectory}"
filename-pattern="*.313" auto-startup="true">
<int:poller id="poller" fixed-delay="10000"/>
</int-file:inbound-channel-adapter>
<int:service-activator input-channel="filesIn" ref="delimitedFileProcessor"
method="processFile" output-channel="outChannel" />
<int:outbound-channel-adapter id="outChannel" ref="fileArchiver" method="archiveFile" >
</int:outbound-channel-adapter>

You have to close the application context when processing is complete, or replace the default taskScheduler with one that uses daemon threads - see the documentation.
But doing that means the program may exit before processing is complete - so you need to keep it running some other way.

Related

Issues in polling a file using Spring Integration

My req. is to poll a directory for a specified time interval say 10 mins. If a file of a particular extension say *.xml is found in the directory then the it just consumes (i.e. picks and deletes) the file and prints the name else after the specified time (say 10 mins.) interval it sends out a mail that the file has not been picked (i.e. consumed) or the file has not come.
There are 2 options either I do it through Spring integration OR WatchService of Core Java. Following is the code in Spring Integration which I have written till now:
<int:channel id="fileChannel" />
<int:channel id="processedFileChannel" />
<context:property-placeholder location="localProps.properties" />
<int:poller default="true" fixed-rate="10000" id="poller"></int:poller>
<int-file:inbound-channel-adapter
directory="file:${inbound.folder}" channel="fileChannel"
filename-pattern="*.xml" />
<int:service-activator input-channel="fileChannel"
ref="fileHandlerService" method="processFile" output-channel="processedFileChannel"/>
<bean id="fileHandlerService" class="com.practice.cmrs.springintegration.Poll" />
The above code is successfully polling the folder for a particular file pattern. Now I have 2 things to do:
1) Stop polling after a particular time interval (configurable) say 10 mins.
2) Check whether a file with a particular extension is there in the folder ... if the file is there (it consumes and then deletes) else it sends an email to a group of people (email part is done.)
Please help me in the above 2 points.
You can use a Smart Poller to do things like that.
You can adjust the poller and/or take different actions if/when the poll results in a message.
Version 4.2 introduced the AbstractMessageSourceAdvice. Any Advice objects in the advice-chain that subclass this class, are applied to just the receive operation. Such classes implement the following methods:
beforeReceive(MessageSource<?> source)
This method is called before the MessageSource.receive() method. It enables you to examine and or reconfigure the source at this time. Returning false cancels this poll (similar to the PollSkipAdvice mentioned above).
Message<?> afterReceive(Message<?> result, MessageSource<?> source)
This method is called after the receive() method; again, you can reconfigure the source, or take any action perhaps depending on the result (which can be null if there was no message created by the source). You can even return a different message!

Spring Integration File Polling. If moving the file does a AcceptOnceFileListFilter need to be used?

I'm writing a file polling implementation and am trying to determine if I need to use a AcceptOnceFileListFilter.
The first step the FileProcessor will perform is to move the file to another directory.
Does the poller "batchFilePoller" use multiple threads when polling? Can a race condition occur where a file will be read by multiple threads? In this case I assume I need to use the AcceptOnceFileListFilter.
However if the poller is only using one thread from the pool.
Then if the file is moved before the next poll time and it succeeds I assume there is no posability of the file been processed twice?
<int-file:inbound-channel-adapter id="batchFileInAdapter" directory="/somefolder" auto-create-directory="true" auto-startup="false" channel="batchFileInChannel" >
<int:poller id="batchFilePoller" fixed-rate="6000" task-executor="batchTaskExecutor" max-messages-per-poll="1" error-channel="batchPollingErrorChannel" />
</int-file:inbound-channel-adapter>
<int:channel id="batchFileInChannel"/>
<int:service-activator input-channel="batchFileInChannel" >
<bean class="com.foo.FileProcessor" />
</int:service-activator>
<task:executor id="batchTaskExecutor" pool-size="5" queue-capacity="20"/>
The <int-file:inbound-channel-adapter> has prevent-duplicates option which is true by default and it is your case since you don't provide any other options which prevent prevent-duplicates to be true.
And yes: any polling adapter is multi-threaded, if you use fixed-rate. In this case the new polling task can be run before a finish of previous one.
Even if it will be a single-threaded (using fixed-delay), the AcceptOnceFileListFilter must be there, because a new polling task doesn't know if file has been processed or not. And it reads the same file again.
AcceptOnceFileListFilter is exactly for those cases when you don't like to read the same file one more time. You can overcome that with <int:transactional synchronization-factory=""/> for the <poller> of the <int-file:inbound-channel-adapter>:
<int:transaction-synchronization-factory id="txSyncFactory">
<int:after-commit expression="payload.delete()"/>
</int:transaction-synchronization-factory>
and PseudoTransactionManager.
More info you can find in the Spring Integration Reference Manual.

Poller stops working after 1 cycle

At the beginning of my flow I have a file inbound adapter which reads a directory periodically:
<int-file:inbound-channel-adapter id="filteredFiles"
directory="${controller.cycle.lists.input.dir}"
channel="semaphoreChannel" filename-pattern="*.xml">
<int:poller fixed-delay="3000"/>**
</int-file:inbound-channel-adapter>
When the SI workflow ends it never happens again. It seems the poller is dead and stops working.
There aren't any error messages in the log nor any warnings.
Channel configuration:
<int:channel id="semaphoreChannel" datatype="java.io.File"/>
Second configuration:
<int-file:inbound-channel-adapter id="filteredFiles"
directory="${controller.cycle.lists.input.dir}"
channel="semaphoreChannel" filename-pattern="*.xml">
<int:poller cron="0 * * * * *" />
</int-file:inbound-channel-adapter>
It does not make sense.
Since you use default settings for other <poller> options, you end up with:
public static final int MAX_MESSAGES_UNBOUNDED = Integer.MIN_VALUE;
private volatile long maxMessagesPerPoll = MAX_MESSAGES_UNBOUNDED;
That means the FileReadingMessageSource reads all files by provided pattern during the single poller cycle.
The poller doesn't stop to work, but there is nothing more in the directory to read.
Change to this max-messages-per-poll="1" and let us know how it is.
From other side you can switch on DEBUG logging level for the org.springframework.integration.endpoint.SourcePollingChannelAdapter and there will be a message in logs:
Received no Message during the poll, returning 'false'

Advice for Queue Channel send timeout

Is there a way to specify an advice, like RequestHandlerAdvice, for QueueChannel's doSend method.
I have a Filter sending a data to a queue channel (with send-timeout of 0). When the queue is full, this throws an exception. I would want to trap this exception instead of throwing it to the sender.
<int:filter id="filterA" input-channel="channelA" output-channel="channelB"
method="fltrBsdOnCondtn" ref="fltr" send-timeout="0" />
<int:channel id="channelB">
<int:queue capacity="5000" />
</int:channel>
Thanks in advance!
Regards,
Satheesh
You could use a custom advice on the queue channel, using normal Spring AOP configuration.
But, it's probably easier to just put an error-channel on whatever is starting the flow (e.g. a <gateway/>) and handle the error there.

Spring Integration - asynchronous JMS

I'm struggling a lot with following scenario:
WS |============| JMS ~~~ async future processing ~~~
The idea is following. Incoming WS request is stored to JMS and once it is stored (correctly sent) the WS client is responded with OK.
I'm able to achieve that via spring jmsTemplate, something like
<chain>
<service-activator>
... calling jmsTemplate send ...
</service-activator>
<OK response>
</chain>
I don't want to use the jmsTemplate, however if I use the jms:outbound-channel-adapter, no reply message is generated and it gets stuck. Other constructions leads to synchronous processing, means that the WS response was delayed until the JMS request is fully processed.
I believe there is an easy solution, but I was't able to find it for hours. Thanks!
EDIT:
Suggested solution works, thanks a lot! Friend of mine recommended me another one - using wire-tap, it looks nice IMHO.
<int:gateway service-interface="MyService" default-request-channel="in"/>
<channel id="in">
<interceptors>
<wire-tap channel="inJms"/>
</interceptors>
</channel>
<channel id="inJms"/>
<transformer expression="'OK'" input-channel="in" order="1"/>
<jms:outbound-channel-adapter channel="inJms" destination="requestQueue"/>
The <publish-subscribe-channel/> helps you:
<publish-subscribe-channel id="storeMessageChannel"/>
<int-ws:inbound-gateway request-channel="storeMessageChannel"/>
<int-jms:outbound-channel-adapter channel="storeMessageChannel"/>
<int:transformer input-channel="storeMessageChannel" expression="'OK'"/>
Well, in this case the message from WS will be sent to the storeMessageChannel with two sequential subscribers: 1. JMS - to place message to the queue; 2. Simple transformer - to return to the WS response 'OK'.
Transformer will apply the message only after jms outbound adapter has done its work.

Resources