Commit 6fb87588 by Alex Nasyr

activemq

parent 7faabd3d
......@@ -4,32 +4,44 @@ using Microsoft.Extensions.DependencyInjection;
using DDO_Application.Model;
using System;
using System.Threading.Tasks;
using System.Net;
namespace DDO_Application.Controllers {
[ApiController]
public class apiController : ControllerBase {
private readonly MessageProducer _messageProducer;
private IApiService _apiService;
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()) {
var uploadProcessingService =
scope.ServiceProvider
.GetRequiredService<IUploadService>();
_uploadService = uploadProcessingService;
.GetRequiredService<IApiService>();
_apiService = uploadProcessingService;
}
_messageProducer = messageProducer;
}
[HttpGet]
[Route("[controller]")]
public async Task<ActionResult<apiStatus>> Get() {
public async Task<ActionResult<apiStatus>> GetStatus() {
return new apiStatus();
}
[HttpGet]
[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 {
......
......@@ -5,6 +5,8 @@
</PropertyGroup>
<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" />
</ItemGroup>
......
......@@ -4,7 +4,7 @@ using System.Threading;
using System.Threading.Tasks;
namespace DDO_Application.Model {
public interface IUploadService{
public interface IApiService{
public int Counter { get; }
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",
"iisSettings": {
"windowsAuthentication": false,
......@@ -19,13 +19,23 @@
},
"DDO_Application": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"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;
using System.Threading.Tasks;
namespace DDO_Application.Services {
public class UploadHostedService : BackgroundService {
public class ApiHostedService : BackgroundService {
public IServiceProvider Services { get; }
public UploadHostedService(IServiceProvider services) {
public ApiHostedService(IServiceProvider services) {
Services = services;
}
......@@ -16,7 +16,7 @@ namespace DDO_Application.Services {
using (var scope = Services.CreateScope()) {
var uploadProcessingService =
scope.ServiceProvider
.GetRequiredService<IUploadService>();
.GetRequiredService<IApiService>();
await uploadProcessingService.DoWork(stoppingToken);
}
......
......@@ -5,11 +5,11 @@ using System.Threading;
using System.Threading.Tasks;
namespace DDO_Application.Services {
public class UploadProcessingService : IUploadService {
public class ApiProcessingService : IApiService {
private int _counter = 0;
public int Counter { get => _counter; }
public UploadProcessingService() {
public ApiProcessingService() {
}
public async Task DoWork(CancellationToken stoppingToken) {
......
......@@ -13,6 +13,9 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ActiveMQ.Artemis.Client.Extensions.DependencyInjection;
using ActiveMQ.Artemis.Client;
using ActiveMQ.Artemis.Client.Extensions.Hosting;
namespace DDO_Application {
public class Startup {
......@@ -27,8 +30,16 @@ namespace DDO_Application {
services.AddControllers();
services.AddSwaggerGen(c => {c.SwaggerDoc("v1", new OpenApiInfo { Title = "DDO_Application", Version = "v1" }); });
services.AddHostedService<UploadHostedService>();
services.AddSingleton<IUploadService, UploadProcessingService>();
services.AddHostedService<ApiHostedService>();
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