c# - using EasyNetQ multiple Handler for one consumer does not work -


we using rabbitmq queuing messages in c# .net (easynetq client).

i want 1 consumer app (c# console app) listen 1 queue , provide multiple handlers each message type.

i implemented scenario , code here :

using (var advancedbus = rabbithutch.createbus("host=localhost;prefetchcount=100")                                     .advanced) {     var queue = advancedbus.queuedeclare("myqueue");      advancedbus.consume(queue, x => x                .add<messagetype1>((message, info) =>                {                   console.writeline("messagetype1 body : " + message.body.body);                })                .add<messagetype2>((message, info) =>                 {                   console.writeline(" messagetype2 body: " + message.body.body);                }).throwonnomatchinghandler = false); } 

my problem : when execute consumer nothing. not thing happen.

i publish messages queue :

using (var advancedbus = rabbithutch.createbus("host=localhost").advanced) {     var queue = advancedbus.queuedeclare("myqueue");      if (advancedbus.isconnected)         advancedbus.publish(exchange.getdefault(), queue.name, false, false,             new message<messagetype1>(change));     else         result = false; } 

what problem.

ok, after testing code, these problems:

first of all, you're disposing advancedbus right after register consumption. need remember when invoke iadvancebus.consume, you're registering callback each message. if dispose bus after registration, delegate can't invoked since connection closed. so, you'll remove using statement around rabbit declaration (don't forget dispose when you're done):

var advancedbus = rabbithutch.createbus("host=localhost;prefetchcount=100").advanced 

second, immediate flag has been deprecated , shouldn't used, message doesn't seem getting queue. change publish to:

advancedbus.publish(exchange.getdefault(), queue.name, true, false,                     new message<messagetype1>(change)); 

also, if you're running console application, don't forget use console.readkey main thread doesn't terminate.

here's working code sample:

static void main() {     var change = new messagetype1();     var advancedbus = rabbithutch.createbus("host=localhost").advanced;      consumemessage(advancedbus);      var queue = advancedbus.queuedeclare("myqueue");     if (advancedbus.isconnected)     {         advancedbus.publish(exchange.getdefault(), queue.name, true, false,             new message<messagetype1>(change));     }     else     {         console.writeline("can't connect");     }      console.readkey(); }  private static void consumemessage(iadvancedbus advancedbus) {     var queue = advancedbus.queuedeclare("myqueue");     advancedbus.consume(queue, registration =>     {         registration.add<messagetype1>((message, info) =>         {             console.writeline("body: {0}", message.body);         });     }); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -