Wednesday 15 April 2015

java how to handle callbacks of rabbitmq messages -



java how to handle callbacks of rabbitmq messages -

i want create server handle socket connections users, , within server want have connection rabbitmq, 1 per connection, in examples provided in webpage see "while" loops wait message, in case need create thread per connection process message rabbitmq.

is there way in java using spring or framework create phone call rabbitmq instead of using while loops? using node.js , pretty straightforward this, , want know proposals java

thanks in advanced.

you should take @ channel.basicconsume , defaultconsumer abstract class: https://www.rabbitmq.com/api-guide.html#consuming

java concurrency require thread callback handle each message, can utilize thread pool reuse threads.

static final executorservice threadpool; static { threadpool = executors.newcachedthreadpool(); }

now need create consumer handle each delivery creating runnable instance passed thread pool execute.

channel.basicconsume(queuename, false, new defaultconsumer(channel) { @override public void handledelivery(string consumertag, envelope envelope, amqp.basicproperties properties, byte[] body) throws ioexception { final byte msgbody = body; // 'final' re-create of body can pass runnable final long msgtag = envelope.getdeliverytag(); runnable runnable = new runnable() { @override public void run() { // handle message here dostuff(msgbody); channel.basicack(msgtag, false); } }; threadpool.submit(runnable); } });

this shows how can handle concurrent deliveries on single connection , channel without while loop in single thread blocked on each delivery. sanity, want factor runnable implementation own class take channel, msgbody, msgtag , other info parameters accessible when run() method called.

java rabbitmq

No comments:

Post a Comment