3

I have two exactly same consumers

Consumer 1

using (var adapter = new BuiltinHandlerActivator())
        {
            adapter.Handle<string>(async (bus, message) =>
            {
                Console.WriteLine("Got message > " + message);

                await bus.Reply("Received in consumer 1");
            });

            Configure.With(adapter)
                .Transport(t => t.UseAzureServiceBus(connectionString, "server"))
                .Start();

            adapter.Bus.Subscribe<string>().Wait();

            Console.WriteLine("Press ENTER to quit");
            Console.ReadLine();
        }

Consumer 2

using (var adapter = new BuiltinHandlerActivator())
        {
            adapter.Handle<string>(async (bus, message) =>
            {
                Console.WriteLine("Got message > " + message);

                await bus.Reply("Received in Consumer 2");
            });

            Configure.With(adapter)
                .Transport(t => t.UseAzureServiceBus(connectionString, "server"))
                .Start();

            adapter.Bus.Subscribe<string>().Wait();

            Console.WriteLine("Press ENTER to quit");
            Console.ReadLine();
        }

Producer

using (var adapter = new BuiltinHandlerActivator())
        {
            adapter.Handle<string>(async message =>
            {
                Console.WriteLine("Returned > " + message);
            });

            var bus = Configure
                .With(adapter)
                .Transport(t => t.UseAzureServiceBus(connectionString, "client"))
                .Routing(r => r.TypeBased().Map<string>("server"))
                .Start();

            Console.WriteLine("Press Q to quit or any other key to produce a job");
            while (true)
            {
                Console.Write("Write something > ");
                var text = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(text)) break;

                bus.Publish(text).Wait();
            }
        }

What I expect is whenever I send message from the Producer, both my Consumers to display the message. Now it only does it in one of them. When I close that one and send another message, the remaining one receives it.

1 Answers1

3

Basically, you only need to give the consumers different names. Rebus creates a topic for each producer (based on assembly, namespace, type), and creates subscriptions for each consumer in these topics. If two consumers use the same name, they compete for the message.

                .Transport(t => t.UseAzureServiceBus(connectionString, "consumer1"))
                .Transport(t => t.UseAzureServiceBus(connectionString, "consumer2"))

Full example: https://github.com/rebus-org/RebusSamples/tree/master/PubSubNative

Some other helpful links:

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • Thanks this works, but I am confused. The second parameter says is `inputQueueAddress` and i thought it needs to be the same (e.g. server) in order for the messages to reach there. Now i'm testing with different string values for this parameter in every class. And it's still working. – Dimitar Mihaylov Mar 25 '20 at 21:34
  • And now both consumers consume all messages that are strings. Basically what I'm playing is 1 Producer - 2 Consumers (send 1 message and receive in the consumers) this is now working. Now what I'd like to try is 1 producer send 1 string message but to given consumer. Do i need to use .Send() in this case. – Dimitar Mihaylov Mar 25 '20 at 21:42
  • Glad I could help. You should probably ask this in a new question though. I am not quite sure what you mean, and if you ask a new question it can be seen by a lot more people. – Alex AIT Mar 26 '20 at 07:34