Skip to content

Commit b650d76

Browse files
committed
Improved extensibility of gateway to allow for dropping in an assembly which will be notified of messages processed by the gateway.
1 parent 1baf7ff commit b650d76

File tree

7 files changed

+60
-7
lines changed

7 files changed

+60
-7
lines changed

src/gateway/NServiceBus.Gateway/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<configuration>
33
<appSettings>
4-
<add key="NumberOfWorkerThreads" value="10"/>
4+
<add key="NumberOfWorkerThreads" value="1"/>
55

66
<add key="InputQueue" value="gateway"/>
77
<add key="ErrorQueue" value="error"/>

src/gateway/NServiceBus.Gateway/EndpointConfig.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Configuration;
22
using System.Net;
33
using System.Threading;
4+
using NServiceBus.Unicast.Transport;
45
using NServiceBus.Unicast.Transport.Msmq;
56

67
namespace NServiceBus.Gateway
@@ -36,9 +37,14 @@ public void Init()
3637
NumberOfWorkerThreads = numberOfWorkerThreads
3738
};
3839

40+
notifier = new MessageNotifier();
41+
42+
NServiceBus.Configure.Instance.Configurer.RegisterSingleton<ITransport>(transport);
43+
NServiceBus.Configure.Instance.Configurer.RegisterSingleton<INotifyAboutMessages>(notifier);
44+
3945
transport.TransportMessageReceived += (s, e) =>
4046
{
41-
new MsmqHandler(listenUrl).Handle(e.Message);
47+
new MsmqHandler(listenUrl, notifier).Handle(e.Message);
4248

4349
if (!string.IsNullOrEmpty(audit))
4450
transport.Send(e.Message, audit);
@@ -57,7 +63,7 @@ public void Run()
5763
{
5864
HttpListenerContext context = listener.GetContext();
5965
ThreadPool.QueueUserWorkItem(
60-
o => new HttpRequestHandler(requireMD5FromClient).Handle(o as HttpListenerContext, transport, outputQueue),
66+
o => new HttpRequestHandler(requireMD5FromClient, notifier).Handle(o as HttpListenerContext, transport, outputQueue),
6167
context);
6268
}
6369
}
@@ -69,6 +75,7 @@ public void Stop()
6975

7076
private static HttpListener listener;
7177
private static MsmqTransport transport;
78+
private static MessageNotifier notifier;
7279
private static bool requireMD5FromClient = true;
7380
private static string outputQueue;
7481

src/gateway/NServiceBus.Gateway/HttpRequestHandler.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88

99
namespace NServiceBus.Gateway
1010
{
11-
public class HttpRequestHandler
11+
internal class HttpRequestHandler
1212
{
1313
private const int maximumBytesToRead = 100000;
1414
private bool requireMD5FromClient = true;
15+
private readonly IMessageNotifier notifier;
1516

16-
public HttpRequestHandler(bool requireMD5)
17+
public HttpRequestHandler(bool requireMD5, IMessageNotifier notifier)
1718
{
1819
requireMD5FromClient = requireMD5;
20+
this.notifier = notifier;
1921
}
2022

2123
public void Handle(HttpListenerContext ctx, MsmqTransport transport, string queue)
@@ -96,6 +98,8 @@ public void Handle(HttpListenerContext ctx, MsmqTransport transport, string queu
9698
transport.Send(msg, header.Value);
9799
else
98100
transport.Send(msg, queue);
101+
102+
notifier.RaiseMessageProcessed(TransportTypeEnum.FromHttpToMsmq, msg);
99103
}
100104

101105
if (hash != null)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using NServiceBus.Unicast.Transport;
3+
4+
namespace NServiceBus.Gateway
5+
{
6+
public interface INotifyAboutMessages
7+
{
8+
event EventHandler<MessageTransportArgs> MessageProcessed;
9+
}
10+
11+
public class MessageTransportArgs : EventArgs
12+
{
13+
public TransportTypeEnum TransportType { get; set;}
14+
public TransportMessage Message { get; set; }
15+
}
16+
17+
public enum TransportTypeEnum { FromHttpToMsmq, FromMsmqToHttp }
18+
19+
internal interface IMessageNotifier : INotifyAboutMessages
20+
{
21+
void RaiseMessageProcessed(TransportTypeEnum transportType, TransportMessage message);
22+
}
23+
24+
internal class MessageNotifier : IMessageNotifier
25+
{
26+
public event EventHandler<MessageTransportArgs> MessageProcessed;
27+
28+
void IMessageNotifier.RaiseMessageProcessed(TransportTypeEnum transportType, TransportMessage message)
29+
{
30+
if (MessageProcessed != null)
31+
MessageProcessed(this, new MessageTransportArgs { TransportType = transportType, Message = message });
32+
}
33+
}
34+
}

src/gateway/NServiceBus.Gateway/MsmqHandler.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66
namespace NServiceBus.Gateway
77
{
8-
public class MsmqHandler
8+
internal class MsmqHandler
99
{
1010
private readonly string from;
11-
public MsmqHandler(string listenUrl)
11+
private readonly IMessageNotifier notifier;
12+
13+
public MsmqHandler(string listenUrl, IMessageNotifier notifier)
1214
{
1315
from = listenUrl;
16+
this.notifier = notifier;
1417
}
18+
1519
public void Handle(TransportMessage msg)
1620
{
1721
var header = msg.Headers.Find(h => h.Key == NServiceBus.Headers.HttpTo);
@@ -60,7 +64,10 @@ public void Handle(TransportMessage msg)
6064
}
6165

6266
if (md5 == hash)
67+
{
6368
Logger.Debug("Message transferred successfully.");
69+
notifier.RaiseMessageProcessed(TransportTypeEnum.FromMsmqToHttp, msg);
70+
}
6471
else
6572
{
6673
Logger.Info(Headers.ContentMd5Key + " header received from client not the same as that sent. Message not transferred successfully. Trying again...");

src/gateway/NServiceBus.Gateway/NServiceBus.Gateway.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="Hasher.cs" />
7070
<Compile Include="HeaderMapper.cs" />
7171
<Compile Include="HttpRequestHandler.cs" />
72+
<Compile Include="INotifyAboutMessages.cs" />
7273
<Compile Include="MsmqHandler.cs" />
7374
<Compile Include="Properties\AssemblyInfo.cs" />
7475
</ItemGroup>

src/gateway/gateway.suo

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)