Maximo Integration with Azure Service Bus- Part 1/2
If you are looking for integrating Maximo with Azure Service Bus for leveraging the messaging engine - Maximo doesn't have inbuilt options and it requires customization. This will be a good start.
While Integrating Azure Service Bus to Azure based workloads and platforms are not that challenging to accomplish, integration of Azure Service Bus with Java applications could be quite a task.
In this article, I am to illustrate how to integrate a Java based Application with Azure Service Bus. Eventually, we will see how do we integrate this with a Maximo bean.
End of this, you will be able to write your custom code based in Java and be able to send and receive messages from Azure Service Bus. (Due Credits: Arushi Chahal- my co-author). Please subscribe to this page if you haven’t yet.
"Maximo supports Kafka integration out of the box"
What is an Azure Service Bus?
Azure Service Bus is reliable cloud messaging as a service (MaaS) provide in Azure platform. This is like a typical message queue commonly used like Kafka. It’s usually implemented as a pub-sub model.
What You Need:
1.     Java programming skills & familiarity with any IDE tool (Eclipse, NetBeans)
2.     Jar files of Azure Service Bus
3.     Azure Subscription and choice if its topic or queue-based setup
4.     Understanding of messaging queues
5.     Overall time - 2 to 4 hours
Jars Required (as of today):
azure-core-1.11.0
azure-core-amqp-2.0.0
azure-messaging-servicebus-7.0.0
proton-j-0.33.8
reactive-streams-1.0.0
reactor-core-3.3.12
Ports & Certificates Required:
1.     Import Azure certificates in our WebSphere Application Server- this is where we have hosted the Java application. The certificates can be extracted from the Azure URL, just like how we extract certificates using browser.
2.   Open specific port for accessing Azure Service Bus - port # 5671/ 9354 should be open for an SSL transaction to happen.
Exception Handling:
While Microsoft provides ample documentation, you will have difficulties in catching exceptions occurring inside out of the box Microsoft methods. Hence, please take care of the exceptions appropriately.
Sample Code:
package slb.azure.servicebus;Â Â
import com.azure.messaging.servicebus.ServiceBusClientBuilder;
import com.azure.messaging.servicebus.ServiceBusErrorContext;
import com.azure.messaging.servicebus.ServiceBusMessage;
import com.azure.messaging.servicebus.ServiceBusProcessorClient;
import com.azure.messaging.servicebus.ServiceBusReceivedMessage;
import com.azure.messaging.servicebus.ServiceBusReceivedMessageContext;
import com.azure.messaging.servicebus.ServiceBusSenderClient;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.json.JSONException;
import org.json.JSONObject;
public class AzureBusHandler {
               public static String connectionString="Connection String";
               public static String queueName="Queue Name";
               public static void main(String[] args){
                               try {
                                               System.out.println("AzureBusHandler:: Sending Message to queue");
                                               sendMessage();
                                               System.out.println("AzureBugHandler:: Calling receiveMessages");
                                               receiveMessages(connectionString, queueName);
                               } catch (JSONException e) {
                                               e.printStackTrace();
                              } catch (InterruptedException e) {
                                               e.printStackTrace();
                               }
               }
                               // sends message
                               public static void sendMessage() throws JSONException
                               {
                                  // create a Service Bus Sender client for the queue
                                               System.out.println("AzureBusHandler:: Creating a Service Bus Sender client for the topic");
                                  ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
                                          .connectionString(connectionString)
                                          .sender()
                                          .queueName(queueName)
                                          //.topicName(topicName)
                                          .buildClient();
                                  System.out.println("AzureBusHandler::: Before sending message");Â
                                  String message;
                                  JSONObject json = new JSONObject();
                                  json.put("ID", "ID001");
                                  json.put("Value", "Test Message");
                                  message = json.toString();
                                  // send one message to the topic
                                  try{
                                            senderClient.sendMessage(new ServiceBusMessage(message));
                                            System.out.println("AzureBusHandler::: Sent a single message to the queue: " + queueName);
                                  }
                                  catch(Exception e){
                                            System.out.println("AzureBusHandler>>>> Inside catch sendMessage()::: "+e.getMessage());
                                            e.printStackTrace();
                                  }
                               }
 public static void receiveMessages(String connectionString, String queueName) throws InterruptedException {
   System.out.println("AzureBusHandler::: Receiving message from queue");
   try {
     Consumer<ServiceBusReceivedMessageContext> messageProcessor = context -> {
         System.out.println("AzureBusHandler::: Inside messageProcessor");
         ServiceBusReceivedMessage message = context.getMessage();     Â
         String msgContentType= message.getContentType();
         String messageId = message.getMessageId();
         System.out.println("AzureBusHandler::: Content Type=="+msgContentType+ " MessageId== "+messageId);
         String messageBodyStr1= message.getBody().toString();
         System.out.println("AzureBusHandler:::: Received message1: " + messageBodyStr1 + " from the subscription: " + queueName);
       };
     Consumer<ServiceBusErrorContext> errorHandler = throwable -> System.out.println("AzureBusHandler:::: Error when receiving messages: " + throwable.getException());
     System.out.println("AzureBusHandler::: Creating an instance of the processor through the ServiceBusClientBuilder");
     ServiceBusProcessorClient processorClient = (new ServiceBusClientBuilder())
       .connectionString(connectionString)
       .processor()
       .queueName(queueName)      Â
       .processMessage(messageProcessor)
       .processError(errorHandler)
       .buildProcessorClient();
     System.out.println("AzureBusHandler::: Is processor already running? " + processorClient.isRunning());
     processorClient.start();
     System.out.println("AzureBusHandler::: Starting the processor with time lag 20 sec");
     TimeUnit.SECONDS.sleep(20);
     System.out.println("AzureBusHandler::: Stopping and closing the processor");
     processorClient.close();
   } catch (Exception e) {
     System.out.println("AzureBusHandler::: Receive Messages catch>> " + e.getMessage());
     e.printStackTrace();
   } catch (Throwable t1) {
       System.out.println("AzureBusHandler::: Receive Messages catch throwable>> " + t1.getMessage());
       t1.printStackTrace();
     }
 }
}
Issues you might face:
Not able to read message body of JSON messages
Not able to persists the message in the databases
Unable to handle exceptions
Port opening issues
Summary:
With the above you will be able to send and receive messages to Azure Service Bus using a stand-alone Java application. In the next article we will see how to use a Maximo bean to leverage this custom code.
References:
1.     Azure Service Bus messaging overview - Azure Service Bus | Microsoft Docs
2.     Azure Service Bus—Cloud Messaging Service | Microsoft Azure
3.     https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-java-how-to-use-queues
4.     https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-troubleshooting-guide
Thanks for reading, this will help you to get started.
Appreciate you documenting these, thank you!