-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgram.cs
More file actions
51 lines (39 loc) · 1.46 KB
/
Program.cs
File metadata and controls
51 lines (39 loc) · 1.46 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
using System.Threading;
using System.Threading.Tasks;
using ActiveMQ.Artemis.Client.Extensions.DependencyInjection;
using ActiveMQ.Artemis.Client;
using ActiveMQ.Artemis.Client.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddActiveMqHostedService();
var endpoint = Endpoint.Create(
host: "localhost",
port: 5699,
"artemis",
"artemis"
);
var activeMqBuilder = builder.Services.AddActiveMq(name: "my-artemis", endpoints: new[] {endpoint});
activeMqBuilder.AddConsumer(address: "foo", routingType: RoutingType.Anycast,
handler: async (message, consumer, serviceProvider, cancellationToken) =>
{
var body = message.GetBody<string>();
var bar = body + "-" + "bar";
var producer = serviceProvider.GetRequiredService<MyProducer>();
await producer.Publish(bar, cancellationToken);
await consumer.AcceptAsync(message);
});
activeMqBuilder.AddProducer<MyProducer>("bar", RoutingType.Multicast);
var app = builder.Build();
app.Run();
public partial class Program { }
public class MyProducer
{
private readonly IProducer _producer;
public MyProducer(IProducer producer) => _producer = producer;
public async Task Publish(string payload, CancellationToken cancellationToken)
{
var message = new Message(payload);
await _producer.SendAsync(message, cancellationToken);
}
}