Skip to content

Message Queuing

Hartono Halim edited this page Aug 23, 2017 · 4 revisions

Shift client/server can integrate successfully with message queuing infrastructure.

The message queue subscriber use Shift client to add jobs similar to any Shift client apps. Here is a simple console app subscriber example with popular RabbitMQ:

class Program
{
    private static JobClient jobClient;
    static void Main(string[] args)
    {
        InitShiftClient();

        using (var bus = RabbitHutch.CreateBus("host=localhost"))
        {
            bus.Subscribe<TextMessage>("test", HandleTextMessage);

            Console.WriteLine("Listening for messages. Hit <return> to quit.");
            Console.ReadLine();
        }
    }

    static void HandleTextMessage(TextMessage textMessage)
    {
        var job = new TestJob();
        var progress = new SynchronousProgress<ProgressInfo>();
        var jobID = jobClient.Add("RabbitMQ.Client", () => job.Start("Hello " + textMessage.Text, progress));
        Console.WriteLine("Got message: {0}", textMessage.Text);
    }
    
    private static void InitShiftClient()
    {
        var config = new Shift.ClientConfig();
                config.StorageMode = "mssql"; 
        config.DBConnectionString = ConfigurationManager.ConnectionStrings["ShiftDBConnection"].ConnectionString;
        //config.EncryptionKey = "[OPTIONAL_ENCRYPTIONKEY]"; //optional, will encrypt parameters in DB
        jobClient = new JobClient(config);
    }
}