Pingpong
Pingpong

Reputation: 8009

Pub sub pattern created Queues and Topics on Azure Service Bus with Rebus

The app below is tweaked to work with Azure Service Bus. https://github.com/rebus-org/RebusSamples/tree/master/PubSub

However, below are created.

Queues

error

publisher

subscriber

Topics

messages_datetimemessage__messages: subscriber

messages_stringmessage__messages: subscriber

messages_timespanmessage__messages: subscriber

My question

is that is this correct?

And is it possible to reduce the number of artifacts that are created? For example, reduce to only one topic, because topic is used for pub sub.

Update

I need to use Pub sub pattern with one Topic and one or two subscriptions if possible.

However, I have got an error below:

System.AggregateException HResult=0x80131500 Message=One or more errors occurred. (Could not publish to topic 'order')
Source=System.Private.CoreLib StackTrace: at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.Wait() at Publisher.Program.Main() in C:_MyLab\ReBus\PubSub\Publisher\Program.cs:line 43

Inner Exception 1: RebusApplicationException: Could not publish to topic 'order'

Inner Exception 2: InvalidOperationException: Cannot open a Topic client for entity type Queue. TrackingId:5c380af2-ad8f-4788-85b8-5427dd7873e4_B4, SystemTracker:myapp:Queue:order, Timestamp:2019-04-29T22:31:57 TrackingId:9c3e0c40-4410-4102-a705-86a6528cd030_B4, SystemTracker:myapp:Queue:order, Timestamp:2019-04-29T22:31:57 TrackingId:401a15d284ad44989f5e451c963d81e5_G16, SystemTracker:gateway7, Timestamp:2019-04-29T22:31:57

UseAzureServiceBus seems wrong, because it is using queue

class Publisher
    {
        static void Main()
        {
            using (var activator = new BuiltinHandlerActivator())
            {
                Configure.With(activator)
                    .Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))  
                    .Transport(t => t.UseAzureServiceBus(Consts.ServiceBusConnectionString, Consts.Order))                   
                    .Start();

                 activator.Bus.Advanced.Topics.Publish(Consts.Order, new StringMessage("Hello there, I'm a publisher!")).Wait();

            }

            }

UseAzureServiceBus seems wrong, because it is using queue. Is the Handler able to handle message?

class Subscriber
    {
        static void Main()
        {
            using (var activator = new BuiltinHandlerActivator())
            {
                activator.Register(() => new Handler());

                Configure.With(activator)
                    .Logging(l => l.ColoredConsole(minLevel: LogLevel.Warn))                   
                    .Transport(t => t.UseAzureServiceBus(Consts.ServiceBusConnectionString, Consts.Order))
                    .Routing(r => r.TypeBased().MapAssemblyOf<StringMessage>(Consts.Order))
                    .Start();

                activator.Bus.Advanced.Topics.Subscribe(Consts.Order);

                Console.WriteLine("This is Subscriber 1");
                Console.WriteLine("Press ENTER to quit");
                Console.ReadLine();
                Console.WriteLine("Quitting...");
            }
        }
    }

    class Handler : IHandleMessages<StringMessage>, IHandleMessages<DateTimeMessage>, IHandleMessages<TimeSpanMessage>
    {
        public async Task Handle(StringMessage message)
        {
            Console.WriteLine("Got string: {0}", message.Text);
        }

        public async Task Handle(DateTimeMessage message)
        {
            Console.WriteLine("Got DateTime: {0}", message.DateTime);
        }

        public async Task Handle(TimeSpanMessage message)
        {
            Console.WriteLine("Got TimeSpan: {0}", message.TimeSpan);
        }
    }

The code above create an Order queue, which is not what I want.

I want a topic, and one or two subscriptions.

Upvotes: 1

Views: 1238

Answers (1)

mookid8000
mookid8000

Reputation: 18628

Rebus pretty much encourages you to use .NET types as topics, which in this case means that the topics

  • messages_datetimemessage__messages
  • messages_stringmessage__messages
  • messages_timespanmessage__messages

get created, because you publish events of types DateTimeMessage, StringMessage, and TimeSpanMessage (all residing in the Messages namespace in the Messages assembly).

If that is not what you want, Rebus allows you to publish to custom topics like so:

await bus.Advanced.Publish("my-topic", new DateTimeMessage(...))`;

which in this case will result in the creation of a single topic: my-topic.

The subscriber(s) will need to subscribe accordingly:

await bus.Advanced.Subscribe("my-topic");

Keep in mind that there's no filtering of the types of events published to a topic, so the subscriber above will receive anything published to the my-topic topic, regardless of whether it is capable of handling it.

Upvotes: 1

Related Questions