Make your own free website on Tripod.com

Developing a simple MSMQ Client
Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................Microsoft Visual J++ ver 6.0
.........................................Windows NT 4.0 Options Pack

1. Develop the Client class

.

MSMQClientSink.java
import com.ms.com.*;
import mqoa.*;

public class MSMQClientSink implements mqoa._DMSMQEventEvents, Runnable
{
 
// The ConnectionPointCookie member is global so that it
  // exists for the life of an instance of this class

  private ConnectionPointCookie cookie;
  private IMSMQQueue queue;
  private IMSMQEvent msmqEvent;

  private Variant variantTrue = new Variant();
  private Variant variantNoParam = new Variant();
 
  //====================================================================
  public MSMQClientSink()
  {
   
// Creating the member event object and setting some
    // variables.

    msmqEvent = new MSMQEvent();
    variantTrue.putBoolean(true);
    variantNoParam.noParam();
  }
 
 
  //====================================================================
  public static void main(String[] args)
  {
    try
    {
      Thread sink = new Thread( new MSMQClientSink() );
      sink.start();
      Thread.currentThread().join();
    }
    catch(Exception e)
    {
    }
  }
 
  //====================================================================
  public void Arrived(Object Queue, int Cursor)
  {
    IMSMQMessage message = null;
    IMSMQTransactionDispenser transactionDispenser = new MSMQTransactionDispenser();
    IMSMQTransaction transaction;

    Variant oneSecondDelay = new Variant();
    Variant transactionHolder = new Variant();
 
    oneSecondDelay.putInt(1000);
   
// Casting the Object parameter to the appropriate
    // IMSMQQueue variable

    queue = (IMSMQQueue)Queue;
   
// Starting a transaction
    transaction = transactionDispenser.BeginTransaction();
    try
    {
     
// Wrapping the transaction object in a Variant
      transactionHolder.putDispatch(transaction);
     
// Receiving a message transactionally
      message = queue.Receive(transactionHolder,variantTrue,variantTrue,oneSecondDelay);
 
    
// Committing the transaction
      transaction.Commit(variantNoParam,variantNoParam,variantNoParam);
    }
    catch(Exception transactionFailed)
    {
     
// Aborting the transaction
      transaction.Abort(variantNoParam,variantNoParam);
    }
 
    if(message != null)
    {
     
// Displaying the message body in the windows message box
      com.ms.wfc.ui.MessageBox.show((String)message.getBody().toString());
    }
 
  
  // Resetting the event mechanism
    queue.EnableNotification(msmqEvent,variantNoParam,variantNoParam);
  }
 
  //====================================================================
  public void ArrivedError(Object Queue, int ErrorCode, int Cursor)
  {
    // Display a windows message box with an error message
    com.ms.wfc.ui.MessageBox.show("Error!");
  
  // Resetting the event mechanism
    queue.EnableNotification(msmqEvent,variantNoParam,variantNoParam);
  }
 
  //====================================================================
  public void run()
  {
   
// This is the typeID that will be associated
    // with the existing queue(s)

    String typeID = "{E9E8CB90-D05E-11d2-B9D0-006097A7D34F}";
    Variant guid = new Variant();
    Variant nop = new Variant((int)RELOPS.REL_NOP);
    Variant isEqual = new Variant((int)RELOPS.REL_EQ);
 
    IMSMQQueueInfo queryInfo;
    IMSMQQueueInfos qInfos;
    IMSMQQuery query = new MSMQQuery();
 
    guid.putString(typeID);
    qInfos = query.LookupQueue(variantNoParam, guid,
                               variantNoParam, variantNoParam, variantNoParam,
                               isEqual, nop, nop, nop);
    qInfos.Reset();
    queryInfo = qInfos.Next();
 
    try
    {
      cookie = new ConnectionPointCookie(msmqEvent, this, Class.forName("mqoa._DMSMQEventEvents"));
 
   }
    catch(ClassNotFoundException notFoundException)
    {
 
     // Display a windows message box with an error message
 
     com.ms.wfc.ui.MessageBox.show("Error - ClassNotFoundException!");
    }
 
    while(queryInfo != null)
    {
      queue = queryInfo.Open(MQACCESS.MQ_RECEIVE_ACCESS,MQSHARE.MQ_DENY_NONE);
 
      try
      {
        queue.EnableNotification(msmqEvent, variantNoParam, variantNoParam);
      }
      catch(ComSuccessException comException)
      {
 
     // Display a windows message box with an error message
        com.ms.wfc.ui.MessageBox.show("Error - ComSuccessException!");
      }
 
      queryInfo = qInfos.Next();
    }
  }
 
  //====================================================================
}
 

 

2. Compile and run your client

 

click here to go to the
Developing a Simple MSMQ Server Page...
click here to go to
My MSMQ HomePage...

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since March 01,1999.

Last Updated : Mar 01,'99

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-99, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site,are trademarks of their respective owners.