-
Notifications
You must be signed in to change notification settings - Fork 9
/
SingleOutputService.cs
44 lines (38 loc) · 1.44 KB
/
SingleOutputService.cs
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
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ConsumeAndPublishWithKafka.Model;
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;
namespace ConsumeAndPublishWithKafka;
public class SingleOutputService : ISingleOutputService<InputMessage, OutputMessage>
{
// Handle incoming messages
public Task<MotorCloudEvent<OutputMessage>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default)
{
// Get the input message from the cloud event
var input = inputEvent.TypedData;
// Do your magic here .....
var output = MagicFunc(input);
// Create a new cloud event from your output message which is automatically published and return a new task.
var outputEvent = inputEvent.CreateNew(output);
return Task.FromResult(outputEvent);
}
private static OutputMessage MagicFunc(InputMessage input)
{
if (string.IsNullOrEmpty(input.FancyText))
{
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}
var output = new OutputMessage
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
};
return output;
}
}