Spring MVC how to get progress of running async task -


i start asynchronous task within controller in following code sniplet spring docs.

import org.springframework.core.task.taskexecutor;   public class taskexecutorexample {     private class messageprintertask implements runnable {       private int cn;       public messageprintertask() {       }       public void run() {  //dummy code  (int = 0; < 10; i++) {  cn = i;  }  }   }   private taskexecutor taskexecutor;   public taskexecutorexample(taskexecutor taskexecutor) {      this.taskexecutor = taskexecutor;    }     public void printmessages() {         taskexecutor.execute(new messageprintertask());     }  }  

afterwards in annother request (in case task running) need check progress of task. basicaly value of cn.

what best aproach in spring mvc how avoid syncronisation issues.

thanks

pepa procházka

have looked @ @async annotation in spring reference doc?

first, create bean asynchronous task:

@service public class asyncservicebean implements servicebean {      private atomicinteger cn;      @async     public void dosomething() {          // triggers async task, updates cn status accordingly     }      public integer getcn() {         return cn.get();     } } 

next, call controller:

@controller public class yourcontroller {      private final servicebean bean;      @autowired     yourcontroller(servicebean bean) {         this.bean = bean;     }      @requestmapping(value = "/trigger")     void triggerasyncjob() {         bean.dosomething();     }      @requestmapping(value = "/status")     @responsebody     map<string, integer> fetchstatus() {         return collections.singletonmap("cn", bean.getcn());     }         } 

remember configure executor accordingly, e.g.

<task:annotation-driven executor="myexecutor"/> <task:executor id="myexecutor" pool-size="5"/> 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -