C# API Subscribe Using webMethods Broker How To Read BytesMessage

I try to use the webmethods broker to get messages from an java jms broker.
This system is distributed by a partner of my client, to whom I don’t have access. I haven’t been able to consume messages, because the message are returned as BytesMessage. How i can read the message and convert to a knowed type or to a string.

Hi Rui,

what about exploring the Built-In-Services Guide for your wM version?

BTW:
Which versions of wM IS and Broker are you using?

Can you describe your scenario a bit more in detail please?

Regards,
Holger

I don’t know the version of wV. My client only gave me the Broker, ClientId, Destination and the instrunctions to create a session with a CreateDurableSubscriber. I can create the connection, create the session, initialize the connection and start receiving messages. However i can’t read the body massage.

In the example below the msg is a BytesMessage, that i can’t extract the body message or convert to a string. Browsing the webMethods.Msg, i can’t found a way to obtain the message. BytesMessages are a type i cant convert into a string.

SAMPLE:

// get a destination
IDestination destination = isQueue ? MsgFactory.GetQueue(“SampleQueue”) : MsgFactory.GetTopic(props.DestinationName);

        // get a connection factory
        IConnectionFactory connectionFactory = MsgFactory.GetConnectionFactory(props.Broker, props.ClientID);

        using (IConnection connection = connectionFactory.CreateConnection())
        {
            
            using (ISession session = connection.CreateSession(false, AcknowledgeMode.Auto))
            {
                using (IMessageConsumer consumer = session.CreateDurableSubscriber(destination, props.DestinationName))
                {
                    // start the connection
                    connection.Start();
                    while (messageCount < messageTotal)
                    {
                        bodyMessage = "";
                        try
                        {
                            // receive a message
                           [b] IMessage msg = consumer.Receive();[/b]

                            if (msg is IMapMessage)
                            {
                                //Console.WriteLine("Message Body: " + ((ITextMessage)message).Text);

                            }
                            else if (msg is ITextMessage)
                            {
                                Console.WriteLine("Message Body: " + ((ITextMessage)msg).Text);
                            }
                            else if (msg is IBytesMessage)
                            {
                                //Console.WriteLine("Message Body: " + ((ITextMessage)message).Text);
                            }
                        }
                        catch (Exception ex)
                        {
                            //HelperLog.Logger.ErrorFormat("Error getting message from step. Message detail {0}", ex);
                        }
                        // increment the counter
                        messageCount++;
                        if (!string.IsNullOrEmpty(bodyMessage))
                        {
                            Process(bodyMessage);
                        }
                    }

                } // close the consumer

            } // close the session

        } // close the connection

Hi,

in your code sample I cannot identify where bodyMessage is populated.
It is only initialzed as empty string.

Regards,
Holger

The bodymessage is populated in the receive method. This method returned a BytesMessage

IMessage msg = consumer.Receive();

Hi Rui,

I am a Java Developer, not a C# Developer.

But from my understanding the isNullOrEmpty is always returning true as bodyMessage only contains a String of lentgh 0 (emptyString).
Processing an empty String does not make sense to me.

Regards,
Holger

Hi, You are correct, there no sense, processing message if it is empty. My mistake and, i already fix it in sample above. But the problem is before, when i receive the message

Hi Rui,

I am still wondering where bodyMessage is populated as I only see its initialization with “”.

bodyMessage is of type String, right?

Then you will have to extract its value from the correct element out of the Message Object.
There should be some methods to extract the data from the message.

Regards,
Holger