简体   繁体   中英

How to send an object to ActiveMQ (C#)

I am trying to send an object (Ex: StudentModel with ID, Name, Address,..) to ActiveMQ , I have read many tutorials but not much for c#. I followed a tutorial and it does not work. Here is my code in send form:

    Person p = new Person(123, "vtp", new DateTime(1999, 11, 01));
    IObjectMessage m = session.CreateObjectMessage(p);
    producer.Send(m);

In receive form:

    private void Consumer_Listener(IMessage message)
    {
        if (message is IObjectMessage)
        {            
            IObjectMessage obj = message as IObjectMessage; //1
            Person p = (Person)obj.Body; //2
            MessageBox.Show(p.Hoten); //3
         }
    }

I debug and realize that the code only repeat at statements 1 and 2, not runs at statement 3

Serialized ObjectMessage style messaging with ActiveMQ is really intended for Java based clients. Using the .NET specific ObjectMessage is a risky proposition and should be avoided in almost all cases. If any Java clients were to be consuming those messages the binary payload of the .NET Objects would cause them to be unable to unmarshal the payload and they would NACK the message to the DLQ. Similarly if the broker had a plugin installed that happened to trigger an unmarshal of the message body the process would fail and the message would likely either go the DLQ or the connection would be dropped.

You should consider some other form of payload serialization such as JSON payloads in a TextMessage or a manual binary encode into a BytesMessage which would at least not cause non .NET clients to fail outright on the message itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM