Announcement Announcement Module
Collapse
No announcement yet.
Move file to other directory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Move file to other directory

    Hi all,
    I have a requirement, in which I need to read a file using a <file:inbound-channel-adapter /> and then send the data in the channel. Then change the name of the file as abc.csv to abc.csv.processed and move it to different folder.

    How can I do this using Spring integration?

    Thanks,
    Dev

  • #2
    I am able to complete one part of my requirement, i.e. moving file from one directory to other and also forwarding the data in the channel , by using <file:outbound-gateway/> after the <file:inbound-channel-adapter/>.
    But now I am having one more requirement i.e. if the file is empty, then move it to rejected folder. How can I achieve this requirement? Please help.

    Thanks,
    Dev

    Comment


    • #3
      seems to me that this might be a small problem..
      Last edited by wikes; Mar 26th, 2012, 07:18 AM.

      Comment


      • #4
        Originally posted by wikes View Post
        seems to me that this might be a small problem..
        As I am new to SI, every suggestion will be helpful for me.

        Thanks,
        Dev

        Comment


        • #5
          Use a <router/> with expression="payload.length() == 0" (this evaluates to File.length() when the payload is a File object). When true, route to rejected channel when false, route to normal channel.

          Comment


          • #6
            Originally posted by Gary Russell View Post
            Use a <router/> with expression="payload.length() == 0" (this evaluates to File.length() when the payload is a File object). When true, route to rejected channel when false, route to normal channel.
            Hi, actually I want to move the file (if empty) from one folder to other, not the message in the channel.
            Using router we can move the message in the channel, what I know.

            Thanks,
            Dev

            Comment


            • #7
              Right...

              Code:
              <file:inbound-channel-adapter channel="choose"
                                            directory="file:${java.io.tmpdir}/spring-integration-samples/input"
                                            filename-pattern="*.txt">
              	<int:poller id="poller" fixed-delay="5000" max-messages-per-poll="2"/>
              </file:inbound-channel-adapter>
              
              <int:router input-channel="choose" expression="payload.length() == 0">
              	<int:mapping value="true" channel="reject" />
              	<int:mapping value="false" channel="filesIn" />
              </int:router>
              
              <int:channel id="reject" />
              
              <int:service-activator input-channel="reject" output-channel="stdout" 
              	expression="payload.renameTo(new java.io.File('/tmp/rejected/' + payload.name))" />
              
              <int-stream:stdout-channel-adapter id="stdout" append-newline="true" />
              This just puts the result of the rename to stdout - you could add a test to make sure it's true. Or, use groovy or java instead of a simple SpEL expression in the <service-activator/> that renames the file.

              Comment


              • #8
                You can have a file inbound adapter with a filter downstream with the expression Gary gave (expression="payload.length() == 0").
                The output channel of the filter can be an input to an file outbound channel adapter which writes to a specific directory. You may have the delete-source-files attribute set to true.

                Have a look at this section for file adapters and this for message filters.

                Comment


                • #9
                  Thanks Gary and Amol.

                  I used mixed solution suggested by you. I used file inbound adapter with a router downstream with the expression Gary gave (expression="payload.length() == 0"). Then I used rejected channel of the filter as an input to an file outbound channel adapter which writes to a specific directory also I used delete-source-files attribute set to true as suggested by Amol.

                  Thank you once again both of you.
                  Thanks,
                  Dev

                  Comment

                  Working...
                  X