java - Concurrent SQS queue listeners -
i don't understand how sqs queuelistener works.
this config:
/** * aws credentials bean */ @bean public awscredentials awscredentials() { return new basicawscredentials(accesskey, secretaccesskey); } /** * aws client bean */ @bean public amazonsqs amazonsqsasyncclient() { amazonsqs sqsclient = new amazonsqsclient(awscredentials()); sqsclient.setregion(region.getregion(regions.us_east_1)); return sqsclient; } /** * aws connection factory */ @bean public sqsconnectionfactory connectionfactory() { sqsconnectionfactory.builder factorybuilder = new sqsconnectionfactory.builder( region.getregion(regions.us_east_1)); factorybuilder.setawscredentialsprovider(new awscredentialsprovider() { @override public awscredentials getcredentials() { return awscredentials(); } @override public void refresh() { } }); return factorybuilder.build(); } /** * registering queuelistener queuename */ @bean public defaultmessagelistenercontainer defaultmessagelistenercontainer() { defaultmessagelistenercontainer messagelistenercontainer = new defaultmessagelistenercontainer(); messagelistenercontainer.setconnectionfactory(connectionfactory()); messagelistenercontainer.setdestinationname(queuename); messagelistenercontainer.setmessagelistener(new messagelisteneradapter(new labqueuelistener())); messagelistenercontainer.seterrorhandler(new queuelistenererrorhandler()); messagelistenercontainer.settaskexecutor(executors.newfixedthreadpool(3)); return messagelistenercontainer; }
as can see, have configured defaultmessagelistenercontainer
executors.newfixedthreadpool(3)
this way expect have 3 concurrent task execution in queue listener @ 1 time.
thsi listener logic:
public class queuelistener { public void handlemessage(string messagecontent) { try { logger.info(string.format("message received: %s", messagecontent)); logger.info("wait 30 sec"); thread.sleep(1000 * 30); logger.info("done"); } catch (throwable th) { throw new queuelistenerexception(messagecontent, th); } } }
right each handlemessage
method blocks (thread.sleep(1000 * 30);
) execution 30 seconds , 1 handlemessage
method executes @ 1 time.
what doing wrong ? how achieve concurrent handlemessage
method invocation @ 1 time ? current configuration expect have 3 handlemessage
performed simultaneously.
you can add parameter handle concurrent execution in bean defaultmessagelistenerconfigurator adding messagelistenercontainer.setconcurrency("3-10");
means start 3 threads , scale 10.
number of concurrentconsumers can alternatively set using messagelistenercontainer.setconcurrentconsumers(3);
Comments
Post a Comment