forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandlerFactory.cs
More file actions
62 lines (50 loc) · 2.25 KB
/
MessageHandlerFactory.cs
File metadata and controls
62 lines (50 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
namespace ServiceStack.Messaging
{
public class MessageHandlerFactory<T>
: IMessageHandlerFactory
{
public const int DefaultRetryCount = 2; //Will be a total of 3 attempts
private readonly IMessageService messageService;
public Func<IMessage, IMessage> RequestFilter { get; set; }
public Func<object, object> ResponseFilter { get; set; }
private readonly Func<IMessage<T>, object> processMessageFn;
private readonly Action<IMessage<T>, Exception> processExceptionFn;
public int RetryCount { get; set; }
public MessageHandlerFactory(IMessageService messageService, Func<IMessage<T>, object> processMessageFn)
: this(messageService, processMessageFn, null)
{
}
public MessageHandlerFactory(IMessageService messageService,
Func<IMessage<T>, object> processMessageFn,
Action<IMessage<T>, Exception> processExceptionEx)
{
if (messageService == null)
throw new ArgumentNullException("messageService");
if (processMessageFn == null)
throw new ArgumentNullException("processMessageFn");
this.messageService = messageService;
this.processMessageFn = processMessageFn;
this.processExceptionFn = processExceptionEx;
this.RetryCount = DefaultRetryCount;
}
public IMessageHandler CreateMessageHandler()
{
if (this.RequestFilter == null && this.ResponseFilter == null)
{
return new MessageHandler<T>(messageService, processMessageFn,
processExceptionFn, this.RetryCount);
}
return new MessageHandler<T>(messageService, msg =>
{
if (this.RequestFilter != null)
msg = (IMessage<T>) this.RequestFilter(msg);
var result = this.processMessageFn(msg);
if (this.ResponseFilter != null)
result = this.ResponseFilter(result);
return result;
},
processExceptionFn, this.RetryCount);
}
}
}