Last active
December 28, 2015 02:09
-
-
Save agilejon/7425629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Publisher { | |
private IModel _model; | |
public Publisher(IConnection conn, string queueName) { | |
_model = conn.CreateModel(); | |
// it's safe to declare queues that already exist as long as their properties are identical | |
// this runs on initialization every time - that will ensure all of your queues exist on app startup | |
model.QueueDeclare(queueName, true, false, false, null); | |
// binds the given queue to the routingKey. We use the same string as both the queue name and routing key | |
model.QueueBind(queueName, "amq.direct", queueName); | |
} | |
// serialize your message body however you want. JSON is nice because it's makes debugging a bit easier. | |
public void PublishMessage(string routingKey, byte[] body) | |
{ | |
var props = _model.CreateBasicProperties(); | |
// this is important, it tells the queue to make the message delivery 'durable' | |
props.DeliveryMode = 2; | |
if (props.Headers == null) | |
{ | |
props.Headers = new Dictionary<string, object>(); | |
} | |
_model.BasicPublish("amq.direct", routingKey, props, body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment