2 Replies Latest reply on Dec 9, 2008 9:51 AM by Mario Scalas

    Running a background task within a SEAM application

    Mario Scalas Newbie

      How do I run a repetive scheduler task within my application? I thought that using the @Asynchronous annotation was enough but it is not the case:


      I put in components.xml that I want to use quartz (but I might use any other scheduling technology):


           
      <async:quartz-dispatcher />
      


      Then I write my scheduler:


      ...
      import org.jboss.seam.annotations.async.Asynchronous;
      import org.jboss.seam.annotations.async.IntervalDuration;
      ...
      
      @Name( "remoteRequestScheduler" ) @Scope( ScopeType.APPLICATION ) @Startup
      public class RemoteRequestScheduler {
      
           @Create
           public void startUp() {
                // Should start the repetive scheduling in my dreams ...
                scheduleToOperators( 5000 );
           }
      
           @Destroy
           public void shutDown() {
                logger.info( "Waiting for Scheduler to shut down ..." );
           }
      
           @Asynchronous
           public void scheduleToOperators( @IntervalDuration Long interval ) {
                logger.info( "Scheduling pending requests ..." );
      
                ensureDAOs();
                List<RemoteRequest> pendingRequests = remoteRequestDAO.all();
                for (RemoteRequest request : pendingRequests) {
                     // No operator? Schedule to someone!
                     if (request.getOperator() == null) {
                          scheduleToOperator( request );
                     }
                }
           }
      }
      


      This obviously doesn't work: the scheduler is just invoked the first time the component is @Created but not anymore time ... I'm pretty lost here since I cannot figure out how to run such background tasks and creating my own threaded component just doesn't work with SEAM's contexts ... :S

        • 1. Re: Running a background task within a SEAM application
          Tobias Hill Newbie

          You cannot schedule from the same component as your do-some-work-method is in ... you know ... you need to get through the layers of interceptors to allow seam do its work. When doing a direct invocation you are not calling the method through all its interceptors.


          I have applied a inner class pattern for all my background needs.


          As a draft:



          @name("foo")
          public class Foo {
             
              @Asynchronous
              public doWork(@IntervalDuration ... ) {...}
          
             
              @Startup
              @Scope(APPLICATION)
              @Name("fooKicket")
              public static class FooKicker {
          
                  @Create
                  public void init() { doWork( ... ) }
          
              }
          }




          Good luck.

          • 2. Re: Running a background task within a SEAM application
            Mario Scalas Newbie

            Great! It works perfectly with the static kicker class pattern! Thanks Tobias, +1 for me!


            Just as side note: it this behavior documented anywhere?