Commit 6fb87588 by Alex Nasyr

activemq

parent 7faabd3d
...@@ -4,32 +4,44 @@ using Microsoft.Extensions.DependencyInjection; ...@@ -4,32 +4,44 @@ using Microsoft.Extensions.DependencyInjection;
using DDO_Application.Model; using DDO_Application.Model;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net;
namespace DDO_Application.Controllers { namespace DDO_Application.Controllers {
[ApiController] [ApiController]
public class apiController : ControllerBase { public class apiController : ControllerBase {
private readonly MessageProducer _messageProducer;
private IApiService _apiService;
public IServiceProvider Services { get; } public IServiceProvider Services { get; }
private IUploadService _uploadService;
public apiController(IServiceProvider services) {
Services = services;
public apiController(IServiceProvider services, MessageProducer messageProducer) {
Services = services;
using (var scope = Services.CreateScope()) { using (var scope = Services.CreateScope()) {
var uploadProcessingService = var uploadProcessingService =
scope.ServiceProvider scope.ServiceProvider
.GetRequiredService<IUploadService>(); .GetRequiredService<IApiService>();
_uploadService = uploadProcessingService; _apiService = uploadProcessingService;
} }
_messageProducer = messageProducer;
} }
[HttpGet] [HttpGet]
[Route("[controller]")] [Route("[controller]")]
public async Task<ActionResult<apiStatus>> Get() { public async Task<ActionResult<apiStatus>> GetStatus() {
return new apiStatus(); return new apiStatus();
} }
[HttpGet] [HttpGet]
[Route("[controller]/lifetime")] [Route("[controller]/lifetime")]
public int GetLifetime() => _uploadService.Counter; public int GetLifetime() => _apiService.Counter;
[HttpPost]
[Route("[controller]/sendmsg")]
public async Task<IActionResult> SendMessage(string msg) {
await _messageProducer.PublishAsync(msg);
return StatusCode((int)HttpStatusCode.Created, null);
}
} }
public class apiStatus { public class apiStatus {
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ArtemisNetClient.Extensions.DependencyInjection" Version="2.6.0" />
<PackageReference Include="ArtemisNetClient.Extensions.Hosting" Version="2.6.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup> </ItemGroup>
......
...@@ -4,7 +4,7 @@ using System.Threading; ...@@ -4,7 +4,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DDO_Application.Model { namespace DDO_Application.Model {
public interface IUploadService{ public interface IApiService{
public int Counter { get; } public int Counter { get; }
Task DoWork(CancellationToken stoppingToken); Task DoWork(CancellationToken stoppingToken);
} }
......
using ActiveMQ.Artemis.Client;
using System.Text.Json;
using System.Threading.Tasks;
namespace DDO_Application.Model {
public class MessageProducer {
private readonly IAnonymousProducer _producer;
public MessageProducer(IAnonymousProducer producer) {
_producer = producer;
}
public async Task PublishAsync<T>(T message) {
var serialized = JsonSerializer.Serialize(message);
var address = typeof(T).Name;
var msg = new Message(serialized);
await _producer.SendAsync(address, msg);
}
}
}
{ {
"$schema": "http://json.schemastore.org/launchsettings.json", "$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": { "iisSettings": {
"windowsAuthentication": false, "windowsAuthentication": false,
...@@ -19,13 +19,23 @@ ...@@ -19,13 +19,23 @@
}, },
"DDO_Application": { "DDO_Application": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} },
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"dotnetRunMessages": "true"
},
"WSL": {
"commandName": "WSL2",
"launchBrowser": true,
"launchUrl": "https://localhost:5001/swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "https://localhost:5001;http://localhost:5000"
},
"distributionName": ""
} }
} }
} }
\ No newline at end of file
...@@ -6,9 +6,9 @@ using System.Threading; ...@@ -6,9 +6,9 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DDO_Application.Services { namespace DDO_Application.Services {
public class UploadHostedService : BackgroundService { public class ApiHostedService : BackgroundService {
public IServiceProvider Services { get; } public IServiceProvider Services { get; }
public UploadHostedService(IServiceProvider services) { public ApiHostedService(IServiceProvider services) {
Services = services; Services = services;
} }
...@@ -16,7 +16,7 @@ namespace DDO_Application.Services { ...@@ -16,7 +16,7 @@ namespace DDO_Application.Services {
using (var scope = Services.CreateScope()) { using (var scope = Services.CreateScope()) {
var uploadProcessingService = var uploadProcessingService =
scope.ServiceProvider scope.ServiceProvider
.GetRequiredService<IUploadService>(); .GetRequiredService<IApiService>();
await uploadProcessingService.DoWork(stoppingToken); await uploadProcessingService.DoWork(stoppingToken);
} }
......
...@@ -5,11 +5,11 @@ using System.Threading; ...@@ -5,11 +5,11 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DDO_Application.Services { namespace DDO_Application.Services {
public class UploadProcessingService : IUploadService { public class ApiProcessingService : IApiService {
private int _counter = 0; private int _counter = 0;
public int Counter { get => _counter; } public int Counter { get => _counter; }
public UploadProcessingService() { public ApiProcessingService() {
} }
public async Task DoWork(CancellationToken stoppingToken) { public async Task DoWork(CancellationToken stoppingToken) {
......
...@@ -13,6 +13,9 @@ using System; ...@@ -13,6 +13,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using ActiveMQ.Artemis.Client.Extensions.DependencyInjection;
using ActiveMQ.Artemis.Client;
using ActiveMQ.Artemis.Client.Extensions.Hosting;
namespace DDO_Application { namespace DDO_Application {
public class Startup { public class Startup {
...@@ -27,8 +30,16 @@ namespace DDO_Application { ...@@ -27,8 +30,16 @@ namespace DDO_Application {
services.AddControllers(); services.AddControllers();
services.AddSwaggerGen(c => {c.SwaggerDoc("v1", new OpenApiInfo { Title = "DDO_Application", Version = "v1" }); }); services.AddSwaggerGen(c => {c.SwaggerDoc("v1", new OpenApiInfo { Title = "DDO_Application", Version = "v1" }); });
services.AddHostedService<UploadHostedService>(); services.AddHostedService<ApiHostedService>();
services.AddSingleton<IUploadService, UploadProcessingService>(); services.AddSingleton<IApiService, ApiProcessingService>();
// turn enable ActiveMQ support in project
services.AddActiveMq("ddoApp-cluster", new[] { Endpoint.Create(host: "localhost", port: 5672, "guest", "guest") })
.AddAnonymousProducer<MessageProducer>()
.AddConsumer("BookUpdated", RoutingType.Multicast, async (message, consumer, serviceProvider, cancellationToken) => {
// your consuming logic
await consumer.AcceptAsync(message);
});
services.AddActiveMqHostedService();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment