DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • AWS Redshift Data Sharing: Unlocking the Power of Collaborative Analytics
  • Why Haven’t You Upgraded to HTTP/2?
  • Understanding the Integration of Embedded Systems in Consumer Electronics

Trending

  • Spring AI on Local LLMs
  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

EasyNetQ: Multiple Handlers per Consumer

By 
Mike Hadlow
·
Dec. 01, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

A common feature request for EasyNetQ has been to have some way of implementing a command pipeline pattern. Say you’ve got a component that is emitting commands. In a .NET application each command would most probably be implemented as a separate class. A command might look something like this:

public class AddUser
{
    public string Username { get; private set; }
    public string Email { get; private set; }

    public AddUser(string username, string email) 
    {
        Username = username;
        Email = email;
    }
}

Another component might listen for commands and act on them. Previously in EasyNetQ it would have been difficult to implement this pattern because a consumer (Subscriber) was always bound to a given message type. You would have had to use the lower level IAdvancedBus binary message methods and implement your own serialization and dispatch infrastructure.

But now EasyNetQ comes with multiple handlers per consumer out of the box.

From version 0.20 there’s a new overload of the Consume method that provides a fluent way for you to register multiple message type handlers to a single consumer, and thus a single queue.

Here’s an example:

bus = RabbitHutch.CreateBus("host=localhost");

var queue = bus.Advanced.QueueDeclare("multiple_types");

bus.Advanced.Consume(queue, x => x
        .Add<AddUser>((message, info) => 
            { 
                Console.WriteLine("Add User {0}", message.Body.Username);
            })
        .Add<DeleteUser>((message, info) =>
            {
                Console.WriteLine("Delete User {0}", message.Body.Username);
            })
    );

Now we can publish multiple message types to the same queue:

bus.Advanced.Publish(Exchange.GetDefault(), queue.Name, false, false, 
    new Message<AddUser>(new AddUser("Steve Howe", "[email protected]"))));
bus.Advanced.Publish(Exchange.GetDefault(), queue.Name, false, false, 
    new Message<DeleteUser>(new DeleteUser("Steve Howe")));

By Default, if a matching handler cannot be found for a message, EasyNetQ will throw an exception. You can change this behaviour, and simply ignore messages that do not have a handler, by setting the ThrowOnNoMatchingHandler property to false, like this:

bus.Advanced.Consume(queue, x => x
        .Add<AddUser>((message, info) => 
            { 
                Console.WriteLine("Add User {0}", message.Body.Username);
            })
        .Add<DeleteUser>((message, info) =>
            {
                Console.WriteLine("Delete User {0}", message.Body.Username);
            })
        .ThrowOnNoMatchingHandler = false
    );

Very soon there will be a send/receive pattern implemented at the IBus level to make this even easier. Watch this space!

Happy commanding!

consumer

Published at DZone with permission of Mike Hadlow. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • AWS Redshift Data Sharing: Unlocking the Power of Collaborative Analytics
  • Why Haven’t You Upgraded to HTTP/2?
  • Understanding the Integration of Embedded Systems in Consumer Electronics

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook