Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main()
- {
- ServiceBase[] ServicesToRun;
- ServicesToRun = new ServiceBase[]
- {
- new TestService()
- };
- ServiceBase.Run(ServicesToRun);
- }
- public static void Start(string[] args)
- {
- // onstart code here
- try
- {
- ConsumeMessage();
- }
- catch (Exception e)
- {
- LogError(e);
- }
- }
- public static void ConsumeMessage()
- {
- ConnectionFactory factory = new ConnectionFactory()
- {
- HostName = "server",
- Port = 5672,
- UserName = "user1",
- Password = "user1",
- VirtualHost = "/",
- RequestedHeartbeat = 30,
- };
- while (!Cancelled)
- {
- try
- {
- if (_subscription == null)
- {
- try
- {
- _connection = factory.CreateConnection();
- }
- catch (BrokerUnreachableException)
- {
- //You probably want to log the error and cancel after N tries,
- //otherwise start the loop over to try to connect again after a second or so.
- continue;
- }
- string queueName = "test-queue";
- _channel = _connection.CreateModel();
- _channel.ExchangeDeclare("test-exchange", "fanout", true);
- _channel.QueueDeclare(queueName, true, false, false, null);
- _channel.QueueBind(queueName, "test-exchange", "");
- _subscription = new Subscription(_channel, queueName, false);
- }
- BasicDeliverEventArgs args;
- bool gotMessage = _subscription.Next(250, out args);
- if (gotMessage)
- {
- if (args == null)
- {
- //This means the connection is closed.
- // DisposeAllConnectionObjects();
- continue;
- }
- savemessage(args.Body);
- _subscription.Ack(args);
- }
- }
- catch (OperationInterruptedException ex)
- {
- // DisposeAllConnectionObjects();
- }
- }
- // DisposeAllConnectionObjects();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement