HOWTO: Using SDK 2.0 for Java for MSMQ Asynchronous Receive

ID: Q176815

The information in this article applies to:

SUMMARY

This article shows how to use Microsoft Message Queue Server (MSMQ) ActiveX components with a Java applet and perform asynchronous Receive in Java using ConnectionPointCookie class in the Microsoft Software Development Kit (SDK) 2.0 for Java.

MORE INFORMATION

A Java object can sink events from the ActiveX object by using the com.ms.com.ConnectionPointCookie class included in Java SDK 2.0. The ConnectionPointCookie handles the COM details of attaching the sink to the correct IConnectionPoint on the connectable object.

NOTE: In order to compile the following code, you must use the latest Java compiler included in the SDK 2.0 that includes support for the new ConnectionPointCookie class.

Before you build the sample, run the JActiveX tool on MSMQ type library (Mqoa.dll) to generate Java wrapper classes for the MSMQ ActiveX components.

The sample code creates and opens a queue, sends a message, and receives the message using the MSMQ ActiveX event. The ConnectionPointCookie object is used to sink the ActiveX event.

Generate a default Java AppWizard Applet project using Visual J++ and use the code below to build the sample.

NOTE: Custom error handling is not shown in this code.

   //******************************************************************
   // msmqjavapp.java:   Applet
   // 
   // Note:  This code works only with MSMQ 1.0 and Java SDK 2.0.
   //*******************************************************************

   import java.applet.*;
   import java.awt.*;
   import mqoa.*;
   import com.ms.com.*;

   public class msmqjavapp extends Applet implements _DMSMQEventEvents
   {
   private Thread    m_msmqjavapp = null;
   private boolean m_fStandAlone = false;

   private ConnectionPointCookie cookie = null;
   IMSMQQueueInfo msmqQInfo=(IMSMQQueueInfo)new MSMQQueueInfo();
   IMSMQQueue msmqRead;

   public void start()
   {
   Variant noParam = new Variant();
   noParam.noParam();

   msmqQInfo.setPathName (".\\msmqjavaq");
   try
   {
   msmqQInfo.Create (noParam, noParam);  // Use defaults       }
   catch(Exception e)
   {
   // Add custom exception handling here.
   }
   try
   {
   msmqRead=msmqQInfo.Open ( MQACCESS.MQ_RECEIVE_ACCESS,
   MQSHARE.MQ_DENY_NONE );
   }
   catch(Exception e)
   {
   // Add custom exception handling here.
   }
   MSMQEvent msmqEvent = new MSMQEvent();
   msmqRead.EnableNotification((IMSMQEvent)msmqEvent, noParam,
   noParam);
   try
   {
   cookie = new ConnectionPointCookie(msmqEvent, this,
   Class.forName("mqoa._DMSMQEventEvents"));
   }
   catch(Exception e)
   {
   // Add custom exception handling here.
   }
   SendMsg("Hello World!");
   }
   private void SendMsg(String msg)
   {
   IMSMQQueueInfo msmqQInfo1=(IMSMQQueueInfo)new MSMQQueueInfo();
   msmqQInfo1.setPathName (".\\msmqjavaq");
   IMSMQQueue msmqRead1=msmqQInfo1.Open ( MQACCESS.MQ_SEND_ACCESS,
   MQSHARE.MQ_DENY_NONE );

   IMSMQMessage msmqMsg=(IMSMQMessage)new MSMQMessage();
   Variant msgbody=new Variant();
   msgbody.putString ( msg);
   msmqMsg.setBody ( msgbody );
   msmqMsg.Send ( msmqRead1, new Variant());
   }

   private void RecvMsg()
   {
   IMSMQMessage msmqMsg;
   Variant wantbody=new Variant();
   wantbody.putBoolean( true );
   try
   {
   msmqMsg=msmqRead.Receive ( new Variant(), new Variant(), wantbody,
   new
   Variant());
   //Show message text in the browser's status bar.
   if ( msmqMsg != null )
   showStatus(msmqMsg.getBody().getString());
   }
   catch(Exception e)
   {
   // Add custom exception handling here.
   }
   }
   // Event handling code.
   public void Arrived(Object pdispQueue, int cursor)
   {
   showStatus("Arrived!");
   RecvMsg();
   // Add code to re-enable notifications.
   }
   public void ArrivedError(Object pdispQueue, int lErrorCode, int
   cursor)
   {
   showStatus("ArrivedError!");
   // Add code to re-enable notifications.
   }
   }

REFERENCES

For a description of JActiveX tool, please refer to the Microsoft SDK 2.0 for Java documentation available at this address:

   http://www.microsoft.com/java/sdk/ 

For more information and details on MSMQ ActiveX components, please Refer to the MSMQ SDK Help.

Please refer to the SDK 2.0 for Java and Visual J++ documentation for more details on Java constructs and variants used in the code.

For more details on ActiveX event handling in Java, please refer to the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q179849
   TITLE     : HOWTO: Use connectable objects including ActiveX objects
               in Java

(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Syed Yousuf, Microsoft Corporation

Keywords          : kbcode MQJava MQProg 
Version           : WINNT:1.0
Platform          : winnt
Issue type        : kbhowto

Last Reviewed: June 16, 1999