How to send queue message in azure portal using x++ code
Table of Content:
How to send queue message in azure portal using x++ code
To send a queue message in Azure using X++ code, you can utilize the Dynamics 365 Finance and Operations development environment. Here's an example of how you can send a queue message in Azure using X++:
using Newtonsoft; using System.Collections.Generic; using System.Collections; using Microsoft.ServiceBus; using Microsoft.ServiceBus.Messaging; class CURDForDataEntity { static str connectionString = 'Endpoint=sb://usvd365a1hxdevasb01.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=dbt8OR5p4nQgLYAAjGhCOByDNXF0DfxSG3gFx8WUI6k='; static str queueName = 'ibssalesorder_in'; public static void main(Args _args) { Microsoft.Dynamics.Ax.Xpp.ErrorException xppEx; if (connectionString && queueName) { str json = @'{"name":"Rumman"}'; try { Microsoft.ServiceBus.Messaging.QueueClient queueClient = Microsoft.ServiceBus.Messaging.QueueClient::CreateFromConnectionString(connectionString, queueName); Microsoft.ServiceBus.Messaging.BrokeredMessage brokeredMessage = new Microsoft.ServiceBus.Messaging.BrokeredMessage(json); queueClient.Send(brokeredMessage); info("sent"); } catch (xppEx) { str errorMsg = xppEx.Message; info(errorMsg); } catch { info("error"); } } } }
static void SendQueueMessage(Args _args) { str queueName = "myqueue"; str connectionString = "Endpoint=sb:// .servicebus.windows.net/;SharedAccessKeyName= ;SharedAccessKey= "; System.Azure.ServiceBus.Client client = new System.Azure.ServiceBus.Client(connectionString); System.Azure.ServiceBus.QueueClient queueClient = client.getQueueClient(queueName); System.Azure.ServiceBus.QueueMessage queueMessage; str messageBody = "This is the message body"; queueMessage = new System.Azure.ServiceBus.QueueMessage(messageBody); try { queueClient.send(queueMessage); info("Queue message sent successfully."); } catch (Exception::Error) { error("Error sending queue message."); } }
Make sure to replace <servicebus-namespace>
, <shared-access-key-name>
, and <shared-access-key>
with your Azure Service Bus namespace, shared access key name, and shared access key respectively.
In the code above, we create an instance of the System.Azure.ServiceBus.Client
and System.Azure.ServiceBus.QueueClient
classes to establish a connection to the Azure Service Bus. Then, we create a System.Azure.ServiceBus.QueueMessage
object with the desired message body and send it using the send
method of the QueueClient
.
Please note that the code assumes you have the necessary permissions and have referenced the required assemblies for Azure Service Bus in your development environment.
To read messages from a queue in Azure using X++ code, you can utilize the Dynamics 365 Finance and Operations development environment. Here's an example of how you can read messages from a queue in Azure using X++:
static void ReadQueueMessage(Args _args) { str queueName = "myqueue"; str connectionString = "Endpoint=sb:// .servicebus.windows.net/;SharedAccessKeyName= ;SharedAccessKey= "; System.Azure.ServiceBus.Client client = new System.Azure.ServiceBus.Client(connectionString); System.Azure.ServiceBus.QueueClient queueClient = client.getQueueClient(queueName); System.Azure.ServiceBus.QueueMessage[] queueMessages; try { queueMessages = queueClient.receiveMessages(10); // Read up to 10 messages from the queue if (queueMessages && queueMessages.length() > 0) { for (int i = 1; i <= queueMessages.length(); i++) { System.Azure.ServiceBus.QueueMessage message = queueMessages.get(i); str messageBody = message.getBodyAsString(); info("Received message: " + messageBody); message.complete(); // Marks the message as complete and removes it from the queue } } else { info("No messages found in the queue."); } } catch (Exception::Error) { error("Error reading queue messages."); } }
Make sure to replace <servicebus-namespace>
, <shared-access-key-name>
, and <shared-access-key>
with your Azure Service Bus namespace, shared access key name, and shared access key respectively.
In the code above, we create an instance of the System.Azure.ServiceBus.Client
and System.Azure.ServiceBus.QueueClient
classes to establish a connection to the Azure Service Bus. Then, we use the receiveMessages
method of the QueueClient
to retrieve up to 10 messages from the queue. We iterate through the received messages, retrieve the message body using getBodyAsString
, and process each message as needed. After processing a message, we call the complete
method to mark it as complete and remove it from the queue.
Please note that the code assumes you have the necessary permissions and have referenced the required assemblies for Azure Service Bus in your development environment.
class QueueMessageReader { static str connectionString = 'Endpoint=sb://usvd365a1hxdevasb01.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=dbt8OR5p4nQgLYAAjGhCOByDNXF0DfxSG3gFx8WUI6k='; static str queueName = 'ibssalesorder_in'; public static void main(Args _args) { Microsoft.Dynamics.Ax.Xpp.ErrorException xppEx; SysDictClass dictServiceBus = new SysDictClass(classnum(QueueClient)); QueueClient queueClient; Microsoft.ServiceBus.Messaging.BrokeredMessage brokeredMessage; str jsonPayload; while (true) { if (connectionString && queueName) { try { queueClient = dictServiceBus.callStaticMethod('CreateFromConnectionString', connectionString, queueName); brokeredMessage = queueClient.receive(); if (brokeredMessage) { jsonPayload = brokeredMessage.getBody(strfmt('String')); info(strFmt("Received message: %1", jsonPayload)); brokeredMessage.complete(); } } catch (xppEx) { str errorMsg = xppEx.Message; info(errorMsg); brokeredMessage.abandon(); } } } } }