Created
May 27, 2018 18:23
-
-
Save thiagoloureiro/a59d1c0fef81ab695e561eeeae22df6b to your computer and use it in GitHub Desktop.
This file contains 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 RabbitMq | |
{ | |
private ConnectionFactory _factory; | |
private IConnection _connection; | |
private IModel _channel; | |
private EventingBasicConsumer _consumer; | |
public string QueueName { get; set; } | |
public RabbitMq(string queuename) | |
{ | |
QueueName = queuename; | |
} | |
public void Connect() | |
{ | |
_factory = new ConnectionFactory() { HostName = "hound.rmq.cloudamqp.com", VirtualHost = "usldnewk", UserName = "usldnewk", Password = "-" }; | |
_connection = _factory.CreateConnection(); | |
_connection.ConnectionShutdown += Connection_ConnectionShutdown; | |
Console.WriteLine($"Declaring Queue: {QueueName}"); | |
_channel = _connection.CreateModel(); | |
_channel.QueueDeclare(QueueName, true, false, false, null); | |
_consumer = new EventingBasicConsumer(_channel); | |
_consumer.Received += Consumer_Received; | |
_channel.BasicConsume(QueueName, true, _consumer); | |
} | |
public void Disconnect() | |
{ | |
Cleanup(); | |
} | |
private void Connection_ConnectionShutdown(object sender, ShutdownEventArgs e) | |
{ | |
Console.WriteLine("Connection broke!"); | |
Cleanup(); | |
while (true) | |
{ | |
try | |
{ | |
Connect(); | |
Console.WriteLine("Reconnected!"); | |
break; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Reconnect failed! {ex.Message}"); | |
Thread.Sleep(3000); | |
} | |
} | |
} | |
private void Cleanup() | |
{ | |
Console.WriteLine("Cleaning Up RabbitMQ"); | |
try | |
{ | |
if (_channel != null && _channel.IsOpen) | |
{ | |
_channel.Close(); | |
_channel = null; | |
} | |
if (_connection != null && _connection.IsOpen) | |
{ | |
_connection.Close(); | |
_connection = null; | |
} | |
} | |
catch (IOException ex) | |
{ | |
Console.WriteLine($"Error: {ex.Message}"); | |
// Close() may throw an IOException if connection | |
// dies - but that's ok (handled by reconnect) | |
} | |
} | |
private void Consumer_Received(object sender, BasicDeliverEventArgs e) | |
{ | |
Console.WriteLine("Message Received!"); | |
var body = e.Body; | |
var content = Encoding.UTF8.GetString(body); | |
var objRootObject = JsonConvert.DeserializeObject<RootObject>(content); | |
WriteInfo(objRootObject); | |
} | |
private void WriteInfo(RootObject objRootObject) | |
{ | |
Console.WriteLine(objRootObject.eventName); | |
Console.WriteLine(objRootObject.eventData.status); | |
Console.WriteLine(objRootObject.eventData.branch); | |
Console.WriteLine(objRootObject.eventData.duration); | |
Console.WriteLine(objRootObject.eventData.repositoryName); | |
Console.WriteLine(objRootObject.eventData.passed); | |
Console.WriteLine(objRootObject.eventData.commitAuthor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment