Announcement Announcement Module
Collapse
No announcement yet.
background threads in Spring application? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • background threads in Spring application?

    Folks;
    currently I try integrating Apache CouchDB change notifications into our Spring webapp. The idea, generally, is to have a long-running HTTP GET request to the CouchDB backend from some Java client class which, therefore, also is a long-running thread (to start with the application context and to terminate with the application context being terminated) providing some sort of event handlers invoked whenever there's some change in the CouchDB database structure.

    At the moment, I try to find the most "clean" way how to include this into the Spring application. My initial attempt was to introduce a ServletContextListener to create (and run) an instance of this monitoring thread, however, this seems pretty much outside Spring.

    The second idea was "just" to create a pretty much standard Spring bean which starts (and runs) the CouchDB "watcher" thread either in its constructor or in some method triggered once, in example, by a SimpleTriggerBean.

    Looking around, however, I am not sure which of these approaches (if any) is the one I would want to go for, or whether there are more sane ways of dealing with this setup. So, how would you add a feature like this one (a "background" thread starting at context startup and terminating with context shutdown) into your Spring app?

    Any input on this greatly welcome. Thanks in advance and all the best,
    Kristian

  • #2
    If you are using Spring 3 you can use the task namespace.

    Basically xmlns:task="http://www.springframework.org/schema/task"

    Using this you can

    Code:
    <!-- Define scheduler-->
    <task:scheduler id="scheduler" pool-size="1"/>
    <!-- Define task, which will use above defined scheduler. This task wil invoke someMethod of beanToBeInvoked   bean  every 5 seconds-->
            <task:scheduled-tasks scheduler="scheduler">
    		<task:scheduled ref="beanToBeInvoked" method="someMethod" fixed-delay="5000"/>
            </task:scheduled-tasks>

    Comment


    • #3
      Hi there;

      and first off, thanks a bunch for your response, greatly appreciated.

      Originally posted by rishishehrawat View Post
      If you are using Spring 3 you can use the task namespace.

      Code:
      <!-- Define scheduler-->
      <task:scheduler id="scheduler" pool-size="1"/>
      <!-- Define task, which will use above defined scheduler. This task wil invoke someMethod of beanToBeInvoked   bean  every 5 seconds-->
              <task:scheduled-tasks scheduler="scheduler">
      		<task:scheduled ref="beanToBeInvoked" method="someMethod" fixed-delay="5000"/>
              </task:scheduled-tasks>
      I will definitely try this in order to ease our task configuration in other parts of the system, but then again, I am unsure how this fits my current use case - actually, I would want to have a long-running background task that starts with the context being started and is terminated with the context being terminated. The use case itself is to have this task continuously connected to the backend system (HTTP long polling) and handle incoming events so regular scheduled tasks don't seem the way to go here. Can I use the <task:> stuff to have a task that just runs _once_ with a given delay after the context has been started? This would help IMHO.

      Thanks in advance and all the best,
      Kristian

      Comment


      • #4
        You can use a cron expression to trigger task once.

        Code:
        <!-- Define scheduler-->
        <task:scheduler id="scheduler" pool-size="1"/>
        <!-- Define task, which will use above defined scheduler. This task wil invoke someMethod of beanToBeInvoked   using the defined cron expression-->
                <task:scheduled-tasks scheduler="scheduler">
        		<task:scheduled ref="beanToBeInvoked" method="someMethod" cron=" " />
                </task:scheduled-tasks>
        If you leave the cron expression empty I feel that it will trigger this task once at startup. If this does not work you can lookup the cron expression for trigerring the task once.

        Comment


        • #5
          This is probably because an vaadin application is not managed by the spring dispatcher and your session scoped bean does not share the same context as your vaadin app

          ccie voice
          ccie voice lab
          msp training

          Comment

          Working...
          X