Library Zone Articles
External Articles
Byte Size

Discovery Zone Catalogue
Diary
Links
Bookstore
Interactive Zone Ask the Gurus
Discussion Groups
Newsletters
Feedback
Etc Cartoons
Humour
COMpetition
Advertising
Site Builder ASP Web Ring ASP Web Ring

Power your site with idr newswire
The Developer's Resource & Community Site
COM XML ASP Java & Misc. NEW: VS.NET
International This Week Forums Author Central Find a Job

Developing an MSMQ Client using VJ++

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


Author: Gopalan Suresh Raj

Gopalan Suresh Raj is a Software Architect, Developer and an active Author.

He is contributing author to a couple of books "Enterprise JavaComputing-Applications and Architecture"(Cambridge University Press, June '99) and "TheAwesome Power of JavaBeans"(Manning, July'98).

His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (https://www.execpc.com/~gopalan)or mail him at [email protected].

Go to Gopalan's pages in Author Central.


Click here

Contribute to IDR:

To contribute an article to IDR, a click here.

To contact us at IDevResource.com, use our feedback form, or email us.

To comment on the site contact our webmaster.

Promoted by CyberSavvy UK - website promotion experts

All content © Copyright 2000 IDevResource.com, Disclaimer notice



Code Project

Java COM integration

Visit our NEW WTL Section

Java COM integration