/// <summary> /// initial mqtt cllient and connect to ActiveMQ /// </summary> public Producer() { m_strProducerClientID = "Producer-" + s_nProducerID.ToString(); s_nProducerID = ((0 != (s_nProducerID % int.MaxValue)) ? (++s_nProducerID) : 0); m_mqttProducer = new MqttClient(ActiveMQ_MQTT_Settings.s_strBrokerName, ActiveMQ_MQTT_Settings.s_nBrokerPort, false, null, null, MqttSslProtocols.None); try { m_mqttProducer.Connect(m_strProducerClientID, ActiveMQ_MQTT_Settings.s_strUserName, ActiveMQ_MQTT_Settings.s_strPassword); m_bIsConnected = true; ActiveMQ_MQTT_Settings.Trace(m_strProducerClientID + " Connect ActiveMQ success."); } catch (Exception ex) { ActiveMQ_MQTT_Settings.Trace("Connect ActiveMQ failed, error msg: " + ex.Message + "; error stack trace: " + ex.StackTrace); if (m_bIsConnected) { m_mqttProducer.Disconnect(); m_bIsConnected = false; } } }
/// <summary> /// initial mqtt cllient and connect to ActiveMQ /// </summary> public Consumer() { m_strConsumerClientID = "Consumer-" + s_nConsumerID.ToString(); s_nConsumerID = ((0 != (s_nConsumerID % int.MaxValue)) ? (++s_nConsumerID) : 0); m_mqttConsumer = new MqttClient(ActiveMQ_MQTT_Settings.s_strBrokerName, ActiveMQ_MQTT_Settings.s_nBrokerPort, false, null, null, MqttSslProtocols.None); m_mqttConsumer.MqttMsgPublishReceived += PublishReceived; try { m_mqttConsumer.Connect(m_strConsumerClientID, ActiveMQ_MQTT_Settings.s_strUserName, ActiveMQ_MQTT_Settings.s_strPassword); m_mqttConsumer.Subscribe(new string[] { ActiveMQ_MQTT_Settings.s_strTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); m_bIsConnected = true; ActiveMQ_MQTT_Settings.Trace(m_strConsumerClientID + " Connect ActiveMQ success."); } catch (Exception ex) { ActiveMQ_MQTT_Settings.Trace("Connect ActiveMQ failed, error msg: " + ex.Message + "; error stack trace: " + ex.StackTrace); if (m_bIsConnected) { m_mqttConsumer.Disconnect(); m_bIsConnected = false; } } m_queueConsumeMsg = new ConcurrentQueue <ConsumeMessageInfo>(); }
/// <summary> /// publish message to the topic /// </summary> private void PublishMessage() { while (m_bIsConnected) { string strMessage = m_strProducerClientID + " produced message " + m_nMesssageSequenceNumber.ToString(); m_nMesssageSequenceNumber = ((0 != (m_nMesssageSequenceNumber % int.MaxValue)) ? (++m_nMesssageSequenceNumber) : 0); m_mqttProducer.Publish(ActiveMQ_MQTT_Settings.s_strTopic, System.Text.Encoding.ASCII.GetBytes(strMessage), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false); ActiveMQ_MQTT_Settings.Trace(m_strProducerClientID + " published topic content: [" + strMessage + "]"); Thread.Sleep(100); } }
/// <summary> /// publish message to the topic /// </summary> private void ConsumeMessage() { while (m_bIsConnected) { if (!m_queueConsumeMsg.IsEmpty) { ConsumeMessageInfo oConsumeMessageInfo; if (m_queueConsumeMsg.TryDequeue(out oConsumeMessageInfo)) { ActiveMQ_MQTT_Settings.Trace(m_strConsumerClientID + " received topic content: [" + System.Text.Encoding.ASCII.GetString(oConsumeMessageInfo.Content) + "] internal message id:[" + oConsumeMessageInfo.InternalMsgID + "]"); } } else { Thread.Sleep(100); } } }