Commit 56ecccb3 by AlexNasyr

produser config obj injected

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