Tuesday, March 31, 2015

How to create a message in a Scribe Insight queue using C#

If you are familiar with Scribe Insight then you would know that “queue” based integration is the preferred integration method. The reason for that is performance. Multiple message processors can be allocated to process the queues. For more information please read this blog on the “Message Processor”.

Scribe Adapters can create the queue message automatically, but, what if we need to create the queue message from the portal or a custom program? This blog will show you how to create a MSMQ message using C#.

The code below will create a message with the label "PortalCustomer" and put it in the ScribeIn queue.

using System;
using System.Messaging;

namespace MSMQ
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Initiating MSMQ object");
           
           // queue address
            string queueAddress = @"FormatName:Direct=OS:machinename\private$\scribein";
            
            Console.WriteLine("Queue Path: {0}", queueAddress);

            //Create an instance of MSMQ Manager
            MSMQManager msmq = new MSMQManager(queueAddress);
            msmq.SendMessageToQueue();

            Console.WriteLine("Message sent successfully");
            Console.ReadLine();
        }
    }

    // Class that initiate the message queue and send the message
    class MSMQManager
    {
        private string queueAddress;

        public MSMQManager(string queueAddress)
        {
            this.queueAddress = queueAddress;
        }


        //Creating and sending the message
        public void SendMessageToQueue()
        {
            MessageQueue msMq = new MessageQueue(queueAddress);

            try
            {
               //create an instance of customer class
                Customer req = new Customer()
                {
                    FirstName="John",
                    LastName="Smith",
                    ReceivedDate=DateTime.Now

                };
                
                Console.WriteLine("Creating customer request: {0}", req.ToString());

                //create the message
                Message m = new Message()
                {
                    
                    Body = req,
                    Label = "PortalCustomer",
                    Recoverable = true,
                   
                };
           
                Console.WriteLine("Initialising message: {0}", m.Label);

                Console.WriteLine("Sending...");
                msMq.Send(m);
            }

            catch (MessageQueueException e)
            {
                Console.Write(e.Message);
            }
            catch (Exception e)
            {
                Console.Write("Error Message: {0}. Trace: {1}", e.Message, e.StackTrace);
            }
            finally
            {
                msMq.Close();
            }
        }
              
    }
}
 
Here is the code for the customer class.

using System;

namespace MSMQ
{
    [Serializable]
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime ReceivedDate { get; set; }

        public override string ToString()
        {
            return string.Format("Link: {0} - Response: {1} - ReceivedDate: {2}", FirstName, LastName , ReceivedDate.ToString());
        }
    }
}


Run the code and look in the ScribeIn queue or in the Dead Message queue (if you don’t have any integration using label “PortalCustomer”).

image

Make sure that MSMQ are installed on the machine where the code is hosted, and can communicate  with Scribe server on port 1801.

No comments:

Post a Comment