Microsoft KB Archive/176815

= HOWTO: Using SDK 2.0 for Java for MSMQ Asynchronous Receive =

Article ID: 176815

Article Last Modified on 11/17/2005

-

APPLIES TO


 * Microsoft Message Queue Server 1.0

-



This article was previously published under Q176815



SUMMARY
This article discusses 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 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. }  try {  MSMQEvent msmqEvent = new MSMQEvent; cookie = new ConnectionPointCookie(msmqEvent, this,  Class.forName("mqoa._DMSMQEventEvents")); msmqRead.EnableNotification((IMSMQEvent)msmqEvent, noParam,  noParam); }  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. }  }

