0


I am using .NET 3.5 to send message to active mq and i have a java listener which process these messages from the queue.

Header contains

userId - long
type - string
isAdd-bool

Listener is expecting payload to be of datatype long. So i should send datatype long in payload.

Here is the code that i am using to post message to active mq.

string payLoad = "123";    
IConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");
                    using (IConnection connection = factory.CreateConnection())
                    {
                        using (ISession session = connection.CreateSession())
                        {
                            IDestination destination = Apache.NMS.Util.SessionUtil.GetDestination(session
                                , "Message.AddUser");
                            using (IMessageProducer producer = 

session.CreateProducer(destination))
                            {
                            // Start the connection so that messages will be processed.
                            connection.Start();

                            //here i need to pass payload as datatype "long"
                            ITextMessage request = session.CreateTextMessage(payLoad);
                            request.Properties["userid"] = 123;
                            request.Properties["type"] = "USER";
                            request.Properties["isAdd"] = true;
                            producer.Send(request);

                        }

                    }
                }

Currently as per the code, i am sending string as payload.

 ITextMessage request = session.CreateTextMessage(payLoad);

How can i change this code to send payload as data type long? i tried to send payload as object. But i am getting an error when listener picks this message. Error: Invalid stream header is corrupted.

As per the comment, i used IStreamMessage

            int userId = 123;
            IStreamMessage message = session.CreateStreamMessage()
            message.WriteInt64((long)idToUpdate);
            message.Properties.SetLong("userId", (long)userId);
            message.Properties.SetString("type", "USER");
            message.Properties.SetBool("isDelete", true);

Then i looked at tomcat logs and this is what i could see, Header is missing in this. I have only Payload

[Payload=ActiveMQStreamMessage {commandId = 5, responseRequired = true, messageId = ID:XXXXX-60790-
stamp=1400070001928, jms_redelivered=false, userId=123, type=USER, jms_messageId=ID:XXXXX-60790-635356865723233336-0:0:1:2:1}]

As this was not working, i tried using ITextMessage.

int userId = 123;
ITextMessage request = session.CreateTextMessage();
request.Text = idToUpdate.ToString();
request.Properties["companyId"] = (long)userId;
request.Properties["type"] = "USER";
request.Properties["isDelete"] = true;

Here is what i could see in the log.

[Payload=46][Headers={timestamp=1400072608501, id=dde638d2-4036-4e81-a3c8-97937ac11087, isDelete=true, jms_timestamp=1400072608280, jms_redelivered=false, userId=1, type=USER, jms_messageId=ID:xxxx-53593-635356892076410652-0:0:1:2:1}]

I am using Sprint Integration and listener is expecting List as the input parameter

public void updateUser(Message<List<Long>> message) {
    Long userId = (Long) message.getHeaders().get("userId");    
    for (Long userId : message.getPayLoad()) { // exception is thrown here "Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long"
        //doing somethign here with userid
    }
}

How can send payload which is of long datatype?

nimi
  • 5,359
  • 16
  • 58
  • 90
  • That last bit sounds like a bug in the StreamMessage, are you using the latest version of the library? Have you tried a BytesMessage instead. – Tim Bish May 15 '14 at 10:16
  • Thanks for the response Tim, I will try with BytesMessage and keep you posted on the same. – nimi May 15 '14 at 13:41

2 Answers2

0

You can use ObjectMessage instead of TextMessage.

See here:

http://docs.oracle.com/javaee/6/api/javax/jms/ObjectMessage.html

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

You have a couple choices in how you implement that.

  • You can use a BytesMessage and use the read / write methods that operate on longs but there is no type checking there so make sure your client's all do the right thing.
  • You can use a StreamMessage which has read / write for long and will check types and do some type conversions as needed if you want.
  • You can use a MapMessage and store the long in a key / value mapping.
  • You can parse the text of the TextMessage to a long using standard Java code.

You can't really use ObjectMessage between .NET and Java, nor would you want to in this case since it's a simple primitive type.

Tim Bish
  • 17,475
  • 4
  • 32
  • 42
  • I stand corrected. didn't notice that the client was Java – Zohar Peled May 14 '14 at 11:47
  • IStreamMessage message = new ActiveMQStreamMessage(); message.WriteInt64((long)idToUpdate);//1 message.Properties.SetLong("userId", (long)userid);//123 message.Properties.SetString("type", headerType.ToString());// USER message.Properties.SetBool("isDelete", isDeleteAction);//true When i look at my payload, [Payload=ActiveMQStreamMessage {commandId = 5, responseRequired = true, messageId = ID:XXXXX-60790- stamp=1400070001928, jms_redelivered=false, userId=1, type=USER, jms_messageId=ID:XXXXX-60790-635356865723233336-0:0:1:2:1}] I dnt hve headers – nimi May 14 '14 at 12:35
  • I don't know what's that's supposed to mean. – Tim Bish May 14 '14 at 13:28
  • @TimBish, i have edited my question with response to your reply. Could you please look at it? – nimi May 14 '14 at 14:21