Skip to content

Instantly share code, notes, and snippets.

@rwindegger
Created May 2, 2019 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwindegger/9f46c7451ebfdde37e8ffe420a2aac25 to your computer and use it in GitHub Desktop.
Save rwindegger/9f46c7451ebfdde37e8ffe420a2aac25 to your computer and use it in GitHub Desktop.
RabbitExpress.ExampleWorker
// ***********************************************************************
// Assembly : RabbitExpress.ExampleWorker
// Author : Rene Windegger
// Created : 04-30-2019
//
// Last Modified By : Rene Windegger
// Last Modified On : 04-30-2019
// ***********************************************************************
// <copyright file="Program.cs" company="Rene Windegger">
// Copyright (c) Rene Windegger. All rights reserved.
// </copyright>
// <summary>
// This file is part of RabbitExpress.
//
// RabbitExpress is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RabbitExpress is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this RabbitExpress. If not, see <http://www.gnu.org/licenses/>.
// </summary>
// ***********************************************************************
namespace RabbitExpress.ExampleWorker
{
using ExampleShared;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
/// <summary>
/// Class Program.
/// </summary>
internal class Program
{
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
private static void Main(string[] args)
{
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
.AddEnvironmentVariables()
.Build();
var r = new Random();
using (var qc = new QueueClient<Queues, JsonSerializer>(new Uri(config["RabbitExpressConnection"])))
{
qc.WatchQueue<ExampleMessage>(Queues.EXAMPLE_QUEUE, m =>
{
try
{
if (string.IsNullOrWhiteSpace(m.Message?.Text))
{
Console.WriteLine("Rejecting empty message.");
m.Reject(false);
return;
}
if (r.Next(100) % 3 == 0)
{
throw new ApplicationException("Simulated recoverable error.");
}
Console.WriteLine($"Acknowledging {m.Message.Text}");
m.Acknowledge();
if (m.Message.Text == "exit")
{
m.Client.StopWatch();
}
}
catch (Exception e)
{
Console.WriteLine($"Rejecting {m.Message?.Text} with reason: {e}");
m.Reject();
}
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment