i want invoke rest webservice call asynchronously using spring 3.1 rest template. using resttemplate.getforobject method invoke rest webservice. per requirement have invoke webservice method concurrently along existing one. invoking method call ejb 2.1 stateless session bean instantiating method class.
sample code below:-
public class myimplserverbean extends ejbobject{ restserviceimpl restimpl = new restserviceimpl(); arraylist<user> userdetailslist = restimpl.getuserdetails(123); } public class restserviceimpl { resttemplate template = new resttemplate(); public arraylist<user> getuserdetails(int userid){ arraylist<user> userlist = new arraylist<user>(); string url = "http://localhost:7001/myuserservice/userid"; user user = template.getforobject(url, user.class); userlist.add(user); return userlist; }
we need make getuserdetails method call asynchronously. got idea using @async annotation not aware how implement. can please in this.
i used this tutorial accomplish similar task. did modifications. here how working.
1) create configuration class , make sure scanned applicationcontext. use annotationconfigwebapplicationcontext.
@enableasync @configuration public class asyncconfiguration implements asyncconfigurer { @bean(name = "threadpooltaskexecutor") public executor threadpooltaskexecutor() { threadpooltaskexecutor executor = new threadpooltaskexecutor(); executor.setcorepoolsize(10); return executor; } @override public executor getasyncexecutor() { return new threadpooltaskexecutor(); } @override public asyncuncaughtexceptionhandler getasyncuncaughtexceptionhandler() { return new customasyncexceptionhandler(); } }
2) annotate method with
@async("threadpooltaskexecutor")
your method must either return void or future.
3) make sure async method public , called class. way spring "sees" annotation when method invoked reflectively.
Comments
Post a Comment