Advertisement
arvigeus

Untitled

Feb 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using RawRabbit;
  5. using RawRabbit.Common;
  6. using RawRabbit.Configuration;
  7. using RawRabbit.Configuration.Exchange;
  8. using RawRabbit.Enrichers.Attributes;
  9. using RawRabbit.Instantiation;
  10.  
  11. namespace BugsBunny
  12. {
  13.     class Program
  14.     {
  15.         [Exchange(Name = "service", Type = ExchangeType.Topic)]
  16.         [Queue(Name = "notification", MessageTtl = 3000)]
  17.         [Routing(RoutingKey = "notification", AutoAck = true, PrefetchCount = 50)]
  18.         class BasicMessage
  19.         {
  20.             public BasicMessage(string message) { }
  21.  
  22.             // Uncoment this to work
  23.             //public BasicMessage() { }
  24.             // Or comment out this to work
  25.             public BasicMessage(string message1, string message2) { }
  26.         }
  27.  
  28.         private static async Task<Acknowledgement> OnBasicMessage(BasicMessage msg)
  29.         {
  30.             Console.WriteLine("Received");
  31.             return new Ack();
  32.         }
  33.  
  34.         public static void Main()
  35.         {
  36.             var exitEvent = new ManualResetEvent(false);
  37.             Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; exitEvent.Set(); };
  38.  
  39.             new RawRabbitOptions
  40.             {
  41.                 Plugins = p => {
  42.                     p.UseAttributeRouting();
  43.                 }
  44.             };
  45.  
  46.             var host = Environment.GetEnvironmentVariable("RABBITMQ_HOST") ?? "localhost";
  47.             var port = Convert.ToInt32(Environment.GetEnvironmentVariable("RABBITMQ_PORT") ?? "5672");
  48.  
  49.             var client = RawRabbitFactory.CreateSingleton(options: new RawRabbitOptions()
  50.             {
  51.                 ClientConfiguration = new RawRabbitConfiguration
  52.                 {
  53.                     Username = Environment.GetEnvironmentVariable("RABBITMQ_USER") ?? "guest",
  54.                     Password = Environment.GetEnvironmentVariable("RABBITMQ_PASS") ?? "guest",
  55.                     VirtualHost = "/",
  56.                     Hostnames = { host },
  57.                     Port = port,
  58.                     RequestTimeout = TimeSpan.FromSeconds(10),
  59.                     PublishConfirmTimeout = TimeSpan.FromSeconds(10),
  60.                     PersistentDeliveryMode = true,
  61.                     TopologyRecovery = true,
  62.                     AutoCloseConnection = false,
  63.                     AutomaticRecovery = true,
  64.                     RecoveryInterval = TimeSpan.FromMinutes(1),
  65.                     GracefulShutdown = TimeSpan.FromMinutes(1),
  66.                     RouteWithGlobalId = true,
  67.                     Ssl = new RabbitMQ.Client.SslOption()
  68.                 }
  69.             });
  70.  
  71.             var _shutdown = new CancellationTokenSource();
  72.            
  73.             client.SubscribeAsync<BasicMessage>(OnBasicMessage);
  74.            
  75.             client.PublishAsync<BasicMessage>(new BasicMessage("Hello. world!"));
  76.  
  77.             Console.WriteLine($"Listening on {host}:{port}");
  78.             Console.WriteLine("Press Ctrl+C to exit...");
  79.             exitEvent.WaitOne();
  80.  
  81.             client.Dispose();
  82.             exitEvent.Dispose();
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement