-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathStartup.cs
More file actions
99 lines (90 loc) · 4.72 KB
/
Startup.cs
File metadata and controls
99 lines (90 loc) · 4.72 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Globalization;
using ActiveMQ.Artemis.Client.AutoRecovering.RecoveryPolicy;
using ActiveMQ.Artemis.Client.MessageIdPolicy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ActiveMQ.Artemis.Client.Extensions.DependencyInjection;
using ActiveMQ.Artemis.Client.Extensions.Hosting;
using ActiveMQ.Artemis.Client.Extensions.HealthChecks;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
namespace ActiveMQ.Artemis.Client.Examples.AspNetCore
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var activeMqBuilder = services.AddActiveMq(name: "my-artemis-cluster", endpoints: new[] { Endpoint.Create(host: "localhost", port: 5672, "artemis", "artemis"), Endpoint.Create(host: "localhost", port: 5673, "artemis", "artemis") })
.ConfigureConnectionFactory((provider, factory) =>
{
factory.LoggerFactory = provider.GetService<ILoggerFactory>();
factory.RecoveryPolicy = RecoveryPolicyFactory.ExponentialBackoff(initialDelay: TimeSpan.FromSeconds(1), maxDelay: TimeSpan.FromSeconds(30), retryCount: 5);
factory.MessageIdPolicyFactory = MessageIdPolicyFactory.GuidMessageIdPolicy;
factory.AutomaticRecoveryEnabled = true;
factory.TCP.KeepAliveTime = 1000 * 30; // 30 seconds
factory.TCP.KeepAliveInterval = 1000; // 1 seconds
})
.ConfigureConnection((_, connection) =>
{
connection.ConnectionClosed += (_, args) =>
{
Console.WriteLine($"Connection closed: ClosedByPeer={args.ClosedByPeer}, Error={args.Error}");
};
connection.ConnectionRecovered += (_, args) =>
{
Console.WriteLine($"Connection recovered: Endpoint={args.Endpoint}");
};
connection.ConnectionRecoveryError += (_, args) =>
{
Console.WriteLine($"Connection recovered error: Exception={args.Exception}");
};
})
.AddConsumer("a1", RoutingType.Multicast, "q1", async (message, consumer, serviceProvider, token) =>
{
Console.WriteLine("q1: " + message.GetBody<string>());
await consumer.AcceptAsync(message);
})
.AddConsumer("a1", RoutingType.Multicast, "q2", async (message, consumer, serviceProvider, token) =>
{
Console.WriteLine("q2: " + message.GetBody<string>());
await consumer.AcceptAsync(message);
})
.AddProducer<MyTypedMessageProducer>("a1", RoutingType.Multicast)
.EnableQueueDeclaration()
.EnableAddressDeclaration();
services.AddActiveMqHostedService();
// Add health checks for ActiveMQ Artemis
services
.AddHealthChecks()
.AddActiveMq(name: "my-artemis-cluster", activeMqBuilder: activeMqBuilder, tags: new[] { "activemq" });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseHealthChecks("/health", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("activemq")
});
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
var messageProducer = context.RequestServices.GetRequiredService<MyTypedMessageProducer>();
await messageProducer.SendTextAsync(DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}