Results 1 to 5 of 5

Thread: File resource changes not visible in TimerTask run ()

  1. #1
    Join Date
    Dec 2009
    Posts
    3

    Default File resource changes not visible in TimerTask run ()

    Hi,

    I am passing text file as classpath resource to TimerTask bean and reading in run() method as follows -

    <property name="resource" value="classpath:somemap.txt"/>

    public void run () {
    BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
    }

    But the problem is if i change somemap.txt contents at runtime, changes are not picked up in next run of the timer task

    any ideas ?

    thanks
    adi

  2. #2
    Join Date
    Jun 2009
    Posts
    190

    Default

    Can you please post some more snippet if possible(bean definition to be specific)

  3. #3
    Join Date
    Dec 2009
    Posts
    3

    Default

    actually i managed to get updates by using

    BufferedReader reader = new BufferedReader(new FileReader(resource.getFile()));

    instead of
    BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));

  4. #4
    Join Date
    Dec 2009
    Posts
    3

    Default

    bean definitions as follows

    <bean id="fileReaderTask" class="com.db.gme.analytics.eas.aws.util.RepeatTas k">
    <property name="resource" value="classpath:somemap.txt"/>
    </bean>

    <bean id="scheduledTask" class="org.springframework.scheduling.timer.Schedu ledTimerTask">
    <property name="delay" value="0" />
    <!-- run every 15 minutes -->
    <property name="period" value="900000" />
    <property name="timerTask" ref="repeatTask" />
    </bean>

    <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerF actoryBean">
    <property name="scheduledTimerTasks">
    <list>
    <ref bean="scheduledTask" />
    </list>
    </property>
    </bean>

  5. #5
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    It looks like the class loader used at your application performs resources caching. That may be the case for Tomcat class loader for example (afair it caches last 500 requested resources by default).

    The general idea of your solution is correct - use the approach that avoids class loader-level caching. However, it can be implemented more elegantly by automatically replacing injected 'org.springframework.core.io.ClassPathResource' object by implied 'org.springframework.core.io.FileSystemResource' object. You can use the following algorithm:
    1. Write the code that has a chance to intercept new resource injection (e.g. by loading resources via custom ResourceLoader or applying aspectj weaving-based AOP);
    2. Check if Resource.isReadable() returns 'true' and Resource.getFile() doesn't throw an exception;
    3. Create new Resource implementation that wraps given resource and delegates all calls except getInputStream() to it. 'getInputStream()' should be implemented via FileInputStream;
      [I]Inject the object constructed above as a dependency to the target bean instead of initial resource;


    Such an approach allows to solve the problem transparently to all defined beans.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •