lloydh
lloydh

Reputation: 395

how to poll a directory for files and send the files to a message queue using spring integration

I am trying to develop an application using which I want to be able to send zip files over to a messaging queue that's running on a separate server. I have successfully implemented the messaging bit using ActiveMQ and the queue is up and listening for the messages on the server side. I have a similar application which sends json files as messages to a queue and it's working fine. I am trying to write my application based on how the former one was implemented.

Below is part of my spring integration configuration:

<int-file:inbound-channel-adapter id="filesIn" directory="${harvest.directory}" filename-pattern="*.zip">
    <int:poller id="poller" fixed-rate="${harvest.pollRate}" max-messages-per-poll="${harvest.queueCapacity}" />
</int-file:inbound-channel-adapter>

<int:transformer id="copyFiles" input-channel="filesIn"
    output-channel="routingChannel" ref="transformationHandler" method="handleFile"/>

<int-jms:outbound-channel-adapter id="jmsOut" destination="requestQueue" channel="filesIn"/>
.
.
.
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${activemq.url}" />
</bean>
<bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="myQueue"/>
</bean> 

As you can see there is a transformer. But in my case, I've got nothing to transform and happy to drop the transformer if possible. I just need to be able to poll a directory for zip files and whenever there's one, send it to the queue called myQueue. Unfortunately the approach of receiving files from filesIn inbound-channel-adapter and sending to the queue using jmsOut out-bound-channel adapter doesn't seem to be working.

I am not sure if this is the right way to do it or if it's doable. Could someone please tell me what's wrong here and what I should do?

Upvotes: 0

Views: 1665

Answers (2)

Erik Williams
Erik Williams

Reputation: 933

I know your question is how to do this purely in Spring, but have you looked into using Apache Camel?

Most specifically the File component and one of JMS (JMS/ActiveMQ) components.

It does the polling for you and is highly configurable. It also plays very well with the other technologies you are using in your example. The route could be configured entirely in Spring.

Upvotes: 3

Gary Russell
Gary Russell

Reputation: 174729

Are you trying to send the File object, or the contents?

While java.io.File is Serializable, it doesn't really transfer properly (it has a number of transient fields).

If the server has access to the filesystem (e.g. NFS), transfer just the file name...

<int:transformer ... expression="payload.absolutePath" />

If you want to transfer the contents of the zip file(s), use a

<int-file:file-to-bytes-transformer/>.

Upvotes: 2

Related Questions