sagar
sagar

Reputation: 177

Recurring message Masstransit rabbitmq

I am trying to implement a heartbeat feature for my application hence was trying to implement recurring message feature from masstransit rabbitmq. I was trying to implement it on the sample given on masstransit's website. Here's all of the code.

namespace MasstransitBasicSample
{
    using System;
    using System.Threading.Tasks;
    using MassTransit;
    using MassTransit.Scheduling;

    class Program
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                var host = sbc.Host(new Uri("rabbitmq://localhost"), h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });

                sbc.UseMessageScheduler(new Uri("rabbitmq://localhost/quartz"));

                sbc.ReceiveEndpoint(host, "test_queue", ep =>
                {
                    ep.Handler<YourMessage>(context =>
                    {
                        return Console.Out.WriteLineAsync($"Received: {context.Message.Text}");
                    });
                    ep.Handler<PollExternalSystem>(context =>
                    {
                        return Console.Out.WriteLineAsync($"Received: {context.Message}");
                    });
                });
            });

            bus.Start();
            SetRecurring(bus);

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

            bus.Stop();
        }

        private static async Task SetRecurring(IBusControl bus)
        {
            var schedulerEndpoint = await bus.GetSendEndpoint(new Uri("rabbitmq://localhost/quartz"));

            var scheduledRecurringMessage = await schedulerEndpoint.ScheduleRecurringSend(
               new Uri("rabbitmq://localhost/test_queue"), new PollExternalSystemSchedule(), new PollExternalSystem());
        }
    }

    public class YourMessage { public string Text { get; set; } }

    public class PollExternalSystemSchedule : DefaultRecurringSchedule
    {
        public PollExternalSystemSchedule()
        {
            CronExpression = "* * * * *"; // this means every minute
        }
    }

    public class PollExternalSystem { }
}

I have created a queue called quartz in my rabbitmq queue.

When i run the application it sends a message to the quartz queue and that message just stays there , it does not go to the test queue.

I was also expecting another message to be sent to the quartz queue after a minute based on the cron expression, that also does not happen.

Is my setup wrong?

Any help would be much appreciated.

Upvotes: 1

Views: 2678

Answers (1)

Alexey Zimarev
Alexey Zimarev

Reputation: 19640

You need to run the scheduling service that listens on rabbitmq://localhost/quartz, where your messages are being sent.

The documentation page says:

There is a standalone MassTransit service, MassTransit.QuartzService, which can be installed and used on servers for this purpose. It is configured via the App.config file and is a good example of how to build a standalone MassTransit service.

Alternatively, you can host Quartz scheduling in the same process by using in-memory scheduling, described here, by configuring it like this:

var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    var host = cfg.Host(new Uri("rabbitmq://localhost/"), h =>
    {
        h.Username("guest");
        h.Password("guest");
    });

    cfg.UseInMemoryScheduler();
});

Upvotes: 2

Related Questions