public void DestroyMQDestination(IDestination dest) { if (dest != null) { dest.Dispose(); } }
public void TerminateConnections() { if (_mqMessageConsumer != null) { try { _mqMessageConsumer.Close(); _mqMessageConsumer.Dispose(); } catch (Exception exception) { FdrCommon.LogEvent(exception, EventLogEntryType.Error); } } if (_mqDestination != null) { _mqDestination.Dispose(); _mqDestination = null; } if (_mqConnection != null) { _mqConnection.Close(); _mqConnection.Dispose(); } if (_newSesstion != null) { _newSesstion.Close(); _newSesstion.Dispose(); } }
public void Subscribe(string topicName) { var connection = _connectionFactory.CreateConnection(); connection.ExceptionListener = IbmMQService.OnException; var session = connection.CreateSession(false, AcknowledgeMode.AutoAcknowledge); IDestination topic = session.CreateTopic(topicName); IMessageConsumer subscriber = session.CreateConsumer(topic); connection.Start(); Console.WriteLine("Waiting for messages...."); while (true) { var textMessage = (ITextMessage)subscriber.Receive(); if (textMessage != null) { Console.WriteLine(textMessage.Text); if (textMessage.Text.Equals("Exit")) { break; } } } connection.Close(); subscriber.Close(); topic.Dispose(); }
public void Listen(int second = 1) { var connection = _connectionFactory.CreateConnection(); connection.ExceptionListener = IbmMQService.OnException; var session = connection.CreateSession(false, AcknowledgeMode.AutoAcknowledge); IDestination topic = session.CreateQueue(_mqConfigModel.QueueName); IMessageConsumer consumer = session.CreateConsumer(topic); connection.Start(); Console.WriteLine("Waiting for messages...."); for (int i = 0; i < second; i++) { var textMessage = (ITextMessage)consumer.Receive(1000); if (textMessage != null) { Console.WriteLine(textMessage.Text); if (textMessage.Text.Equals("Exit")) { break; } } } connection.Close(); consumer.Close(); topic.Dispose(); }
public void Dispose() { if (Interlocked.Decrement(ref _references) == 0) { _destination.Dispose(); _disposeAction(); } }
/// <summary> /// Delete a destination (Queue, Topic, Temp Queue, Temp Topic). /// </summary> public void DeleteDestination(IDestination destination) { // Nothing to delete. Resources automatically disappear. if (destination.IsTemporary) { destination.Dispose(); } return; }
private void OnExceptionListener(Exception e) { Debug.Write(e.StackTrace); Debug.Write("##### There was an Exception ####### "); destinationRequest.Dispose(); destinationResponse.Dispose(); // responseConsumer.Close(); producer.Close(); session.Close(); con.Close(); }
private bool _disposedValue = false; // To detect redundant calls private void Dispose(bool disposing) { if (!_disposedValue) { _disposedValue = true; if (disposing) { DisposeMiddlewares(CollectMiddleware); Destination.Dispose(); } } }
private void ProduceMessageXMS() { // txMessage.Text = "Producing..."; _mqHelper.Host = txHost.Text; _mqHelper.Port = Int32.Parse(txPort.Text); _mqHelper.QueueManager = txQMgr.Text; _mqHelper.Channel = txQChannel.Text; _mqHelper.ProducerQueue = txQName.Text; _mqHelper.MQType = MQXMSHelper.MQTypes.QUEUE; string msg = txMessage.Text.ToString(); try { IConnectionFactory _mqConnectionFactory = _mqHelper.CreateMQConnectionFactoryWMQ(); IConnection _mqConnection = _mqHelper.CreateMQConnection(_mqConnectionFactory); ISession _mqSession = _mqHelper.CreateMQSession(_mqConnection); IDestination _mqDestination = _mqHelper.CreateMQDestination(_mqSession, _mqHelper.ProducerQueue); if (_mqSession != null && String.IsNullOrEmpty(msg) == false) { IDestination mqDestination = _mqHelper.CreateMQDestination(_mqSession, _mqHelper.ProducerQueue); // mqDestination.SetBooleanProperty(XMSC.WMQ_MQMD_WRITE_ENABLED, true); // when set XMSC.WMQ_MQMD_WRITE_ENABLED = true for the Destination of producer/sender, XMSC.WMQ_MQMD_READ_ENABLED = true must be set for the Destination of consumer/receiver IMessageProducer mqProducer = _mqHelper.CreateMQProducer(_mqSession, mqDestination); // mqProducer.DeliveryMode = DeliveryMode.NonPersistent; // mqProducer.Priority = 0; IMessage retMsgObj = _mqHelper.CreateMQMessage(_mqSession, "text", msg); //retMsgObj.SetIntProperty(XMSC.JMS_IBM_MSGTYPE, MQC.MQMT_REPLY); //retMsgObj.SetIntProperty(XMSC.JMS_IBM_ENCODING, XMSC.WMQ_ENCODING_NATIVE); //retMsgObj.JMSCorrelationID = originalMsgObj.JMSMessageID; //retMsgObj.SetStringProperty(XMSC.JMS_IBM_MQMD_REPLYTOQMGR, originalMsgObj.GetStringProperty(XMSC.JMS_IBM_MQMD_REPLYTOQMGR)); _mqHelper.SendMessage(_mqSession, mqProducer, retMsgObj); _mqHelper.DestroyMQProducer(mqProducer); mqDestination.Dispose(); MessageBox.Show("Message sent to " + _mqHelper.ProducerQueue); } } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source + Environment.NewLine + ex.GetBaseException()); } }
protected override void OnDisappearing() { base.OnDisappearing(); if (MQTTTopic != null) { MQTTTopic.Dispose(); } if (StompTopic != null) { StompTopic.Dispose(); } if (OpenWireTopic != null) { OpenWireTopic.Dispose(); } if (MQTTConsumer != null) { MQTTConsumer.Dispose(); } if (StompConsumer != null) { StompConsumer.Dispose(); } if (OpenWireConsumer != null) { OpenWireConsumer.Dispose(); } if (connection != null) { connection.Dispose(); } if (session != null) { session.Dispose(); } }
private void PublishMessageXMS() { // option 2: XMS // NOTE: in MQ, Topic --> Object Authorities --> Manage Authority Records --> Specific Profiles, make sure the users/groups (using the XMS API calls) have the proper permissions, eg. Publish, Subscribe and etc. XMSFactoryFactory ff = null; IConnectionFactory cf = null; IConnection conn = null; ISession sess = null; IDestination dest = null; IMessageProducer prod = null; ITextMessage tmsg = null; string topicName = txQName.Text.ToString(); try { ff = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ); cf = ff.CreateConnectionFactory(); cf.SetStringProperty(XMSC.WMQ_HOST_NAME, txHost.Text.ToString()); int portNo = 0; Int32.TryParse(txPort.Text.ToString(), out portNo); cf.SetIntProperty(XMSC.WMQ_PORT, portNo); cf.SetStringProperty(XMSC.WMQ_CHANNEL, txQChannel.Text.ToString()); cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT_UNMANAGED); // default is XMSC.WMQ_CM_CLIENT, it is managed - using the same language; if need to access different platform, use unmanaged cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, txQMgr.Text.ToString()); /* * MessageBox.Show("Host:" + cf.GetStringProperty(XMSC.WMQ_HOST_NAME) + Environment.NewLine + * "Port:" + cf.GetIntProperty(XMSC.WMQ_PORT) + Environment.NewLine + * "Queue Manager:" + cf.GetStringProperty(XMSC.WMQ_QUEUE_MANAGER) + Environment.NewLine + * "Channel:" + cf.GetStringProperty(XMSC.WMQ_CHANNEL) + Environment.NewLine + * "Topic:" + topicName + Environment.NewLine); */ conn = cf.CreateConnection(); sess = conn.CreateSession(false, AcknowledgeMode.AutoAcknowledge); // MessageBox.Show("Session created."); dest = sess.CreateTopic(topicName); // dest.SetIntProperty(XMSC.WMQ_TARGET_CLIENT, XMSC.WMQ_TARGET_DEST_MQ); // exclude RFH2 header since amqchange.exe cannot handle // MessageBox.Show("Destination created as Topic."); prod = sess.CreateProducer(dest); // MessageBox.Show("Message Producer created."); conn.Start(); // MessageBox.Show("Connection started."); tmsg = sess.CreateTextMessage(txMessage.Text.ToString()); prod.Send(tmsg); MessageBox.Show("Message " + tmsg.Text.Length + " sent to " + topicName); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source + Environment.NewLine + ex.GetBaseException()); } finally { if (prod != null) { prod.Close(); } if (dest != null) { dest.Dispose(); } if (sess != null) { sess.Close(); sess.Dispose(); } if (conn != null) { conn.Close(); conn.Dispose(); } } }
/// <summary> /// less efficient version of receive message /// </summary> /// <returns></returns> public String ReceiveMessage() { String msgStr = ""; IConnectionFactory connFactory = CreateMQConnectionFactoryWMQ(); using (IConnection conn = CreateMQConnection(connFactory)) // ensure Dispose() even with exceptions, similar to try-catch { using (ISession sess = CreateMQSession(conn)) { using (IDestination dest = CreateMQDestination(sess, _consumerQueue)) { using (IMessageConsumer consumer = CreateMQConsumer(sess, dest)) { //using (receiveCompleteEvent = new System.Threading.AutoResetEvent(false)) // not using ASYNC way //{ // MessageListener msgLsnr = new MessageListener(handleMessage); // consumer.MessageListener = msgLsnr; // conn.Start(); //} conn.Start(); IMessage msg = null; msg = consumer.ReceiveNoWait(); // non-blocking //Console.WriteLine(msg) ; if (msg != null) { if (msg is ITextMessage) { msg.SetIntProperty(XMSC.JMS_IBM_CHARACTER_SET, XMSC.CCSID_UTF8); msg.SetIntProperty(XMSC.JMS_IBM_ENCODING, XMSC.WMQ_ENCODING_NATIVE); msgStr = ((ITextMessage)msg).Text.Replace("\0", " "); // replace null-terminated characters to empty; otherwise, cannot display // tbxOutput.AppendText(((ITextMessage)msg).Text); } else if (msg is IBytesMessage) { byte[] bytes = null; int num = ((IBytesMessage)msg).ReadBytes(bytes); msgStr = Encoding.UTF8.GetString(bytes); // tbxOutput.AppendText(Encoding.UTF8.GetString(bytes)); } msg.ClearBody(); } else { msgStr = "nothing received.\n"; } consumer.Close(); consumer.Dispose(); } dest.Dispose(); } sess.Close(); sess.Dispose(); } conn.Stop(); conn.Close(); conn.Dispose(); } // close connection return(msgStr); }