How to Read Xml and Json Messages From the Queue

I recently wrote a series of blog posts that introduced the elements of Solace letters including bulletin header properties and how to encode data in the payload.  Now I'd similar to dig a picayune deeper and talk near using text messages in Solace. Text messages offer more than than just simple cord transfer: mutual formats such as JSON and XML allow the transfer of complex data as strings with readily bachelor libraries to write, read and parse the data for you.

Performance

Text messages offer simpler, quicker and perhaps more agile development.  However, they are slower to write, read and parse, and they add together extra latency and tend to be bigger than the equivalent binary or structured types.

Text Letters in Different Protocols

Solace encodes Test Letters using UTF-eight.  Solace Enterprise APIs (Coffee, JavaRTO, JMS, C, C#) interoperate seamlessly with Text Messages and hide the encoding for you.

Javascript requires a little trick: check the message is a solace.MessageType.TEXT, and so call getSdtContainer() which returns a solace.SDTField.  This will be a string which is the Text message payload.

With Residuum, of course, everything is text.  Binary data is dealt with via Content Types.  Solace routers utilize the HTTP Content-Type header in a REST message to determine the SMF bulletin blazon:  in general text and application/json, application/xml and application/soap+xml are mapped to a TextMessage while everything else is mapped to a BytesMessage.  Similarly TextMessages are mapped to text/plain.  Override this behavior with the HTTP_Content_Type property (eastward.g. for JSON)  Use the Map and Stream messages aren't supported over Remainder.  Run into the encoding department of the Rest Messaging Protocol Guide for more than details.

It is not recommended to transport Text Messages to other APIs such equally MQTT. However, message size tin be dealt with using compression (see beneath).  Unless you are focused entirely on performance, information technology is unwise to discount using text.  This commodity aims to show you lot how and why.

XML, JSON… what format?

The first option to make when using a text message is what format will y'all apply.  Plain text works well for simpler data, but you will end upwards parsing it yourself, and so using an existing format has large advantages.  Ii common formats are XML and JSON.

Some mutual XML libraries are:

  • For C: libxml2 and expat
  • For .Cyberspace: XDocument
  • For Coffee: JAXB
  • For Javascript: DOMParser

JSON is, of course, native to Javascript.  Common libraries in other languages:

  • For C: JSON-C
  • For .Net: NET
  • For Java: Google GSON

I'm not recommending any particular library or format hither, only giving some examples to help y'all get started.  For the rest of this post I'll apply JSON, but the majority of points utilize to XML and probably to most other text format packages as well. If you're interested in learning more hither's an introduction to JSON.

A use example

To illustrate the concepts, let's imagine the employ instance of writing a system to track, route and allocate delivery vehicles.  Let's ascertain our vehicle object:

public form VehicleRecord { 	private grade LatLong { 		private float Lat; 		private float Lng;  		public void setLatLong(float lat,  float lng) { 			this.Lat = lat; 			this.Lng = lng; 		} 	} 	public class Parcel { 		individual String sender; 		private String recipient; 		private LatLong deliveryLatLong; 		public Parcel() { 			… 		} 	} 	individual LatLong currentLatLong; 	     private String depotName; 	private LatLong depotLatLong;  	private String destinationName; 	private LatLong destLatLong;  	individual String commuter; 	     individual Vector<Parcel> manifest; }        

All we demand to do is utilize Gson to catechumen this to JSON:

import com.google.gson.Gson;  Gson gson = new GsonBuilder().create(); gson.toJson(VehicleRecordObject);        

Sending this as a bulletin is so trivial:

VehicleRecord vehicleObject = new VehicleRecord();  // Populate…  TextMessage msg = 	JCSMPFactory.onlyInstance().createMessage(TextMessage.grade); msg.setText(gson.toJson(VehicleObject)); … producer.Send(msg,  destination);        

Reception

One time we've got this message in the onReceive callback, we need to deserialize it.  The most important part is to make certain we cast the BytesXMLMessage to a TextMessage to get the text payload:

vehicleRecord vehicleRcvd;  public void onReceive(BytesXMLMessage msg) { 	if (msg instanceof TextMessage) { 		try { 			vehicleRcvd = gson.fromJson( 				((TextMessage) msg).getText(),  				VehicleRecord.course); 		} take hold of (…) {…} 	} }        

Now we have our original object in vehicleRcvd, with hardly any effort at all. Quick, easy, and powerful!

Java form every bit the message protocol definition

If you're using the same language on both the sending and receiving side, information technology's a simple matter to use the same definition of the object (in this instance VehicleRecord) on both sides.

For Coffee, this is the class definition.  Your form definition serves as a message format specification while providing you with all the normal object access and manipulation, which is pretty cool.

What if I modify my form?

What this doesn't requite yous is any version control over this format.  If you change the format (for case, add a field) applications will probably fail to parse the message unless you update all the applications at the same time, .

So information technology is well worth working out how you'll deal with this at the first of the project.  You can decorate the message with a User Divers Header Field, or you tin use the message topic.  I'd recommend using topics and then you don't accept to worry about treatment unknown formats in your business logic.  It's a simple matter to utilize something like:

vehicletrack/straight/json/vehicleRecord/1.two/…        

That way, a version compiled with version i.2 of the vehicleRecord class will only receive that version.  With a fiddling cleverness, y'all can comprise the version in to your build scripts so the version topic level is automatically filled with the version number.

What if I have different APIs?

The C-manner JSON parsers tend to generate a tree structure or allow iterative admission to fields. This means you don't have a straight one-to-one mapping between sent and received objects. With C# and C++ you will at least take classes to compare against.

Compression

Text formats tend to exist very chatty.  All the same, this very chattiness lends itself very nicely to pinch. Solace APIs and routers implement pinch, which makes things easier.  It'south like shooting fish in a barrel to enable:

  • JCSMPChannelProperties.setCompressionLevel(9);

In that location are 9 pinch levels, with ix being highest effort compression.

I created 30 messages of unlike sizes from 700B to 5kB, for a total information set size of 85kB. With this test there was no real difference in the levels, with all giving a pinch ratio of shut to xx times.

In comparison, using a mapMessage to create this object saved about xx% in size compared to the uncompressed bulletin, at the expense of considerably more code to serialise and deserialise the message.  In this case, of course, most of the information are strings. There volition, all the same, be more CPU utilise to parse the text with a TextMessage compared to a map message.

Closing curly brace

In all only the most CPU intensive or latency sensitive applications, text messages, along with text formats such as JSON and XML with their associated libraries, offer an easy to develop, easy to debug design for data payloads.   If yous're worried about bandwidth consumption, Solace'southward pinch facilities offering an extremely piece of cake to use and high operation way to reduce the payload size.  Use text letters where you tin can!

Array ( [30] => Array ( [name] => Tom Fairbairn [picture] => [bio] =>

Tom works in Solace's systems compages team helping customers define their architectures to address challenges such every bit the increasing digitisation of business across a wide range of industries including retail, financial services and smart metropolis initiatives. He joined Solace in 2013 as part of the Singapore Technical Back up team, then moved to London in 2014.

If you ain a smart phone or tablet, the chances are yous already utilize Tom's work, as prior to Solace Tom was an IC designer responsible for implementing the low-power hardware in the processors nowadays in over 90% of these devices.

[position] => [url] => https://solace.com/blog/author/tom-fairbairn/ ) )

Tom works in Solace's systems architecture team helping customers define their architectures to accost challenges such as the increasing digitisation of business organisation across a wide range of industries including retail, financial services and smart city initiatives. He joined Solace in 2013 as part of the Singapore Technical Support team, and so moved to London in 2014.

If you ain a smart telephone or tablet, the chances are you lot already use Tom's piece of work, as prior to Solace Tom was an IC designer responsible for implementing the depression-ability hardware in the processors present in over 90% of these devices.

gerhardalred1966.blogspot.com

Source: https://solace.com/blog/text-data-solace-apis-xml-json/

0 Response to "How to Read Xml and Json Messages From the Queue"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel