Example #1
0
        internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func <Task> waitForDone)
        {
            var config = new TapetiConfig(dependencyResolver)
                         .WithDataAnnotations()
                         .WithTransient(TimeSpan.FromSeconds(5), "tapeti.example.04.transient")
                         .RegisterAllControllers()
                         .Build();


            using (var connection = new TapetiConnection(config))
            {
                await connection.Subscribe();


                Console.WriteLine("Sending request...");

                var transientPublisher = dependencyResolver.Resolve <ITransientPublisher>();
                var response           = await transientPublisher.RequestResponse <LoggedInUsersRequestMessage, LoggedInUsersResponseMessage>(
                    new LoggedInUsersRequestMessage());

                Console.WriteLine("Response: " + response.Count);


                // Unlike the other example, there is no need to call waitForDone, once we're here the response has been handled.
            }
        }
Example #2
0
        internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func <Task> waitForDone)
        {
            var doneCount = 0;

            var container = (IDependencyContainer)dependencyResolver;

            container.RegisterDefaultSingleton <IMessageParallelization>(new MessageParallelization(MessageCount, () =>
            {
                doneCount++;
                Console.WriteLine($"Processed batch #{doneCount}");

                if (doneCount != RepeatBatch)
                {
                    return(false);
                }

                var exampleState = dependencyResolver.Resolve <IExampleState>();
                exampleState.Done();
                return(true);
            }, count =>
            {
                Console.WriteLine($"Timeout while processing batch after processing {count} messages");

                var exampleState = dependencyResolver.Resolve <IExampleState>();
                exampleState.Done();
            }));



            var config = new TapetiConfig(dependencyResolver)
                         .RegisterAllControllers()
                         .Build();


            await using var connection = new TapetiConnection(config)
                        {
                            Params = new TapetiConnectionParams
                            {
                                // Default is 50, which means we'll get a timeout after 50 messages
                                PrefetchCount = MessageCount
                            }
                        };

            var subscriber = await connection.Subscribe(false);


            var publisher = dependencyResolver.Resolve <IPublisher>();

            Console.WriteLine($"Publishing {MessageCount * RepeatBatch} messages...");

            await PublishMessages(publisher, MessageCount *RepeatBatch);



            Console.WriteLine("Consuming messages...");
            await subscriber.Resume();

            await waitForDone();
        }
Example #3
0
        private static void Main()
        {
            // TODO SQL based flow store
            // TODO logging
            // TODO uitzoeken of we consumers kunnen pauzeren (denk: SQL down) --> nee, EFDBContext Get Async maken en retryen? kan dat, of timeout dan Rabbit?

            var container = new Container();

            container.Register <MarcoEmitter>();
            container.Register <Visualizer>();
            container.Register <ILogger, Tapeti.Default.ConsoleLogger>();

            var config = new TapetiConfig(new SimpleInjectorDependencyResolver(container))
                         .WithFlow()
                         .WithDataAnnotations()
                         .RegisterAllControllers()
                         .Build();

            using (var connection = new TapetiConnection(config)
            {
                Params = new TapetiAppSettingsConnectionParams()
            })
            {
                var flowStore  = container.GetInstance <IFlowStore>();
                var flowStore2 = container.GetInstance <IFlowStore>();

                Console.WriteLine("IFlowHandler is singleton = " + (flowStore == flowStore2));

                connection.Connected += (sender, e) => {
                    Console.WriteLine("Event Connected");
                };
                connection.Disconnected += (sender, e) => {
                    Console.WriteLine("Event Disconnected");
                };
                connection.Reconnected += (sender, e) => {
                    Console.WriteLine("Event Reconnected");
                };

                Console.WriteLine("Subscribing...");
                var subscriber = connection.Subscribe(false).Result;

                Console.WriteLine("Consuming...");
                subscriber.Resume().Wait();

                Console.WriteLine("Done!");

                connection.GetPublisher().Publish(new FlowEndController.PingMessage());

                container.GetInstance <IFlowStarter>().Start <MarcoController, bool>(c => c.StartFlow, true);

                Thread.Sleep(1000);

                var emitter = container.GetInstance <MarcoEmitter>();
                emitter.Run().Wait();
            }
        }
Example #4
0
        internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func <Task> waitForDone)
        {
            var container = (IDependencyContainer)dependencyResolver;

            container.RegisterDefaultSingleton <IMessageCounter>(new MessageCounter(MessageCount, () =>
            {
                var exampleState = dependencyResolver.Resolve <IExampleState>();
                exampleState.Done();
            }));



            var config = new TapetiConfig(dependencyResolver)
                         // On a developer test machine, this makes the difference between 2200 messages/sec and 3000 messages/sec published.
                         // Interesting, but only if speed is more important than guaranteed delivery.
                         //.DisablePublisherConfirms()
                         .RegisterAllControllers()
                         .Build();


            using (var connection = new TapetiConnection(config))
            {
                var subscriber = await connection.Subscribe(false);


                var publisher = dependencyResolver.Resolve <IPublisher>();
                Console.WriteLine($"Publishing {MessageCount} messages...");

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                await PublishMessages(publisher);

                stopwatch.Stop();
                Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds} ms, {MessageCount / (stopwatch.ElapsedMilliseconds / 1000F):F0} messages/sec");



                Console.WriteLine("Consuming messages...");
                await subscriber.Resume();

                stopwatch.Restart();

                await waitForDone();

                stopwatch.Stop();
                Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds} ms, {MessageCount / (stopwatch.ElapsedMilliseconds / 1000F):F0} messages/sec");
            }
        }
Example #5
0
        internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func <Task> waitForDone)
        {
            var config = new TapetiConfig(dependencyResolver)
                         .WithDataAnnotations()
                         .RegisterAllControllers()
                         .Build();

            using (var connection = new TapetiConnection(config)
            {
                // Params is optional if you want to use the defaults, but we'll set it
                // explicitly for this example
                Params = new TapetiConnectionParams
                {
                    HostName = "localhost",
                    Username = "******",
                    Password = "******",

                    // These properties allow you to identify the connection in the RabbitMQ Management interface
                    ClientProperties = new Dictionary <string, string>
                    {
                        { "example", "01 - Publish Subscribe" }
                    }
                }
            })
            {
                // IoC containers that separate the builder from the resolver (Autofac) must be built after
                // creating a TapetConnection, as it modifies the container by injecting IPublisher.
                (dependencyResolver as AutofacDependencyResolver)?.Build();


                // Create the queues and start consuming immediately.
                // If you need to do some processing before processing messages, but after the
                // queues have initialized, pass false as the startConsuming parameter and store
                // the returned ISubscriber. Then call Resume on it later.
                await connection.Subscribe();


                // We could get an IPublisher from the container directly, but since you'll usually use
                // it as an injected constructor parameter this shows
                await dependencyResolver.Resolve <ExamplePublisher>().SendTestMessage();


                // Wait for the controller to signal that the message has been received
                await waitForDone();
            }
        }
Example #6
0
        internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func<Task> waitForDone)
        {
            var config = new TapetiConfig(dependencyResolver)
                .RegisterAllControllers()
                .EnableDeclareDurableQueues()
                .Build();

            using (var connection = new TapetiConnection(config))
            {
                // This creates or updates the durable queue
                await connection.Subscribe();

                await dependencyResolver.Resolve<IPublisher>().Publish(new PublishSubscribeMessage
                {
                    Greeting = "Hello durable queue!"
                });

                // Wait for the controller to signal that the message has been received
                await waitForDone();
            }
        }
Example #7
0
        internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func <Task> waitForDone)
        {
            var config = new TapetiConfig(dependencyResolver)
                         .WithDataAnnotations()
                         .WithFlow()
                         .RegisterAllControllers()
                         .Build();


            using (var connection = new TapetiConnection(config))
            {
                // Must be called before using any flow. When using a persistent repository like the
                // SQL server implementation, you can run any required update scripts (for example, using DbUp)
                // before calling this Load method.
                // Call after creating the TapetiConnection, as it modifies the container to inject IPublisher.
                await dependencyResolver.Resolve <IFlowStore>().Load();


                await connection.Subscribe();


                var flowStarter = dependencyResolver.Resolve <IFlowStarter>();

                var startData = new SimpleFlowController.StartData
                {
                    RequestStartTime = DateTime.Now,
                    Amount           = 1
                };


                await flowStarter.Start <SimpleFlowController, SimpleFlowController.StartData>(c => c.StartFlow, startData);

                await flowStarter.Start <ParallelFlowController>(c => c.StartFlow);


                // Wait for the controller to signal that the message has been received
                await waitForDone();
            }
        }
Example #8
0
        internal static async Task MainAsync(IDependencyResolver dependencyResolver, Func <Task> waitForDone)
        {
            var config = new TapetiConfig(dependencyResolver)
                         .WithDataAnnotations()
                         .RegisterAllControllers()
                         .Build();


            using (var connection = new TapetiConnection(config))
            {
                await connection.Subscribe();

                var publisher = dependencyResolver.Resolve <IPublisher>();
                await publisher.PublishRequest <ExampleMessageController, QuoteRequestMessage, QuoteResponseMessage>(
                    new QuoteRequestMessage
                {
                    Amount = 1
                },
                    c => c.HandleQuoteResponse);

                await waitForDone();
            }
        }