As a follow-up to the previous article about MapMessage exchange with Apache ActiveMQ, here is an example for an ObjectMessage, sent from a Java JMS client and consumed from a Delphi application.

Java (producer side)

The Java code creates a java.util.Properties instance, fills it with example data,, and sends it to the destination queue as a ObjectMessage. (Note that the class of the transferred must be serializable and must be in the ActiveMQ class path)

Destination dest = session.createQueue("Habari.ObjectMessage");
MessageProducer producer = session.createProducer(dest);

Properties l = new Properties();
l.put("date", new Date());
l.put("info", "hello world!");
msg.setObject(l);

producer.send(msg);

When this code completes, the JMS ObjectMessage instance will wait in the message broker queue for a consumer.

Delphi / Free Pascal (consumer side)

We want to receive the ObjectMessage as XML, so the header is:

transformation=jms-object-xml

Now the broker knows which serialization our client understands. ActiveMQ sends the ObjectMessage to our STOMP client as XML. The java.util.Properties instance contains a list of key-value pairs, which we have filled with two entries on the Java side. The first entry is a String (“hello world!”), stored under the key “info”, the second a java.util.Date instance stored under “date”. The client received this XML payload:

<properties>
  <property name="info" value="hello world!"/>
  <property name="date" value="Mon Jan 09 17:22:18 CET 2017"/>
</properties>

The Delphi / Free Pascal example client receives the message and uses a custom message transformer to convert the incoming XML:

function TDemoObjectTransformer.Decode(const SerializedObject: 
  RawByteString): TObject;
var
  XmlDoc: IXMLDocument;
  Entry: IXMLNode;
  Key, Value: string;
begin
  XmlDoc := LoadXMLData(SerializedObject);

  Entry := XmlDoc.DocumentElement.ChildNodes[1];
  Key := Entry.GetAttribute('name');
  Value := Entry.GetAttribute('value');

  Result := TExampleClass.Create(Key, Value);
end;

Note that the source code is a proof of concept, using experimental features of the Habari Client library. Also note that transferring objects with ObjectMessage also does have its disadvantages.

About Habari Client for ActiveMQ

Habari Client library for ActiveMQ is available from Habarisoft.habari_logo_2016

 

 

 

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s