Many messaging applications need to exchange data across
languages and platforms, using the native datatypes of each
programming language. AMQP provides a set of portable datatypes,
but does not directly support a set of named type/value
pairs. Java JMS provides the MapMessage
interface, which allows sets of named type/value pairs, but does
not provide a set of portable datatypes.
The Qpid Messaging API supports maps in message
content. Unlike JMS, any message can contain maps. These maps
are supported in each language using the conventions of the
language. In Java, we implement the
MapMessage
interface; in Python, we
support dict
and
list
in message content; in C++, we
provide the Variant::Map
and
Variant::List
classes to represent maps
and lists. In all languages, messages are encoded using AMQP's
portable datatypes.
Because of the differences in type systems among languages, the simplest way to provide portable messages is to rely on maps, lists, strings, 64 bit signed integers, and doubles for messages that need to be exchanged across languages and platforms.
In Python, Qpid supports the dict
and list
types directly in message content. The following code shows how to send these structures in a message:
Example 2.14. Sending Qpid Maps in Python
from qpid.messaging import * # !!! SNIP !!! content = {'Id' : 987654321, 'name' : 'Widget', 'percent' : 0.99} content['colours'] = ['red', 'green', 'white'] content['dimensions'] = {'length' : 10.2, 'width' : 5.1,'depth' : 2.0}; content['parts'] = [ [1,2,5], [8,2,5] ] content['specs'] = {'colors' : content['colours'], 'dimensions' : content['dimensions'], 'parts' : content['parts'] } message = Message(content=content) sender.send(message)
The following table shows the datatypes that can be sent in a Python map message, and the corresponding datatypes that will be received by clients in Java or C++.
Table 2.4. Python Datatypes in Maps
Python Datatype | → C++ | → Java |
---|---|---|
bool | bool | boolean |
int | int64 | long |
long | int64 | long |
float | double | double |
unicode | string | java.lang.String |
uuid | qpid::types::Uuid | java.util.UUID |
dict | Variant::Map | java.util.Map |
list | Variant::List | java.util.List |
In C++, Qpid defines the the
Variant::Map
and
Variant::List
types, which can be
encoded into message content. The following code shows how to
send these structures in a message:
Example 2.15. Sending Qpid Maps in C++
using namespace qpid::types; // !!! SNIP !!! Message message; Variant::Map content; content["id"] = 987654321; content["name"] = "Widget"; content["percent"] = 0.99; Variant::List colours; colours.push_back(Variant("red")); colours.push_back(Variant("green")); colours.push_back(Variant("white")); content["colours"] = colours; Variant::Map dimensions; dimensions["length"] = 10.2; dimensions["width"] = 5.1; dimensions["depth"] = 2.0; content["dimensions"]= dimensions; Variant::List part1; part1.push_back(Variant(1)); part1.push_back(Variant(2)); part1.push_back(Variant(5)); Variant::List part2; part2.push_back(Variant(8)); part2.push_back(Variant(2)); part2.push_back(Variant(5)); Variant::List parts; parts.push_back(part1); parts.push_back(part2); content["parts"]= parts; Variant::Map specs; specs["colours"] = colours; specs["dimensions"] = dimensions; specs["parts"] = parts; content["specs"] = specs; encode(content, message); sender.send(message, true);
The following table shows the datatypes that can be sent in a C++ map message, and the corresponding datatypes that will be received by clients in Java and Python.
Table 2.5. C++ Datatypes in Maps
C++ Datatype | → Python | → Java |
---|---|---|
bool | bool | boolean |
uint16 | int | long | short |
uint32 | int | long | int |
uint64 | int | long | long |
int16 | int | long | short |
int32 | int | long | int |
int64 | int | long | long |
float | float | float |
double | float | double |
string | unicode | java.lang.String |
qpid::types::Uuid | uuid | java.util.UUID |
Variant::Map | dict | java.util.Map |
Variant::List | list | java.util.List |