This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Messaging; | |
| using RabbitMQ.Client; | |
| using RabbitMQ.Client.Events; | |
| namespace Consumer; | |
| public class Worker : BackgroundService | |
| { | |
| private readonly ILogger<Worker> _logger; | |
| private readonly IRabbitMQChannel _channel; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Consumer; | |
| using Messaging; | |
| IHost host = Host.CreateDefaultBuilder(args) | |
| .ConfigureServices(services => | |
| { | |
| services.AddHostedService<Worker>() | |
| .AddSingleton<IRabbitMQConnection, RabbitMQConnection>() | |
| .AddSingleton<IRabbitMQChannel, RabbitMQChannel>(); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Threading.Channels; | |
| using Messaging; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.AspNetCore.Mvc.RazorPages; | |
| namespace Publisher.Pages; | |
| public class IndexModel : PageModel | |
| { | |
| private readonly ILogger<IndexModel> _logger; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using RabbitMQ.Client; | |
| namespace Messaging { | |
| public interface IRabbitMQConnection { | |
| public IModel CreateModel(); | |
| } | |
| public class RabbitMQConnection : IRabbitMQConnection { | |
| private readonly IConnection _connection; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using RabbitMQ.Client; | |
| namespace Messaging { | |
| public interface IRabbitMQChannel { | |
| public IModel Model { get; } | |
| public void BasicPublish(string exchange, string routingKey, bool mandatory, ReadOnlyMemory<byte> body); | |
| } | |
| public class RabbitMQChannel : IRabbitMQChannel { | |
| public IModel Model { get; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using RabbitMQ.Client; | |
| namespace Messaging { | |
| public class QueueConfig { | |
| public static string Exchange { get; } = "amq.direct"; | |
| // Queues | |
| public static string QueueA { get; } = "QueueA"; // Active queue | |
| public static string QueueW { get; } = "QueueW"; // Wait queue | |
| public static string QueueP { get; } = "QueueP"; // Parking queue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.rabbitdemo; | |
| import org.springframework.amqp.AmqpRejectAndDontRequeueException; | |
| import org.springframework.amqp.core.Message; | |
| import org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler; | |
| import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException; | |
| import org.springframework.dao.DataIntegrityViolationException; | |
| import org.springframework.stereotype.Component; | |
| @Component |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.rabbitdemo; | |
| import org.springframework.amqp.rabbit.core.RabbitTemplate; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.PathVariable; | |
| import org.springframework.web.bind.annotation.RestController; | |
| @RestController | |
| public class RequestHandler { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.rabbitdemo; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.amqp.core.Message; | |
| import org.springframework.amqp.rabbit.annotation.RabbitListener; | |
| import org.springframework.amqp.rabbit.core.RabbitTemplate; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.stereotype.Component; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.rabbitdemo; | |
| import org.springframework.amqp.core.DirectExchange; | |
| import org.springframework.amqp.core.*; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| @Configuration | |
| public class QueueConfig { | |
| public static final String DIRECT_EXCHANGE_NAME = "amqp.direct"; |
NewerOlder