ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ API Support » Setting and getting the MessageID

Post new topic  Reply to topic
 Setting and getting the MessageID « View previous topic :: View next topic » 
Author Message
Yars13
PostPosted: Thu Nov 04, 2004 1:46 pm    Post subject: Setting and getting the MessageID Reply with quote

Novice

Joined: 27 Sep 2004
Posts: 21

Hi, i'm having some problems putting in my own MessageID when i put the message on the queue, then i set a COD queue and i want to retrieve that messageID. I'm sure i'm just not understanding correctly how things work, so if someone could explain it to me i would really appreciate it. I have tried finding documantation on this or some sample code but have been unsuccessful at either finding anything or understanding what i have found.

I'm putting a message on a queue, then immediately retrieving it from the same queue and then sending a COD to a different queue. Below is my code, hopefully someone can point me in the right direction. Thanks in advance.

Code:

using System;
using IBM.WMQ;

namespace MQSeries
{
   class MQSeriesClass
   {
      DateTime date = DateTime.Now;
      MQQueueManager queueManager;
      MQQueue queue;
      MQMessage queueMessage;
      MQPutMessageOptions queuePutMessageOptions;
      MQGetMessageOptions queueGetMessageOptions;

      static string QueueName;
      static string QueueManagerName;
      static string ChannelInfo;

      static string channelName;
      static string transportType;
      static string connectionName;

      string message = "this is a test";

      static void Main(string[] args)
      {
         QueueName = "STNQMCD.INPUT.SIST";
         

         QueueManagerName = "STNQMCD";
         

         ChannelInfo = "STNCH/TCP/168.169.89.63(1415)";
         

         MQSeriesClass mqSeries = new MQSeriesClass();

         char[] separator = {'/'};
         string[] ChannelParams;
         ChannelParams = ChannelInfo.Split( separator );
         channelName = ChannelParams[0];
         transportType = ChannelParams[1];
         connectionName = ChannelParams[2];

         mqSeries.queueManager = new MQQueueManager( QueueManagerName, channelName, connectionName );
         mqSeries.queue = mqSeries.queueManager.AccessQueue( QueueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING );

         mqSeries.putTheMessageIntoQueue();
         mqSeries.GetMessageFromTheQueue();
         mqSeries.GetResponseFromTheQueue();

         Console.ReadLine();

      }

      public void putTheMessageIntoQueue()
      {

         try
         {
            queueMessage = new MQMessage();
            queueMessage.WriteString( message );
            queueMessage.Format = MQC.MQFMT_STRING;
            queueMessage.MessageId = //????????
            
            queueMessage.ReplyToQueueName = "STNQM.INPUT.COD";
            queueMessage.Report = MQC.MQRO_COD_WITH_FULL_DATA;
            queuePutMessageOptions = new MQPutMessageOptions();
            queuePutMessageOptions.Options = MQC.MQGMO_SYNCPOINT;
            
            //putting the message into the queue
            queue.Put( queueMessage, queuePutMessageOptions );
            Console.WriteLine("Successfully entered the message into the queue ("+date+")\n");
            queueManager.Commit();

         }
         catch(MQException mqexp)
         {
            Console.WriteLine( "MQSeries Exception: " + mqexp.Message );
         }
      }

      public void GetMessageFromTheQueue()
      {
         try
         {
            queue = queueManager.AccessQueue( QueueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING );
         }
         catch(MQException MQExp)
         {
            Console.WriteLine(MQExp.Message);
         }

         queueMessage = new MQMessage();
         queueMessage.Format = MQC.MQFMT_STRING;
         
         queueGetMessageOptions = new MQGetMessageOptions();
         queueGetMessageOptions.Options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_SYNCPOINT;


         try
         {
            queue.Get( queueMessage, queueGetMessageOptions );   

            // Received Message.
            System.Console.WriteLine( "MESSAGE TAKEN FROM QUEUE: " + date+ " \n {0}", queueMessage.ReadString(queueMessage.MessageLength));
            queueManager.Commit();

         }
         catch (MQException MQExp)
         {

            System.Console.WriteLine( "MQQueue::Get ended with " + MQExp.Message );
            if (MQExp.Message.Equals("CompCode: 2, Reason: 2033"))
            {
               System.Console.WriteLine("no more messages on queue");
            }
            else
            {
               queueManager.Backout();
            }
         }
      }

      public void GetResponseFromTheQueue()
      {
         string x = "STNQM.INPUT.COD";
         queue = queueManager.AccessQueue( x, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING );//+ MQC.MQGMO_SYNCPOINT);
         queueMessage = new MQMessage();
         queueMessage.Format = MQC.MQFMT_STRING;
         queueGetMessageOptions = new MQGetMessageOptions();
         queueGetMessageOptions.Options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_SYNCPOINT;


         try
         {
            queue.Get( queueMessage, queueGetMessageOptions );

            System.Console.WriteLine(queueMessage.MessageId);

            // Received Message.
            System.Console.WriteLine( "\nCONFIRMATION OF DELIVERY: "+date+" \n{0}", queueMessage.ReadString(queueMessage.MessageLength));
            queueManager.Commit();


         }
         catch (MQException MQExp)
         {

            System.Console.WriteLine( "MQQueue::Get ended with " + MQExp.Message );
            if (MQExp.Message.Equals("CompCode: 2, Reason: 2033"))
            {
               System.Console.WriteLine("no more messages on queue");
            }
            else
            {
               queueManager.Backout();
            }
         }
      }

   }
}
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Thu Nov 04, 2004 2:23 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

You do not specify SYNCPOINT on the options but use COMMIT.

Do not set the MessageID use it as a read only property. It will be set after you put the message to the queue.

It is pointless to set the format on the inbound message. The message holds its own format.

On the inbound read the MessageID from the message after the get.
Do not expect a COD message. You did not set the COD report options when sending the message.

Hope this helps some.
F.J.
Back to top
View user's profile Send private message Send e-mail
Yars13
PostPosted: Thu Nov 04, 2004 5:23 pm    Post subject: Reply with quote

Novice

Joined: 27 Sep 2004
Posts: 21

I did set the report option to COD in the PutMessageOnQueue method.
Code:
queueMessage.Report = MQC.MQRO_COD_WITH_FULL_DATA;



I see that i use Commit where i shouldn't be, thanks.

I need to be able to set the MessageID and according to MQ .Net manual, the MessageID property has both set and get options. Also, i do recieve a COD when i call a get on the replyto queue and when i try to read the MessageID the only thing that prints out is something like System.Byte[].

I realize that it's probably easier to just let MQ set the unique messageId for me, but that's not what i'm trying to accomplish here, i NEED to be able to set it myself and then be able to retrieve it from the replyto queue.

Thanks
Yars
Back to top
View user's profile Send private message
clindsey
PostPosted: Thu Nov 04, 2004 6:52 pm    Post subject: Reply with quote

Knight

Joined: 12 Jul 2002
Posts: 586
Location: Dallas, Tx

Yars,

You can set it yourself. It is type byte with length 24 and the queue manger pads it if you don't. Try padding out to 24 bytes with spaces and then use the same sequence of bytes on your get. You can also save queueMessage.MessageId after the put and use the saved messageid to set the id before your get call.

Also, the default behavior for COD/COA is for the original messageid to be returned in the correlid field of the reply. A new messageid is generated for the reply. So, you should copy the original messageid into msg.CorrelId prior to the get. You can override this by setting MQRO_PASS_MSG_ID in your report options. Although this will work for COA/COD. I would not recommend this because many server applications are not so well behaved and they don't check this flag in the report options. Most just blindly copy messageid to correlid.

Finally, default behavior also is to match on both messageid and correlid on the get. (MQMO_MATCH_MSG_ID+MQMO_MATCH_CORREL_ID in matchOptions). You can override this or set the messageid to MQMI_NONE which causes it to match on anything.

Charlie
Back to top
View user's profile Send private message
Yars13
PostPosted: Thu Nov 04, 2004 7:28 pm    Post subject: Reply with quote

Novice

Joined: 27 Sep 2004
Posts: 21

Thanks Charlie,

Do you know why i'm getting the output of "System.Byte[]" when i try to retrieve the messageID?
Code:

System.Console.WriteLine(queueMessage.MessageId);

Also, if i wanted to set the messageID to say an integer 199, what would the syntex look like?

Thanks,

Yars
Back to top
View user's profile Send private message
clindsey
PostPosted: Thu Nov 04, 2004 8:09 pm    Post subject: Reply with quote

Knight

Joined: 12 Jul 2002
Posts: 586
Location: Dallas, Tx

Quote:

Do you know why i'm getting the output of "System.Byte[]" when i try to retrieve the messageID

I think it is trying to tell you a byte stream may not be printable as a string.

Add this method to your class and pass the messageid to it
Code:

   static void dumpHexId(byte[] myId) {
      System.out.print("X'");
      for (int i=0; i < myId.length; i++) {
        char b = (char)(myId[i] & 0xFF);
        if (b < 0x10) {
           System.out.print("0");
        }
        System.out.print((String)(Integer.toHexString(b)).toUpperCase());
      }
      System.out.println("'");
   }


Quote:

Also, if i wanted to set the messageID to say an integer 199, what would the syntex look like?


One way to do it is just:
Code:

msg.messageId = {'1','9','9',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};


Also, be sure to re-read the previous post. I edited it to add more to it.

Charlie
Back to top
View user's profile Send private message
Yars13
PostPosted: Thu Nov 04, 2004 8:58 pm    Post subject: Reply with quote

Novice

Joined: 27 Sep 2004
Posts: 21

Charlie, thank you very much, that was very helpful and exactly what i was looking for.

Yars
Back to top
View user's profile Send private message
Yars13
PostPosted: Fri Nov 05, 2004 5:51 am    Post subject: Reply with quote

Novice

Joined: 27 Sep 2004
Posts: 21

For anyone interested, the code that Charlie posted is in Java, not C#, but it made me understand what i'm trying to do.

In the put message i did the following code...
Code:

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();            
            queueMessage.MessageId = encoding.GetBytes("127");


and when i was retrieving the message i used this code...
Code:

System.Text.ASCIIEncoding decode = new System.Text.ASCIIEncoding();

            string id = decode.GetString(queueMessage.CorrelationId);
            System.Console.WriteLine("MessageId: " + id);


Again, thank you for all your help

Yars
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Fri Nov 05, 2004 2:10 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

Yars

Be careful in the way you treat MessageID and CorrelationID.
Even though sometimes they may be Strings disguised as byte arrays the system really treats them as byte arrays.
Now I know this sounds a little bit cryptic.
What it really means is that if you can get a decent String out of a MessageID or CorrelationID it is really chance and happenstance and you cannot rely on it. For exemple a byte array filled with byte 0 would qualify as a valid byte array but certainly not as a valid String...

This is why in JMS you get a String that is in fact a hex representation of each byte in the array.

Enjoy
Back to top
View user's profile Send private message Send e-mail
bower5932
PostPosted: Mon Nov 08, 2004 6:31 am    Post subject: Reply with quote

Jedi Knight

Joined: 27 Aug 2001
Posts: 3023
Location: Dallas, TX, USA

fjb_saper wrote:
Even though sometimes they may be Strings disguised as byte arrays the system really treats them as byte arrays.


They also don't get converted when they flow around various systems.
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ API Support » Setting and getting the MessageID
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.