In this fast driven technical world we have new technologies and one of them is AWS-MQ but most of our legacy or web applications will have code written in C#. I will try to explain how you can connect to AWS-MQ using C# code.
Explaining two scenarios -
- If we have SSO Login to AWS Console.
- If we have AWS account with IAM login.
1. C# Code to connect with AWS-MQ when user have SSO Login -
- Add References Apache.NMS and Apache.NMS.ActiveMQ -
- Headers used -
using System;
using Apache.NMS;
using Apache.NMS.ActiveMQ.Transport.Tcp;
using Apache.NMS.ActiveMQ.Transport;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Util;
- Create a new function as producer ( ), this will the code which will command AWS-MQ what to create queue and store message.
- Create a new function as consumer ( ), this will the code which will fetch what is stored in AWS-MQ and used to showcase from .NET -
2. C# Code to connect with AWS-MQ when user have IAM Login -
- Write the same code, just replace the amq_broker with aws access key Id and secret access key. And try passing it. It will work for you.
Complete Code -
using System;using Apache.NMS;using Apache.NMS.ActiveMQ.Transport.Tcp;using Apache.NMS.ActiveMQ.Transport;using Apache.NMS.ActiveMQ;using Apache.NMS.ActiveMQ.Util;using System.Threading;namespace WindowsService1{ public class CSharp_to_AWSMQ { public static void producer() { SslTransportFactory ssl = new SslTransportFactory(); string amq_broker = "ssl://b-19ffa89d-5b13-48a2-a604-3e8975962250-1.mq.us-east-1" + ".amazonaws.com:61617?nms.AsyncSend=true"; ssl.ServerName = "52.4.134.145"; //IP of server where AMQ is running ssl.SslProtocol = "Tls"; ssl.AcceptInvalidBrokerCert = true; Uri uri = new Uri(amq_broker); ITransport transport = ssl.CreateTransport(uri); //transport.Start(); Connection connection = new Connection(uri, transport, new IdGenerator()) { UserName = "Soccer", Password = "Soccer7654321" }; connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IDestination dest = session.GetQueue("BishalQueue"); IMessageProducer producer = session.CreateProducer(dest); producer.DeliveryMode = MsgDeliveryMode.NonPersistent; var text = "Acknowlegement"; var msg = session.CreateTextMessage(text); producer.Send(msg); Console.WriteLine("Msg Printed from Producer: " + text); connection.Close(); // connection.Start(); // -> here is error: } public static void consumer() { SslTransportFactory ssl = new SslTransportFactory(); string amq_broker = "ssl://b-19ffa89d-5b13-48a2-a604-3e8975962250-1.mq.us-east-1" + ".amazonaws.com:61617?nms.AsyncSend=true"; ssl.ServerName = "52.4.134.145"; //IP of server where AMQ is running ssl.SslProtocol = "Tls"; ssl.AcceptInvalidBrokerCert = true; Uri uri = new Uri(amq_broker); ITransport transport = ssl.CreateTransport(uri); //transport.Start(); Connection connection = new Connection(uri, transport, new IdGenerator()) { UserName = "Soccer", Password = "Soccer7654321" }; connection.Start(); ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); IDestination dest = session.GetQueue("BishalQueue"); IMessageConsumer consumer = session.CreateConsumer(dest); IMessage consumerMessage = consumer.Receive(); ITextMessage consumerTextMesaage = (ITextMessage)consumerMessage; Console.WriteLine("Msg Recieved from Consumer: " + consumerTextMesaage.Text); connection.Close();}public static void Main(string[] args){Console.WriteLine("Creating Producer Thread");Thread prod = new Thread(producer);Console.WriteLine("Creating Consumer Thread");Thread cons = new Thread(consumer);prod.Start();cons.Start();}
}}For any queries please comment below i will try solving your queries.
How do you read the count from queue ?
ReplyDelete@Ali - Can you please explain in detail, count of what you need from queue?
Delete