java - @Async is not working in REST class -
i working on webservice project built using spring. configuration done using annotation. there 1 method sends push notification. there many notifications send, causes delay in response. so, apply @async annotation "sendpushnotification" method. but, still no improvement in response. have gone through of blogs , stackoverflow find solution. but, no success. have applied following annotation service class.
@component @configuration @enableaspectjautoproxy @enableasync
method async called.
@post @path("/sampleservice") @consumes(mediatype.application_form_urlencoded) public response sampleservice(@formparam ...) { ... list<user> commentors = <other method fetch commentors>; sendpushnotification(commentors); ... }
my async method.
@async private void sendpushnotification(list<user> commentors) { if (commentors != null) { (user user : commentors) { try { int numnewcomments = ps.getcommentsunseenbyuser(user); sendmessage("ios", user, "2", "" + numnewcomments, "true"); } catch (exception e) { log.error(e.getmessage(), e); } } } }
is there missing?
you're invoking method on this
sendpushnotification(commentors); // equivalent this.sendpushnotification(commentors);
that won't work. spring works proxying beans provide functionality. gives proxy bean has reference real object. caller sees , invokes
proxy.someenhancedmethod()
but happens is
proxy.someenhancedmethod() -> enhanced logic -> target.someenhancedmethod()
but in sampleservice
service method, don't have reference proxy, have reference target. you've gained nothing, basically. suggest moving @async
logic different type, declaring , injecting bean of type resource.
spring explains of above in documentation, here.
Comments
Post a Comment