Skip to content

Commit 3f0b9b7

Browse files
author
Ethan Hann
committed
Merge branch 'master' into develop
2 parents a46c145 + 3fc4412 commit 3f0b9b7

File tree

6 files changed

+48
-27
lines changed

6 files changed

+48
-27
lines changed

IntegrationEngine.ConsoleHost/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ public static void Main(string[] args)
2222
{
2323
Start(args);
2424
Console.WriteLine("Press any key to stop...");
25-
Console.ReadKey(true);
25+
Console.ReadLine();
2626
Stop();
2727
}
2828
}
2929

30-
3130
private static void Start(string[] args)
3231
{
3332
EngineHosts = new EngineHost(typeof(Program).Assembly);
3433
EngineHosts.Initialize();
3534
}
3635

3736
private static void Stop()
38-
{}
37+
{
38+
EngineHosts.Dispose();
39+
}
3940

4041
public class Service : ServiceBase
4142
{

IntegrationEngine/EngineHost.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace IntegrationEngine
77
{
8-
public class EngineHost
8+
public class EngineHost : IDisposable
99
{
1010
EngineHostConfiguration _engineConfiguration;
1111
public IList<Assembly> AssembliesWithJobs { get; set; }
@@ -15,10 +15,10 @@ public EngineHost(params Assembly[] assembliesWithJobs)
1515
AssembliesWithJobs = assembliesWithJobs.ToList();
1616
}
1717

18-
~EngineHost()
18+
public void Dispose()
1919
{
20-
if (_engineConfiguration == null)
21-
_engineConfiguration.Shutdown();
20+
if (_engineConfiguration != null)
21+
_engineConfiguration.Dispose();
2222
}
2323

2424
public void Initialize()

IntegrationEngine/EngineHostConfiguration.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace IntegrationEngine
2222
{
23-
public class EngineHostConfiguration
23+
public class EngineHostConfiguration : IDisposable
2424
{
2525
public IUnityContainer Container { get; set; }
2626
public EngineConfiguration Configuration { get; set; }
@@ -113,6 +113,7 @@ public void SetupMessageQueueListener()
113113
IntegrationEngineContext = Container.Resolve<IntegrationEngineContext>(),
114114
ElasticClient = Container.Resolve<IElasticClient>(),
115115
};
116+
Container.RegisterInstance<IMessageQueueListener>(rabbitMqListener);
116117
rabbitMqListener.Listen();
117118
}
118119

@@ -176,10 +177,12 @@ public void SetupElasticClient()
176177
});
177178
}
178179

179-
public void Shutdown()
180+
public void Dispose()
180181
{
181182
var engineScheduler = Container.Resolve<IEngineScheduler>();
182183
engineScheduler.Shutdown();
184+
var messageQueueListener = Container.Resolve<IMessageQueueListener>();
185+
messageQueueListener.Dispose();
183186
}
184187
}
185188
}

IntegrationEngine/MessageQueue/IMessageQueueListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace IntegrationEngine.MessageQueue
88
{
9-
public interface IMessageQueueListener
9+
public interface IMessageQueueListener : IDisposable
1010
{
1111
void Listen();
1212
}

IntegrationEngine/MessageQueue/MsmqListener.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public void Listen()
5454
}
5555
}
5656

57+
public void Dispose()
58+
{
59+
throw new NotImplementedException();
60+
}
5761
T AutoWireJob<T>(T job, Type type)
5862
{
5963
if (type.GetInterface(typeof(IMailJob).Name) != null)

IntegrationEngine/MessageQueue/RabbitMQListener.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,26 @@
99
using RabbitMQ.Client.Events;
1010
using System;
1111
using System.Collections.Generic;
12+
using System.IO;
1213
using System.Linq;
1314
using System.Text;
1415
using System.Threading;
1516

1617
namespace IntegrationEngine.MessageQueue
1718
{
18-
public class RabbitMQListener : IMessageQueueListener, IDisposable
19+
public class RabbitMQListener : IMessageQueueListener
1920
{
2021
Thread listenerThread;
21-
bool shouldTerminate;
22+
volatile bool shouldTerminate;
23+
QueueingBasicConsumer consumer;
2224
public IList<Type> IntegrationJobTypes { get; set; }
2325
public MessageQueueConfiguration MessageQueueConfiguration { get; set; }
2426
public MessageQueueConnection MessageQueueConnection { get; set; }
2527
public ILog Log { get; set; }
2628
public IMailClient MailClient { get; set; }
2729
public IntegrationEngineContext IntegrationEngineContext { get; set; }
2830
public IElasticClient ElasticClient { get; set; }
29-
31+
3032
public RabbitMQListener()
3133
{
3234
shouldTerminate = false;
@@ -35,6 +37,8 @@ public RabbitMQListener()
3537
public void Dispose()
3638
{
3739
shouldTerminate = true;
40+
if (consumer != null)
41+
consumer.Queue.Close();
3842
listenerThread.Join();
3943
}
4044

@@ -43,35 +47,44 @@ void _listen()
4347
var connection = MessageQueueConnection.GetConnection();
4448
using (var channel = connection.CreateModel())
4549
{
46-
var consumer = new QueueingBasicConsumer(channel);
50+
consumer = new QueueingBasicConsumer(channel);
4751
channel.BasicConsume(MessageQueueConfiguration.QueueName, true, consumer);
48-
4952
Log.Info(x => x("Waiting for messages..."));
53+
5054
while (true)
5155
{
52-
if (shouldTerminate)
53-
return;
54-
var eventArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
55-
var body = eventArgs.Body;
56-
var message = JsonConvert.DeserializeObject<DispatchMessage>(Encoding.UTF8.GetString(body));
57-
Log.Debug(x => x("Message queue listener received {0}", message));
58-
if (IntegrationJobTypes != null && !IntegrationJobTypes.Any())
59-
continue;
60-
var type = IntegrationJobTypes.FirstOrDefault(t => t.FullName.Equals(message.JobTypeName));
61-
var integrationJob = Activator.CreateInstance(type) as IIntegrationJob;
62-
integrationJob = AutoWireJob(integrationJob, type);
56+
var message = new DispatchMessage();
6357
try
6458
{
59+
if (shouldTerminate)
60+
{
61+
connection.Close();
62+
Log.Info("Message queue listener has stopped listening for messages.");
63+
return;
64+
}
65+
var eventArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
66+
var body = eventArgs.Body;
67+
message = JsonConvert.DeserializeObject<DispatchMessage>(Encoding.UTF8.GetString(body));
68+
Log.Debug(x => x("Message queue listener received {0}", message));
69+
if (IntegrationJobTypes != null && !IntegrationJobTypes.Any())
70+
continue;
71+
var type = IntegrationJobTypes.FirstOrDefault(t => t.FullName.Equals(message.JobTypeName));
72+
var integrationJob = Activator.CreateInstance(type) as IIntegrationJob;
73+
integrationJob = AutoWireJob(integrationJob, type);
6574
if (integrationJob != null)
6675
{
6776
if (integrationJob.GetType() is IParameterizedJob)
6877
(integrationJob as IParameterizedJob).Parameters = message.Parameters;
6978
integrationJob.Run();
7079
}
7180
}
81+
catch (EndOfStreamException exception)
82+
{
83+
Log.Debug(x => x("The message queue ({0}) has closed.", MessageQueueConfiguration.QueueName), exception);
84+
}
7285
catch (Exception exception)
7386
{
74-
Log.Error(x => x("Integration job did not run successfully ({0})}", message), exception);
87+
Log.Error(x => x("Integration job did not run successfully ({0})}", message.JobTypeName), exception);
7588
}
7689
}
7790
}

0 commit comments

Comments
 (0)