Interoperability between Java World and Microsoft World by implementing a MSMQ Bridge in Visual C++

 

Hi Community,

As Developers, consultants or architects we always have to face challenges when it comes to integrating to existing systems. This is a bit harder when technologies aren’t from the same vendor. This blog post describes an IPC mechanism I had to implement for an integration project that comprised Microsoft technologies and Java.

The beauty of C++ is that one can virtually build anything with it, and almost every technology or platform provides means to be extended through the use of C++. Java is not exception because it provides JNI that allows running native code from the JVM. This would be the equivalent of the DllImportAttribute in .NET.

MSMQ provides reliable delivery of messages and it also provides a SDK that enables developers to implement queues in their applications, below code snippet to delete a queue

1 2 QueueOpResult CMSMQBridge::DeleteQueue(int nQueueId) { 3 HRESULT ret; 4 QueueOpResult retval; 5 retval.isSuccess = false; 6 wchar_t szFormatName[MAX_PATH]; 7 DWORD cbFormatLength = MAX_PATH; 8 hash_map<int, QueueInformation>::const_iterator found = Queues.find(nQueueId); 9 10 if (Queues.size() > 0 || found != Queues.end()) { 11 auto pathName = found->second.Queuepath.c_str(); 12 auto result = MQPathNameToFormatName(pathName, &szFormatName[0], &cbFormatLength); 13 if (SUCCEEDED((ret = MQDeleteQueue(szFormatName)))) { 14 retval.hr = S_OK; 15 retval.isSuccess = true; 16 Queues.erase(found); 17 } else { 18 CreateFailedQueueOpResult(retval, ret); 19 } 20 } else { 21 retval.isSuccess = false; 22 retval.errMessage = L"Queue is empty or Queue Id wasn't found"; 23 } 24 25 return retval; 26 }

Once we run “DeleteQueue” unit test and set a breakpoint inside the method, we can clearly see that our library is interacting with MSMQ effectively

image

So the way all pieces hang together is as follows:

  • Native Visual C++ Library provides an interface which is consumed from Java through JNI (source code here)
  • Java sends messages to Microsoft application using a queue.
  • .NET application provides classes to interact and use MSMQ (e.g.: MessageQueue class).

And once again, Visual C++ has saved the day Smile

Angel

Leave a Reply

Your email address will not be published. Required fields are marked *