Exemple #1
0
        static void SendAutoDistributeDemo()
        {
            // demonstrate distributing messages evenly across queues
            // each call to Send chooses the next queue to send the message to

            IBus bus = new BusBuilder()
                       .WithLogging(new FileLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineWriteQueue("MiniBus.messages1")
                       .DefineWriteQueue("MiniBus.messages2")
                       .DefineWriteQueue("MiniBus.messages3")
                       .CreateLocalQueuesAutomatically()
                       .AutoDistributeOnSend()
                       .JsonSerialization()
                       .CreateBus();

            var p = new Person {
                Name = "Bob", Age = 20
            };

            bus.Send(p); // MiniBus.messages1
            bus.Send(p); // MiniBus.messages2
            bus.Send(p); // MiniBus.messages3
            bus.Send(p); // MiniBus.messages1

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
Exemple #2
0
        static async Task <IBus> JupiterService()
        {
            var bus = new BusBuilder()
                      .UseAzureStorageQueue(
                "UseDevelopmentStorage=true",
                configuration =>
            {
                configuration.PublishAndSendOptions.AddAvailableQueues("saturn");
                configuration.PublishAndSendOptions
                .MapCommandToQueue <AppleCommand>("saturn");
            })
                      //.UseAzureServiceBus(
                      //    ConnectionString,
                      //    configuration =>
                      //    {
                      //        configuration.SetManagementSettings(SubscriptionId, TenantId, ClientId, ClientSecret, ResourceGroupName, NamespaceName);
                      //        configuration.PublishAndSendOptions
                      //                     .AttemptCreateTopicWith(new Microsoft.Azure.Management.ServiceBus.Models.SBTopic { EnablePartitioning = true })
                      //                     .MapCommandToQueue<AppleCommand>("saturn");
                      //    })
                      .DefineCommandScanRuleWith(t => t.Namespace == "RockyBus.DemoMessages" && t.Name.EndsWith("Command"))
                      .DefineEventScanRuleWith(t => t.Namespace == "RockyBus.DemoMessages" && t.Name.EndsWith("Event"))
                      .Build();
            await bus.Start();

            await bus.Publish(new CatEvent { Text = "Meow" });

            await bus.Send(new AppleCommand { Text = "jupiter to saturn" });

            return(bus);
        }
Exemple #3
0
        static void SendDemo()
        {
            // demonstrate sending message to defined write queue

            IBus bus = new BusBuilder()
                       .WithLogging(new FileLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineWriteQueue("MiniBus.messages1")
                       .CreateLocalQueuesAutomatically()
                       .EnlistInAmbientTransactions()
                       .JsonSerialization()
                       .CreateBus();


            // transaction scope isn't required unless modifying another transactional resource at the same time
            using (var scope = new TransactionScope())
            {
                for (int i = 1; i <= 10; i++)
                {
                    var p = new Person {
                        Name = "Bob", Age = i
                    };

                    // insert/update a database to see atomic commit

                    bus.Send(p);
                }
                scope.Complete();
            }

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
Exemple #4
0
        public Passenger(int _pid, bool _isMissionary) // TODO: call this from main with data received from args[]
        {
            this.PID    = _pid;
            this._TYPE_ = _isMissionary ? PassengerType.Missionary : PassengerType.Cannibal;

            // create a bus for sending messages
            IBus bus = new BusBuilder()
                       .WithLogging(new ConsoleLogger())
                       .DefineErrorQueue("Passenger.errors")
                       .DefineWriteQueue("Boat")
                       .CreateLocalQueuesAutomatically()
                       .JsonSerialization()

                       .CreateBus();

            // create your message type
            var msg = new Message {
                PID = this.PID, Value = (int)_TYPE_
            };

            // send it
            bus.Send(msg);

            // create a bus for receiving messages
            bus = new BusBuilder()
                  .WithLogging(new ConsoleLogger())
                  .DefineErrorQueue("Passenger.errors")
                  .DefineReadQueue("Passenger." + PID)
                  //.DefineWriteQueue("Boat")
                  .CreateLocalQueuesAutomatically()
                  .JsonSerialization()
                  .CreateBus();

            // register one or more message handlers
            bus.RegisterHandler <bool>(new ResponseHandler());
            // process messages on the read queue synchronously
            bus.Receive <bool>();

            // process messages on the read queue asynchronously
            //bus.ReceiveAsync<PlaceOrder>();

            // DIE in handler
        }
Exemple #5
0
        private bool ProcessPassengers()
        {
            // after 4 seconds, check if CONDITION_CrossRiver
            if (CONDITION_CrossRiver)
            {
                List <String> temp = new List <string>();
                foreach (var p in PassengersInBoat)
                {
                    temp.Add(pre + p.PID);
                }

                var tempBus = new BusBuilder()
                              .WithLogging(new ConsoleLogger())
                              .DefineErrorQueue("Boat.errors")
                              .DefineWriteQueues(temp.ToArray())
                              .CreateLocalQueuesAutomatically()
                              .JsonSerialization()
                              .CreateBus();


                // send it to all passengers in boat that crossed the river (:
                tempBus.Send(true);

                PassengersInBoat.Clear();
                // transfer passengers from waiting list to boat
                var removeList = new List <int>();
                foreach (var pr in PassengerRequests)
                {
                    if (CONDITION_AddPassengerToBoat)
                    {
                        PassengersInBoat.Add(pr);
                        removeList.Add(pr.PID);
                    }
                }
                PassengerRequests.RemoveAll(r => removeList.Any(a => a == r.PID));

                return(true); // keep going
            }

            // die
            return(false);
        }
Exemple #6
0
        static async Task <IBus> JupiterService()
        {
            var bus = new BusBuilder()
                      .UseAzureServiceBus(
                ConnectionString,
                configuration =>
            {
                configuration.SetManagementSettings(SubscriptionId, TenantId, ClientId, ClientSecret, ResourceGroupName, NamespaceName);
                configuration.PublishAndSendOptions
                .MapCommandToQueue <AppleCommand>("saturn");
            })
                      .DefineCommandScanRuleWith(t => t.Namespace == "RockyBus.DemoMessages" && t.Name.EndsWith("Command"))
                      .DefineEventScanRuleWith(t => t.Namespace == "RockyBus.DemoMessages" && t.Name.EndsWith("Event"))
                      .Build();
            await bus.Start();

            await bus.Publish(new CatEvent { Text = "Meow" });

            await bus.Send(new AppleCommand { Text = "jupiter to saturn" });

            return(bus);
        }