The scenario of ActiveMQ, C # demo in industrial IoT or system integration is comprehensively analyzed,

Source: Internet
Author: User

The scenario of ActiveMQ, C # demo in industrial IoT or system integration is comprehensively analyzed,

1. [serialization] C # communication (Serial Port and network) Framework Design and Implementation

2. [Open Source] C # cross-platform Iot communication framework ServerSuperIO (SSIO) Introduction

2. Overall system construction solution using SuperIO and open-source cross-platform Iot framework ServerSuperIO

3. C # technical route of industrial IoT and integrated system solutions (data sources, data collection, data upload and receiving, ActiveMQ, Mongodb, WebApi, and mobile App)

5. ServerSuperIO Open Source Address: https://github.com/wxzz/ServerSuperIO

 

Contents

Comprehensive Analysis of ActiveMQ scenarios in industrial IoT or system integration... 1

Preface... 1

Chapter 1 Terminal/interaction scenarios... 3

1.1 terminal devices... 3

1.2 communication mechanism... 3

Chapter 2 ActvieMQ application scenarios... 4

2.1 Publish/Subscribe (Publish/Subscribe)... 4

2.2 Producer/Consumer (Producer/Consumer)... 7

2.3 Request/Response (Request/Response)... 10

Chapter 3 scenario analysis... 16

3.1 communication layer... 16

3.2 data service layer... 16

3.3 summary... 16

 

Preface

Internet technology has developed very mature, with various open source code, frameworks and solutions. In view of the versatility of internet technology, it is bound to extend to other fields. Whether it is industry 4.0 or Internet + industry, the transmission of Internet technology to the industrial field is inevitable.

Therefore, the technical reserves and technical line research for industrial application scenarios are also an important part of daily work, providing technical platforms and support for horizontal and vertical development of the company, of course, it also depends on the leader's vision.

Chapter 1 Terminal/interaction scenarios

Any technology serves the business, and the business has a specific application scenario. It is meaningless to talk about technology without implementing the environment. solving actual problems and ensuring stability for a long period of time is our goal. At the same time, we should consider the problem from multiple perspectives and make a balance.

1.1 terminal Devices

(1) Types of terminals: embedded hardware/sensors, PCs (monitoring stations, large monitoring devices, etc.), and mobile terminals.

(2) Interaction Mode: one-way interaction, data upload, the server may return confirmation information to prove that the data has been received; two-way interaction, the server will not only return confirmation information, at the same time, it also needs to actively send command information to the specified terminal, such as controlling the mechanical operation commands of hardware devices, modifying the parameters commands of hardware devices, and sending supplementary data commands.

(3) device management: device management refers to the status of a device, which includes two aspects: Device IO status and device communication status. The IO status of the device includes: I/O enabled and I/O disabled. The device communication status includes: Communication interruption, communication interference, and normal communication. In order to judge the fault, the logical relationship here is: When I/O is enabled, it does not necessarily mean that the communication is normal; when I/O is disabled, it does not necessarily mean that the communication is interrupted; communication interference does not necessarily mean that I/O is enabled.

(4) Data Integrity: data loss is allowed. Generally, incremental data based on the original data can be lost. Data loss is not allowed. Generally, pulse data cannot be lost.

1.2 Communication Mechanism

(1) Active Data Request: the server sends commands to the terminal to determine who to upload data and what data to upload.

(2) passive data reception: the server passively receives data uploaded by the terminal, processes data based on the data information, and returns confirmation information.

Chapter 2 ActvieMQ application scenarios

There are many message queues. This article uses ActiveMQ as an example to describe how to implement C # In all code, mainly considering common application modes. Download the sample code: http://pan.baidu.com/s/1qxz1su4.

2.1 Publish/Subscribe (Publish/Subscribe)

A message publisher publishes a message on a topic. All subscriptions to this topic receive the same message. This mode is a one-to-many relationship, for example:

Release code:

static void Main(string[] args)        {            try            {                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");                using (IConnection connection = factory.CreateConnection())                {                    using (ISession session = connection.CreateSession())                    {                        IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("Topic"));                        string text = Console.ReadLine();                        while (text!="exit")                        {                            ITextMessage msg = prod.CreateTextMessage();                            msg.Text = text;                            prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);                            Console.WriteLine("Sending: " + text);                            System.Threading.Thread.Sleep(2000);                        }                    }                }                Console.ReadLine();            }            catch (System.Exception e)            {                Console.WriteLine("{0}", e.Message);                Console.ReadLine();            }        }

Subscriber code:

static void Main(string[] args)        {            try            {                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");                using (IConnection connection = factory.CreateConnection())                {                    connection.ClientId = "testing listener1";                    connection.Start();                    using (ISession session = connection.CreateSession())                    {                        IMessageConsumer consumer = session.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("Topic"), "testing listener1", null, false);                        consumer.Listener += new MessageListener(consumer_Listener);                        Console.ReadLine();                    }                    connection.Stop();                    connection.Close();                }            }            catch (System.Exception e)            {                Console.WriteLine(e.Message);                Console.ReadLine();            }        }        static void consumer_Listener(IMessage message)        {            try            {                ITextMessage msg = (ITextMessage)message;                Console.WriteLine("Receive: " + msg.Text);            }            catch (System.Exception e)            {                Console.WriteLine(e.Message);            }        }
2.2 Producer/Consumer (Producer/Consumer)

The producer has produced a piece of soap, and the consumer has purchased the soap. After it is used up, the message is sent in this world. The relationship between the producer and the consumer is contingent, this is a one-to-one relationship, for example:

 

Production Code:

 static void Main(string[] args)        {            try            {                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");                using (IConnection connection = factory.CreateConnection())                {                    using (ISession session = connection.CreateSession())                    {                        IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("Queue"));                        string text = Console.ReadLine();                        while (text != "exit")                        {                            ITextMessage msg = prod.CreateTextMessage();                            msg.Text = text;                            prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);                            Console.WriteLine("Sending: " + text);                            System.Threading.Thread.Sleep(2000);                        }                    }                }                Console.ReadLine();            }            catch (System.Exception e)            {                Console.WriteLine("{0}", e.Message);                Console.ReadLine();            }        }

 Consumer Code:

static void Main(string[] args)        {            try            {                IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");                using (IConnection connection = factory.CreateConnection())                {                    //connection.ClientId = "testing listener2";                    connection.Start();                     using (ISession session = connection.CreateSession())                    {                        IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("Queue"));                        consumer.Listener += new MessageListener(consumer_Listener);                        Console.ReadLine();                    }                    connection.Stop();                    connection.Close();                }            }            catch (System.Exception e)            {                Console.WriteLine(e.Message);                Console.ReadLine();            }        }        static void consumer_Listener(IMessage message)        {            try            {                ITextMessage msg = (ITextMessage)message;                Console.WriteLine("Receive: " + msg.Text);            }            catch (System.Exception e)            {                Console.WriteLine(e.Message);            }        }

 

2.3 Request/Response (Request/Response)

The communication method of request-response is widely used. The client uploads real-time data or parameters to the server. After the server completes processing, it must return confirmation information. The Interaction relationship is as follows:

 

Client code:

static void Main(string[] args)        {            IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");            try            {                using (IConnection connection = factory.CreateConnection())                {                    connection.Start();                    using (ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge))                    {                        IDestination destination =  new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("client.messages");                        IMessageProducer producer = session.CreateProducer(destination);                        producer.DeliveryMode=MsgDeliveryMode.NonPersistent;                        IDestination tempDest = session.CreateTemporaryQueue();                        IMessageConsumer responseConsumer = session.CreateConsumer(tempDest);                        responseConsumer.Listener += new MessageListener(consumer_Listener);                                               string text = Console.ReadLine();                        while (text != "exit")                        {                            ITextMessage msg = session.CreateTextMessage();                            msg.Text = text;                            msg.NMSReplyTo = tempDest;                            msg.NMSCorrelationID = DateTime.Now.ToString("yyyyMMddHHmmss");                            producer.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue);                            Console.WriteLine("Sending: " + text);                            System.Threading.Thread.Sleep(2000);                        }                        Console.ReadLine();                    }                    connection.Stop();                    connection.Close();                }            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                Console.ReadLine();            }        }        static void consumer_Listener(IMessage message)        {            try            {                ITextMessage msg = (ITextMessage)message;                Console.WriteLine("Receive: " + msg.Text);            }            catch (System.Exception e)            {                Console.WriteLine(e.Message);            }        }

 Server code:

 private static ISession session;        private static IMessageProducer replyProducer;        static void Main(string[] args)        {            IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616/");            try            {                    IConnection connection = factory.CreateConnection();                    connection.Start();                    session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);                    IDestination adminQueue = new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue("client.messages");                    replyProducer = session.CreateProducer();                    replyProducer.DeliveryMode=MsgDeliveryMode.NonPersistent;                    IMessageConsumer consumer = session.CreateConsumer(adminQueue);                    consumer.Listener += new MessageListener(consumer_Listener);                Console.ReadLine();            }            catch (Exception ex)            {                Console.WriteLine(ex.Message);                Console.ReadLine();            }        }        static void consumer_Listener(IMessage message)        {            try            {                ITextMessage response = session.CreateTextMessage();                if (message is ITextMessage) {                    ITextMessage txtMsg = (ITextMessage)message;                    string messageText = txtMsg.Text;                    response.Text = messageText;                    Console.WriteLine("Receive:" + messageText);                }                response.NMSCorrelationID=message.NMSCorrelationID;                replyProducer.Send(message.NMSReplyTo, response);            }            catch (Exception e)            {                Console.WriteLine(e.Message);            }        }
Chapter 3 Scenario Analysis

We analyze the problems of message queues at two layers, namely the communication layer and data service layer, based on the architecture of the system construction process.

3.1 communication layer

Can ActiveMQ be used in the communication layer )? This problem depends on two aspects: 1. If the terminal device has embedded hardware or even developed by C51, then compatibility issues will be involved in the process of system integration and Iot. Obviously, it is a headache to connect to the Message Queue. It is not impossible to write a connection driver with the C51, but it is necessary to evaluate the workload and stability. 2. The two-way interaction between the server and a specified terminal is frequent, especially when the server sends the device calibration command in real time. In this case, the message queue is inferior to the communication framework.

3.2 data service layer

After receiving the data, the server can use the producer and consumer modes of the message queue to process the data and decouple the system services.

You can also use message queues to send commands to a specified terminal.

3.3 Summary

We recommend that you use the communication framework at the communication layer to promptly respond to the I/O and communication statuses of devices, ensuring the communication efficiency. For the data service layer, we recommend that you do not deploy it in the Communication Framework for processing. You can use message queues to work with the communication framework.

The overall architecture is as follows:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The article was supported by group friends:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.