Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- using RawRabbit;
- using RawRabbit.Common;
- using RawRabbit.Configuration;
- using RawRabbit.Configuration.Exchange;
- using RawRabbit.Enrichers.Attributes;
- using RawRabbit.Instantiation;
- namespace BugsBunny
- {
- class Program
- {
- [Exchange(Name = "service", Type = ExchangeType.Topic)]
- [Queue(Name = "notification", MessageTtl = 3000)]
- [Routing(RoutingKey = "notification", AutoAck = true, PrefetchCount = 50)]
- class BasicMessage
- {
- public BasicMessage(string message) { }
- // Uncoment this to work
- //public BasicMessage() { }
- // Or comment out this to work
- public BasicMessage(string message1, string message2) { }
- }
- private static async Task<Acknowledgement> OnBasicMessage(BasicMessage msg)
- {
- Console.WriteLine("Received");
- return new Ack();
- }
- public static void Main()
- {
- var exitEvent = new ManualResetEvent(false);
- Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; exitEvent.Set(); };
- new RawRabbitOptions
- {
- Plugins = p => {
- p.UseAttributeRouting();
- }
- };
- var host = Environment.GetEnvironmentVariable("RABBITMQ_HOST") ?? "localhost";
- var port = Convert.ToInt32(Environment.GetEnvironmentVariable("RABBITMQ_PORT") ?? "5672");
- var client = RawRabbitFactory.CreateSingleton(options: new RawRabbitOptions()
- {
- ClientConfiguration = new RawRabbitConfiguration
- {
- Username = Environment.GetEnvironmentVariable("RABBITMQ_USER") ?? "guest",
- Password = Environment.GetEnvironmentVariable("RABBITMQ_PASS") ?? "guest",
- VirtualHost = "/",
- Hostnames = { host },
- Port = port,
- RequestTimeout = TimeSpan.FromSeconds(10),
- PublishConfirmTimeout = TimeSpan.FromSeconds(10),
- PersistentDeliveryMode = true,
- TopologyRecovery = true,
- AutoCloseConnection = false,
- AutomaticRecovery = true,
- RecoveryInterval = TimeSpan.FromMinutes(1),
- GracefulShutdown = TimeSpan.FromMinutes(1),
- RouteWithGlobalId = true,
- Ssl = new RabbitMQ.Client.SslOption()
- }
- });
- var _shutdown = new CancellationTokenSource();
- client.SubscribeAsync<BasicMessage>(OnBasicMessage);
- client.PublishAsync<BasicMessage>(new BasicMessage("Hello. world!"));
- Console.WriteLine($"Listening on {host}:{port}");
- Console.WriteLine("Press Ctrl+C to exit...");
- exitEvent.WaitOne();
- client.Dispose();
- exitEvent.Dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement