Commit 56ecccb3 by AlexNasyr

produser config obj injected

parent 9eaadc67
...@@ -6,18 +6,18 @@ using System; ...@@ -6,18 +6,18 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net; using System.Net;
using ActiveMQ.Artemis.Client.Transactions; using ActiveMQ.Artemis.Client.Transactions;
using Microsoft.Extensions.Configuration;
namespace DDO_Application.Controllers { namespace DDO_Application.Controllers {
[ApiController] [ApiController]
public class apiController : ControllerBase { public class apiController : ControllerBase {
private readonly MessageProducer _messageProducer; private readonly MessageProducer _messageProducer;
private readonly MessageCunsomer _messageCunsomer;
private IApiService _apiService; private IApiService _apiService;
public IServiceProvider Services { get; } public IServiceProvider Services { get; }
public apiController(IServiceProvider services, MessageProducer messageProducer, MessageCunsomer messageCunsomer) { public apiController(IServiceProvider services, MessageProducer messageProducer) {
Services = services; Services = services;
using (var scope = Services.CreateScope()) { using (var scope = Services.CreateScope()) {
...@@ -27,14 +27,12 @@ namespace DDO_Application.Controllers { ...@@ -27,14 +27,12 @@ namespace DDO_Application.Controllers {
_apiService = uploadProcessingService; _apiService = uploadProcessingService;
} }
_messageProducer = messageProducer; _messageProducer = messageProducer;
_messageCunsomer = messageCunsomer;
} }
[HttpGet] [HttpGet]
[Route("[controller]")] [Route("[controller]")]
public async Task<ActionResult<apiStatus>> GetStatus() { public async Task<ActionResult<apiStatus>> GetStatus() => new apiStatus();
return new apiStatus();
}
[HttpGet] [HttpGet]
[Route("[controller]/lifetime")] [Route("[controller]/lifetime")]
public int GetLifetime() => _apiService.Counter; public int GetLifetime() => _apiService.Counter;
......
...@@ -15,7 +15,6 @@ namespace DDO_Application.Model { ...@@ -15,7 +15,6 @@ namespace DDO_Application.Model {
builder.AddConsumer(address, routingType, HandleCustomerMessage<TMessage, TConsumer>); builder.AddConsumer(address, routingType, HandleCustomerMessage<TMessage, TConsumer>);
return builder; return builder;
} }
private static async Task HandleCustomerMessage<TMessage, TConsumer>(Message message, IConsumer consumer, IServiceProvider serviceProvider, CancellationToken token) where TConsumer : class, ITypedConsumer<TMessage> { private static async Task HandleCustomerMessage<TMessage, TConsumer>(Message message, IConsumer consumer, IServiceProvider serviceProvider, CancellationToken token) where TConsumer : class, ITypedConsumer<TMessage> {
var msg = JsonSerializer.Deserialize<TMessage>(message.GetBody<string>()); var msg = JsonSerializer.Deserialize<TMessage>(message.GetBody<string>());
using var scope = serviceProvider.CreateScope(); using var scope = serviceProvider.CreateScope();
...@@ -23,7 +22,5 @@ namespace DDO_Application.Model { ...@@ -23,7 +22,5 @@ namespace DDO_Application.Model {
await typedConsumer.ConsumeAsync(msg, token); await typedConsumer.ConsumeAsync(msg, token);
await consumer.AcceptAsync(message); await consumer.AcceptAsync(message);
} }
} }
} }
\ No newline at end of file
using ActiveMQ.Artemis.Client; using ActiveMQ.Artemis.Client;
using ActiveMQ.Artemis.Client.Transactions; using ActiveMQ.Artemis.Client.Transactions;
using Microsoft.Extensions.Configuration;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -7,16 +8,12 @@ using System.Threading.Tasks; ...@@ -7,16 +8,12 @@ using System.Threading.Tasks;
namespace DDO_Application.Model { namespace DDO_Application.Model {
public class MessageProducer { public class MessageProducer {
private readonly IAnonymousProducer _producer; private readonly IAnonymousProducer _producer;
private readonly string _address; private string _address;
public MessageProducer(IAnonymousProducer producer) {
public MessageProducer(IAnonymousProducer producer, IConfiguration configuration) {
_producer = producer; _producer = producer;
_address = "test"; _address = configuration.GetSection("TestQueues").Get<configTestQueues>().Out;
}
public MessageProducer(IProducer producer, string address) {
_producer = producer as IAnonymousProducer;
_address = address;
} }
public async Task PublishAsync<T>(T message) { public async Task PublishAsync<T>(T message) {
var serialized = JsonSerializer.Serialize(message); var serialized = JsonSerializer.Serialize(message);
var msg = new Message(serialized); var msg = new Message(serialized);
......
...@@ -27,9 +27,8 @@ namespace DDO_Application { ...@@ -27,9 +27,8 @@ namespace DDO_Application {
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) { public void ConfigureServices(IServiceCollection services) {
var ActiveMQ = Configuration.GetSection("ActiveMQ").Get<configActiveMQ>().Endpoint; var ActiveMQ = Configuration.GetSection("ActiveMQ").Get<configActiveMQ>().Endpoint;
var testQueue = Configuration.GetSection("TestQueues").Get<configTestQueues>();
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" }); });
...@@ -38,14 +37,9 @@ namespace DDO_Application { ...@@ -38,14 +37,9 @@ namespace DDO_Application {
// turn enable ActiveMQ support in project // turn enable ActiveMQ support in project
services.AddActiveMq("ddoApp-cluster", new[] { Endpoint.Create(host: "192.168.2.19", port: 5672, "guest", "guest") }) services.AddActiveMq("ddoApp-cluster", new[] { Endpoint.Create(host: "192.168.2.19", port: 5672, "guest", "guest") })
//services.AddActiveMq("ddoApp-cluster", new[] { ActiveMQ }) //services.AddActiveMq("ddoApp-cluster", new[] { ActiveMQ })
//.AddAnonymousProducer<MessageProducer>() .AddAnonymousProducer<MessageProducer>()
.AddProducer<MessageProducer>(testQueue.Out) .AddTypedConsumer<TestMessage, MessageCunsomer>(Configuration.GetSection("TestQueues").Get<configTestQueues>().In, RoutingType.Multicast);
//.AddTypedProducer<TestMessage, TypedProducer>(testQueue.Out, RoutingType.Multicast)
.AddTypedConsumer<TestMessage, MessageCunsomer>(testQueue.In, RoutingType.Multicast);
services.AddActiveMqHostedService(); services.AddActiveMqHostedService();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
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