create queue and set priority before publishing using masstransit rabbitmq in .net core

555 views
Skip to first unread message

muhamm...@datamagnetics.com

unread,
Jul 13, 2018, 11:32:59 AM7/13/18
to masstransit-discuss
create queue and set priority before publishing using masstransit rabbitmq in .net core.

Code in startup file.

builder.Register(c => {
                
                return Bus.Factory.CreateUsingRabbitMq(sbc =>
                {
                    sbc.Host(new Uri(ConfigurationManager.AppSettings["RabbitMQHost"]), h =>
                    {
                        h.Username(ConfigurationManager.AppSettings["RabbitMQHostUsername"]);
                        h.Password(ConfigurationManager.AppSettings["RabbitMQHostPassword"]);
                    });
                    sbc.EnablePriority(6);
                });

            })
                .As<IBusControl>()
                .As<IBus>()
                .SingleInstance();

Code in my controller class

await _bus.Publish<MyEvent>(new
                    {
                        Message = string.Format("Event - {0}", i)
                    });

Chris Patterson

unread,
Jul 13, 2018, 5:21:07 PM7/13/18
to masstrans...@googlegroups.com
Is there a question?

FWIW, there isn't a receive endpoint specified in this configuration, so I'm not sure what EnablePriority is going to do for you.

If you want to set the priority of a message, you need to do so in the context of the message, via:

_bus.Publish<MyEvent>(new{}, context => context.SetPriority(6));



--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/7f287fc1-ec6e-43a8-8ec9-0c77e85dfab5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

muhammad...@gmail.com

unread,
Jul 14, 2018, 3:21:30 AM7/14/18
to masstransit-discuss
Hi,
Thank you for your response. yes, it was my question.

Is it necessary to specify the receive endpoint in the publishing side, I am not talking about only for EnablePriority I am talking about that what is the best practice for receive end point?

last thing I have tried the same thing for message priority but its giving me error, my code is not compiling. what I am missing?

I have used.
_bus.Publish<MyEvent>(new{}, context => context.SetPriority(6));


CS1061 'PublishContext<MyEvent>' does not contain a definition for 'SetPriority' and no extension method 'SetPriority' accepting a first argument of type 'PublishContext<MyEvent>' could be found (are you missing a using directive or an assembly reference?)


Thanks,
To post to this group, send email to masstrans...@googlegroups.com.

Chris Patterson

unread,
Jul 14, 2018, 8:54:54 AM7/14/18
to masstrans...@googlegroups.com
What version of MassTransit are you using?

And receive endpoints should only be configured on the consumer side, not the publisher.


To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

muhammad...@gmail.com

unread,
Jul 14, 2018, 9:14:45 AM7/14/18
to masstransit-discuss
I am using:
MassTransit 5.1.3
MassTransit.Autofac 5.1.3
MassTransit.RabbitMQ 5.1.3
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

Chris Patterson

unread,
Jul 14, 2018, 9:23:53 AM7/14/18
to masstrans...@googlegroups.com
It's there, not sure why you are having the problem.

[Test]
public async Task Should_allow_priority_to_be_specified()
{
await Bus.Publish<PingMessage>(new PingMessage(), x => x.SetPriority(6));
}
Compiles for me in this unit test when I change the line:



To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

muhammad...@gmail.com

unread,
Jul 14, 2018, 9:50:22 AM7/14/18
to masstransit-discuss
Thank you, Its working now. :)

await _bus.Publish<EmailMessage>(new {}, x => x.SetPriority(3));
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

muhammad...@gmail.com

unread,
Jul 14, 2018, 10:01:50 AM7/14/18
to masstransit-discuss
Is there any option to set the queue name while publishing?

await _bus.Publish<EmailMessage>(new {}, x => x.SetPriority(3));

Chris Patterson

unread,
Jul 14, 2018, 9:17:31 PM7/14/18
to masstrans...@googlegroups.com
That would be _sending_ and not publishing. And you can call GetSendEndpoint(...) with an address, and then call Send().


To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

muhammad...@gmail.com

unread,
Jul 15, 2018, 1:16:56 PM7/15/18
to masstransit-discuss
Thank you, I got it. :)
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

muhamm...@datamagnetics.com

unread,
Jul 16, 2018, 6:03:23 AM7/16/18
to masstransit-discuss
Message priority is not working. what I am missing?
Code on publisher side:

builder.Register(c =>
            {
                return Bus.Factory.CreateUsingRabbitMq(sbc =>
                {
                    var host = sbc.Host(new Uri("rabbitmq://rabbitmqserver/"), h =>
                    {
                        h.Username("devuser");
                        h.Password("devuser");
                    });
                    sbc.EnablePriority(10);
                });
            })
                .As<IBusControl>()
                .As<IBus>()
                .As<IPublishEndpoint>()
                .SingleInstance();


for (int i = 1; i <= model.EventCount; i++)
                    await _bus.Publish(new MyMessageEvent
                    {
                        Message = $"{model.Message} {i}"
                    }, context => context.SetPriority(model.Priority));


Code on consumer side:
builder.Register(context =>
                {
                    var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
                    {
                        var host = cfg.Host(new Uri("rabbitmq://rabbitmqserver/"), h =>
                        {
                            h.Username("devuser");
                            h.Password("devuser");
                        });

                        cfg.EnablePriority(10);

                        cfg.ReceiveEndpoint(host, "my_message_queue", e =>
                        {
                            e.LoadFrom(context);
                            e.PrefetchCount = 1;
                            //e.Consumer<ApplicantAppliedConsumer>();
                        });
                    });

                    return busControl;
                })
                .SingleInstance()
                .As<IBusControl>()
                .As<IBus>();


public async Task Consume(ConsumeContext<MyMessageEvent> context)
        {
            System.Threading.Thread.Sleep(5000);
            Console.Write(context.Message.Message);
            Logging.WriteTextAsync(context.Message.Message);
        }

muhamm...@datamagnetics.com

unread,
Jul 16, 2018, 8:22:38 AM7/16/18
to masstransit-discuss
RabbitMQ.Client.Exceptions.OperationInterruptedException: 'The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text="PRECONDITION_FAILED - inequivalent arg 'x-max-priority' for queue 'my_message_queue' in vhost '/': received the value '1' of type 'signedint' but current is none", classId=50, methodId=10, cause='

muhamm...@datamagnetics.com

unread,
Jul 16, 2018, 8:53:13 AM7/16/18
to masstransit-discuss
Last exception is due to enablePriority in receiver configuration.
cfg.ReceiveEndpoint(host, "my_message_queue", e =>
                        {
                            e.LoadFrom(context);
                            //e.PrefetchCount = 1;
                            e.EnablePriority(1);
                            //e.Consumer<ApplicantAppliedConsumer>();
                        });

Alexey Zimarev

unread,
Jul 16, 2018, 4:06:54 PM7/16/18
to masstransit-discuss
Maybe you can first explain what are you trying to do?

muhammad...@gmail.com

unread,
Jul 17, 2018, 12:36:15 AM7/17/18
to masstransit-discuss
Hi, 
I want to send messages with different priority to the same queue and want to consume high priority messages first. My goal is to utilize the priority how I can achieve that?

Chris Patterson

unread,
Jul 17, 2018, 11:44:12 AM7/17/18
to masstrans...@googlegroups.com
If you're getting errors from the broker after getting your configuration right (setting priority on the receive endpoint vs the bus), delete the exchanges and queues from the broker and start your service again - it will recreate them properly.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

Muhammad Lall khan

unread,
Jul 17, 2018, 11:47:23 AM7/17/18
to masstrans...@googlegroups.com
is it accessory to EnablePriority on receiver or publisher end?

On Tue, Jul 17, 2018 at 8:43 PM, Chris Patterson <ch...@phatboyg.com> wrote:
If you're getting errors from the broker after getting your configuration right (setting priority on the receive endpoint vs the bus), delete the exchanges and queues from the broker and start your service again - it will recreate them properly.

On Mon, Jul 16, 2018 at 11:36 PM, <muhammad...@gmail.com> wrote:
Hi, 
I want to send messages with different priority to the same queue and want to consume high priority messages first. My goal is to utilize the priority how I can achieve that?

On Tuesday, July 17, 2018 at 1:06:54 AM UTC+5, Alexey Zimarev wrote:
Maybe you can first explain what are you trying to do?

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

To post to this group, send email to masstransit-discuss@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Best Regards,

Muhammad Lall
Senior Software Engineer | Data Magnetics | Muhamm...@datamagnetics.com
441-Q, Iqra Street, Model Town, Lahore


Chris Patterson

unread,
Jul 17, 2018, 11:01:19 PM7/17/18
to masstrans...@googlegroups.com
It's a queue thing, so it's only on the receiver.


On Tue, Jul 17, 2018 at 10:47 AM, Muhammad Lall khan <muhamm...@datamagnetics.com> wrote:
is it accessory to EnablePriority on receiver or publisher end?
On Tue, Jul 17, 2018 at 8:43 PM, Chris Patterson <ch...@phatboyg.com> wrote:
If you're getting errors from the broker after getting your configuration right (setting priority on the receive endpoint vs the bus), delete the exchanges and queues from the broker and start your service again - it will recreate them properly.


On Mon, Jul 16, 2018 at 11:36 PM, <muhammad...@gmail.com> wrote:
Hi, 
I want to send messages with different priority to the same queue and want to consume high priority messages first. My goal is to utilize the priority how I can achieve that?

On Tuesday, July 17, 2018 at 1:06:54 AM UTC+5, Alexey Zimarev wrote:
Maybe you can first explain what are you trying to do?

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/bbdc226d-5cc2-4c8d-bf32-e12338e76706%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.



--

Best Regards,

Muhammad Lall
Senior Software Engineer | Data Magnetics | Muhammad.lall@datamagnetics.com
441-Q, Iqra Street, Model Town, Lahore


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.

muhamm...@datamagnetics.com

unread,
Jul 18, 2018, 3:48:34 AM7/18/18
to masstransit-discuss
Thank you chris


On Wednesday, July 18, 2018 at 8:01:19 AM UTC+5, Chris Patterson wrote:
It's a queue thing, so it's only on the receiver.

On Tue, Jul 17, 2018 at 10:47 AM, Muhammad Lall khan <muhamm...@datamagnetics.com> wrote:
is it accessory to EnablePriority on receiver or publisher end?
On Tue, Jul 17, 2018 at 8:43 PM, Chris Patterson <ch...@phatboyg.com> wrote:
If you're getting errors from the broker after getting your configuration right (setting priority on the receive endpoint vs the bus), delete the exchanges and queues from the broker and start your service again - it will recreate them properly.

On Mon, Jul 16, 2018 at 11:36 PM, <muhammad...@gmail.com> wrote:
Hi, 
I want to send messages with different priority to the same queue and want to consume high priority messages first. My goal is to utilize the priority how I can achieve that?

On Tuesday, July 17, 2018 at 1:06:54 AM UTC+5, Alexey Zimarev wrote:
Maybe you can first explain what are you trying to do?

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.



--

Best Regards,

Muhammad Lall
Senior Software Engineer | Data Magnetics | Muhamm...@datamagnetics.com
441-Q, Iqra Street, Model Town, Lahore


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages