Announcement Announcement Module
Collapse
No announcement yet.
New to Spring Integration - Need help with Poller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • New to Spring Integration - Need help with Poller

    Code:
    Hi Guys,
    
    I am new to spring/spring integration and I am trying to develop a poller that will poll a folder and (for now) just continously list all the files in the folder. I have the following in my config.xml 
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:si="http://www.springframework.org/schema/integration"
           xmlns:file="http://www.springframework.org/schema/integration/file"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/integration
                http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
                http://www.springframework.org/schema/integration/file
                http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">
    
    <si:channel id="input">
            <si:queue capacity="100"/>
    </si:channel>
    <file:inbound-channel-adapter id ="fileinput" directory="/tmp" channel="input">
    <si:poller>
        <si:interval-trigger interval="1"/>
    </si:poller>
    </file:inbound-channel-adapter>
    <si:gateway id="readfile" service-interface="folderpoller.readFiles" default-request-channel="input"/>
    
    <bean id="readFileNames" class="folderpoller.readFileNames"
        p:pathName="/tmp">
        </bean>
    </beans>
    and I have the following in my main.
    Code:
        ApplicationContext context = new ClassPathXmlApplicationContext("folderPoller/config.xml");
         PollableChannel channel = (PollableChannel) context.getBean("input");
            Message<?> recv = channel.receive();
    I need to find a way to test this to make sure it is polling, currently I think it is only doing the pull once and not polling the folder over and over. On the INFO line I only get the following once for each file. I assume that if it is polling correctly it should show this each time it polls? Am I correct? If not can anyone spot a reason why this is not polling and only running once? There is a good chance that there is a fundamental flaw in my code, as I have mentioned before this is my first time using spring integration (and spring for that matter) so i am quite new to the whole thing.
    Code:
    INFO: Created message: [[Payload=\tmp\Copy of test.txt]
    The bean that it points to (readFileNames) just reads the files from the directory.

    Thanks in advance for any help that I get. If i am not explaining something clearly, please let me know and i will try and explain again.

  • #2
    The default behavior for the file polling is to only accept each file once while the app is running. Do you want to re-read the same file repeatedly?

    Also, your polling interval is 1 millisecond. Most likely you want that to be a bigger number (e.g. interval="1000" for 1 second).

    Comment


    • #3
      Hi Mark thanks for the reply. That actually makes sense. I added another file and it picks it up on the fly. Thanks (I think I was being quite a noob there, I apologize).

      I was playing with the timer, so it was 1 millisecond when i copy pasted my code. Have been using 1000, but going to configure it to 5000 later on.

      Thanks again. I see now that I get another INFO: message on the console for the new file. Is there anyway I can notify a thread the second the poller picks up something (i.e. in this case spots a new file in the folder)?

      Comment


      • #4
        For immediate notification, you can remove the 'queue' sub-element from your 'input' channel. Then, connect something to that channel (e.g. a "service-activator" element). It will actually occur in the same thread that read the File by default. You can either configure the "task-executor" reference on that "poller" element to manage a thread pool there, or you can use a task-executor for the dispatching of the message itself. What version of Spring Integration are you using?

        Comment


        • #5
          Using 1.0.3 (actually followed this guide of yours http://blog.springsource.com/2009/02/13/982/). It was very helpful. I will try using a task-excutor and a service activator when I get a chance a little later.

          Is there any way to customize the file poller to notify when a file is modified (currently it only seems to notify on a new file)?

          Thanks again.

          Comment


          • #6
            So I added a service activator and task executor (attached to my poller).

            My Service activator looks like this:

            Code:
            <si:service-activator input-channel="input" ref="processFile" method="fileProcessor" output-channel="output" >
            </si:service-activator>
            Everytime it polls I get a message back on the command line (INFO: message). I want to read this message. So I use the event handler to print the messages off the pollable channel. It seems to only be getting every second message for some reason, and I can't figure out why.

            Latest xml looks like:
            Code:
            <si:channel id="input">
                <si:queue capacity="10"/>
            </si:channel>
            
            <si:channel id="output">
                <si:queue capacity="10"/>
            </si:channel>
            
            <file:inbound-channel-adapter id ="fileinput" directory="/tmp" channel="input" >
            <si:poller task-executor="pool">
                <si:interval-trigger interval="1000"/>
            </si:poller>
            </file:inbound-channel-adapter>
            
            <bean id="processFile" class="folderpoller.processfile">
            </bean>
            The bean is running the following:
            Code:
                    PollableChannel channel;
                    channel = (PollableChannel) context.getBean("input");
                    Message<?> recv;
                    recv = Main.channel.receive();
                    System.out.println("MESSAGE IS:" + recv);
            Can anyone see where the messages are going? I get all the messages on the INFO: line, but it only prints half the messages.

            Comment


            • #7
              Cron Expression on file inbound adapter

              In the below configuration Am I missing anything?

              <int-file:inbound-channel-adapter id="fileIn" directory="file://${to.transfer}"
              filename-regex="${file.pattern}"
              queue-size="10" auto-create-directory="true" channel="localfile">

              <int : poller id="poller" default="false"
              cron="*/10 * 15-16 * * MON-FRI"
              max-messages-per-poll="10"
              error-channel="ftpErrorChannel"/>

              </int-file:inbound-channel-adapter>

              Why cron expression is not working?

              Comment


              • #8
                Please don't ask the same question in 2 places.

                Comment

                Working...
                X