Article ID: 176816
Article Last Modified on 3/11/2005
import mqoa.* ;
import com.ms.com.*;
import com.ms.mtx.*;
import javasample1.*;
class MQSendRcv implements IMQJavaSpl
{
public String MQSend (String strSend)
{
try
{
IMSMQQueueInfo qinfoSend = (IMSMQQueueInfo) new
MSMQQueueInfo();
IMSMQQueue qSend;
IMSMQMessage qmsgMessage = (IMSMQMessage) new MSMQMessage();
// Add code for database updates if needed.
// Open destination queue with MQ_SEND_ACCESS
// and MQ_DENY_NONE options.
qinfoSend.putPathName ("myMachine\\Localx") ;
qSend = qinfoSend.Open(MQACCESS.MQ_SEND_ACCESS,
MQSHARE.MQ_DENY_NONE) ;
if (qSend != null)
{
// Set up variant for the Body property.
Variant vBody = new Variant();
vBody.putString (strSend) ;
// Assemble message.
qmsgMessage.putBody (vBody) ;
qmsgMessage.putLabel ("Java Message") ;
// Select the transaction option. If you are in a
// transactional component, you need to use
// MQ_MTS_TRANSACTION,
// otherwise, since you are sending to a transactional
// queue,
// you need to use MQ_SINGLE_MESSAGE.
Variant vTransaction = new Variant();
if (MTx.GetObjectContext().IsInTransaction())
vTransaction.putInt(MQTRANSACTION.MQ_MTS_TRANSACTION) ;
else
vTransaction.putInt(MQTRANSACTION.MQ_SINGLE_MESSAGE) ;
// Send the message.
qmsgMessage.Send (qSend, vTransaction) ;
qSend.Close() ;
// Commit the transaction.
MTx.GetObjectContext().SetComplete();
return "Message Sent" ;
}
else
{
// Abort the transaction.
MTx.GetObjectContext().SetAbort();
return "Couldn't open queue" ;
}
}
catch (ComFailException e)
{
// Abort the transaction.
MTx.GetObjectContext().SetAbort();
return "An Error Occurred" ;
}
}
public String MQReceive (String[] strRcv)
{
try
{
IMSMQQueueInfo qinfoRcv = (IMSMQQueueInfo) new
MSMQQueueInfo();
IMSMQQueue qRcv;
IMSMQMessage qmsgMessage ;
// Add code for database updates if needed.
// Open destination queue with MQ_RECEIVE_ACCESS
// and MQ_DENY_NONE options.
qinfoRcv.putPathName ("myMachine\\Localx") ;
qRcv = qinfoRcv.Open(MQACCESS.MQ_RECEIVE_ACCESS,
MQSHARE.MQ_DENY_NONE) ;
if (qRcv != null)
{
// Create and set up the Variant parameters
// required by the Receive method.
Variant vTransaction = new Variant();
Variant vWantDestQueue = new Variant();
Variant vWantBody = new Variant();
Variant vReceiveTimeout = new Variant();
vTransaction.putInt (MQTRANSACTION.MQ_MTS_TRANSACTION) ;
vWantDestQueue.putBoolean(false) ; // You do not want the
// destination queue.
vWantBody.putBoolean(true) ; // You want the message body.
vReceiveTimeout.putInt(0) ; // You do not want to wait for
// a message.
// Receive the message & close queue.
qmsgMessage = qRcv.Receive(vTransaction, vWantDestQueue,
vWantBody, vReceiveTimeout) ;
qRcv.Close() ;
// Process message if you received one.
if (qmsgMessage != null)
{
// Get the message body (it's a Variant).
Variant vBody;
vBody = qmsgMessage.getBody () ;
// Get the string (since you know that's what you sent),
// and return it.
strRcv[0] = vBody.getString ();
MTx.GetObjectContext().SetComplete();
return "Message Received" ;
}
else
{
MTx.GetObjectContext().SetComplete();
return "No Message Received" ;
}
}
else
{
MTx.GetObjectContext().SetAbort();
return "Couldn't open queue" ;
}
}
catch (ComFailException e)
{
MTx.GetObjectContext().SetAbort();
return "An Error Occurred" ;
}
}
}
174387 INFO: Using a Single MSMQ Queue for an MTS Component
Additional query words: viper Falcon
Keywords: kbhowto kbjava kbcode KB176816