Creating JMS‑Compatible Messages With Other APIs
This section shows code samples of how non-JMS PubSub+ Messaging APIs can create and set data on messages so that the PubSub+ Messaging API for JMS can interpret those messages as one of the following standard message types:
BytesMessage
The following examples show how to create and receive a BytesMessage using non-JMS PubSub+ Messaging APIs.
PubSub+ JCSMP API
Create:
BytesMessage message = JCSMPFactory.onlyInstance().createMessage(BytesMessage.class);
message.setData(new byte[] {0x01, 0x02});
Receive:
FlowReceiver receiver = session.createFlow(endpoint, topic, null); receiver.start(); BytesMessage message = (BytesMessage)receiver.receive(); byte[] data = message.getData();
PubSub+ C API
Create:
solClient_opaqueMsg_pt msg_p = NULL;
char* data[2];
// create a message
if (solClient_msg_alloc(&msg_p) == SOLCLIENT_OK)
{
// copy the content of data to the binary attachment
if ( solClient_msg_setBinaryAttachment (msg_p,data,(solClient_uint32_t)2) == SOLCLIENT_OK)
{
// at this point the message is ready to be sent
}
}
Receive:
// binary attachment buffer pointer and size
void *dataBuf_p;
solClient_uint32_t dataBufSize;
// Assuming that msg_p is a reference to a received message
if (solClient_msg_getBinaryAttachmentPtr(msg_p, &dataBuf_p, &dataBufSize ) == SOLCLIENT_OK)
{
// at this point dataBuf_p and dataBufSize can be used to access or copy out
// the binary attachment
}
PubSub+ .NET API
Create:
// create a message
IMessage message = ContextFactory.Instance.CreateMessage();
byte[] data = {0x01, 0x02};
message.BinaryAttachment = data;
// Set other message properties
// ...
// at this point the message is ready to be sent
Receive:
// assuming that message is a reference to a received message
IMessage message = args.Message; /*args is a MessageEventArgs*/
if (SDTUtils.GetContainer(message) == null && SDTUtils.GetText(message) == null)
{
// it is not a map, stream or text message
Byte[] rxData = message.BinaryAttachment;
}
PubSub+ JavaScript API
Create:
// the binary data var data; // create a message var message = solace.SolclientFactory.createMessage(); // assuming that data the user added content to data message.setBinaryAttachment(data); // Set other message properties // ... // At this point the message is ready to be sent
Receive:
// assuming that message is a reference to a received message
var message;
if (message.getType() == solace.MessageType.BINARY)
{
// rxData will be the JavaScript string representing the binary data
var rxData = message.getBinaryAttachment();
}
MapMessage
The following examples show how to create and receive a MapMessage using non-JMS PubSub+ Messaging APIs.
PubSub+ JCSMP API
Create:
MapMessage message = JCSMPFactory.onlyInstance().createMessage(MapMessage.class);
SDTMap map = JCSMPFactory.onlyInstance().createMap();
map.putBoolean("success", true);
message.setMap(map);
Receive:
FlowReceiver receiver = session.createFlow(endpoint, topic, null);
receiver.start();
MapMessage message = (MapMessage )receiver.receive();
if (message !=null) {
SDTMap rcvdMap = message.getMap();
...
}
PubSub+ C API
Create (message independent container):
solClient_opaqueMsg_pt msg_p = NULL;
solClient_opaqueContainer_pt mapContainer = NULL;
char map[1024];
// create a message
if (solClient_msg_alloc(&msg_p) == SOLCLIENT_OK)
{
// create a map container
if (solClient_container_createMap (&mapContainer, map, sizeof(map) == SOLCLIENT_OK)
{
// populate the map container
// ...
// set the map container as message payload
if (solClient_msg_setBinaryAttachmentContainer (msg_p, mapContainer) == SOLCLIENT_OK)
{
// at this point the message is ready to be sent
}
}
}
Create (message dependent container):
solClient_opaqueMsg_pt msg_p = NULL;
solClient_opaqueContainer_pt mapContainer = NULL;
// create a message
if (solClient_msg_alloc(&msg_p) == SOLCLIENT_OK)
{
// create a map container
if (solClient_msg_createBinaryAttachmentMap (msg_p, &mapContainer, 1024) == SOLCLIENT_OK)
{
// populate the map container
// ...
// at this point the message is ready to be sent
}
}
Receive:
solClient_opaqueContainer_pt mapContainer = NULL;
// Assuming that msg_p is a reference to a received message
if (solClient_msg_getBinaryAttachmentMap (msg_p, &mapContainer) == SOLCLIENT_OK)
{
// At this point mapContainer is accessible using SDT accessor functions
// ...
}
PubSub+ .NET API
Create (message independent container):
// create a message
IMessage message = ContextFactory.Instance.CreateMessage();
// create a map container and populate it
IMapContainer map = SDTUtils.CreateMap(1024);
// set the map as the message payload
SDTUtils.SetSDTContainer(message,map);
Create (message dependent container):
// create a message IMessage message = ContextFactory.Instance.CreateMessage(); // create a map container and populate it IMapContainer map = SDTUtils.CreateMap(message,1024); // no need to add the map to the message, it is already in the payload
Receive:
// assuming that message is a reference to a received message
IMessage message = args.Message; /*args is a MessageEventArgs*/
ISDTContainer container = SDTUtils.GetContainer(message);
if (container != null && container is IMapContainer)
{
// typecast the container to IMapContainer
IMapContainer map = (IMapContainer) container;
}
PubSub+ JavaScript API
Create:
// create the map container var map = new solace.SDTMapContainer(); // create a message var message = solace.SolclientFactory.createMessage(); // assuming the map contains entries message.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.MAP, map)); // set other message properties // ... // at this point the message is ready to be sent
Receive:
// assuming that message is a reference to a received message
var message;
if (message.getType() == solace.MessageType.MAP)
{
var rxMap = message.getSdtContainer();
}
StreamMessage
The following examples show how to create and receive a StreamMessage using non-JMS PubSub+ Messaging APIs.
PubSub+ JCSMP API
Create:
StreamMessage message = JCSMPFactory.onlyInstance().createMessage(StreamMessage.class); SDTStream stream = JCSMPFactory.onlyInstance().createStream(); stream.writeBoolean(true); message.setStream(stream);
Receive:
FlowReceiver receiver = session.createFlow(endpoint, topic, null); receiver.start(); BytesMessage message = (BytesMessage)receiver.receive(); byte[] data = message.getData();
PubSub+ C API
Create (message independent container):
solClient_opaqueMsg_pt msg_p = NULL;
solClient_opaqueContainer_pt streamContainer = NULL;
char stream[1024];
// create a message
if (solClient_msg_alloc(&msg_p) == SOLCLIENT_OK)
{
// create a stream container
if (solClient_container_createStream (&streamContainer, stream, sizeof(stream) == SOLCLIENT_OK)
{
// populate the stream container
// ...
// set the stream container as message payload
if (solClient_msg_setBinaryAttachmentContainer (msg_p, streamContainer) == SOLCLIENT_OK)
{
// at this point the message is ready to be sent
}
}
}
Create (message dependent container):
solClient_opaqueMsg_pt msg_p = NULL;
solClient_opaqueContainer_pt streamContainer = NULL;
// create a message
if (solClient_msg_alloc(&msg_p) == SOLCLIENT_OK)
{
// create a stream container
if (solClient_msg_createBinaryAttachmentStream (msg_p, &streamContainer, 1024) == SOLCLIENT_OK)
{
// populate the stream container
// ...
// at this point the message is ready to be sent
}
}
Receive:
solClient_opaqueContainer_pt streamContainer = NULL;
// Assuming that msg_p is a reference to a received message
if (solClient_msg_getBinaryAttachmentStream (msg_p, &streamContainer) == SOLCLIENT_OK)
{
// At this point streamContainer is accessible using SDT accessor functions
// ...
}
PubSub+ .NET API
Create (message independent container):
// create a message IMessage message = ContextFactory.Instance.CreateMessage(); // create a stream container and populate it IStreamContainer map = SDTUtils.CreateStream(1024); // set the stream as the message payload SDTUtils.SetSDTContainer(message,stream);
Create (message dependent container)
// create a message IMessage message = ContextFactory.Instance.CreateMessage(); // create a stream container and populate it IStreamContainer map = SDTUtils.CreateStream(message,1024); // no need to add the stream to the message, it is already in the payload
Receive:
// assuming that message is a reference to a received message
IMessage message = args.Message; /*args is a MessageEventArgs*/
ISDTContainer container = SDTUtils.GetContainer(message);
if (container != null && container is IStreamContainer)
{
// typecast the container to IStreamContainer
IStreamContainer map = (IStreamContainer) container;
}
PubSub+ JavaScript API
Create:
// create the stream container var stream = new solace.SDTStreamContainer(); // create a message var message = solace.SolclientFactory.createMessage(); // assuming the stream contains entries message.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STREAM, stream)); // set other message properties // ... // at this point the message is ready to be sent
Receive:
// assuming that message is a reference to a received message
var message;
if (message.getType() == solace.MessageType.STREAM)
{
var rxStream = message.getSdtContainer();
}
TextMessage
The following examples show how to create and receive a TextMessage using non-JMS PubSub+ Messaging APIs.
PubSub+ JCSMP API
This sample requires the JMS connection factory setting text-msg-xml-payload to be set to false, which specifies that the TextMessage string is set/get in the messageʼs binary attachment.
Create:
TextMessage message = JCSMPFactory.onlyInstance().createMessage(TextMessage.class);
message.setText("This is a text message.");
Receive:
FlowReceiver receiver = session.createFlow(endpoint, topic, null); receiver.start(); TextMessage message = (TextMessage)receiver.receive(); String text = message.getText();
PubSub+ C API
This sample requires the JMS connection factory setting text-msg-xml-payload to be set to false, which specifies that the TextMessage string is set/get in the messageʼs binary attachment.
Create:
solClient_opaqueMsg_pt msg_p = NULL;
char* text = "This is a text message.";
// create a message
if (solClient_msg_alloc(&msg_p) == SOLCLIENT_OK)
{
if (solClient_msg_setBinaryAttachmentString (msg_p, text )== SOLCLIENT_OK)
{
// at this point the message is ready to be sent
}
}
Receive:
// pointer to the string in the payload
const char *textStr;
// Assuming that msg_p is a reference to a received message
if (solClient_msg_getBinaryAttachmentString (msg_p, &textStr) == SOLCLIENT_OK)
{
// at this point textStr points to the contained string in the
// text message payload
// ...
}
PubSub+ .NET API
This sample requires the JMS connection factory setting text-msg-xml-payload to be set to false, which specifies that the TextMessage string is set/get in the messageʼs binary attachment.
Create:
// create a message IMessage message = ContextFactory.Instance.CreateMessage(); // and set the text SDTUtils.SetText(message,"This is a text message.");
Receive:
// assuming that message is a reference to a received message
IMessage message = args.Message; /*args is a MessageEventArgs*/
String textStr = SDTUtils.GetText(message);
if (textStr != null)
{
// message is a text message
}
PubSub+ JavaScript API
This sample requires the JMS connection factory setting text-msg-xml-payload to be set to false, which specifies that the TextMessage string is set/get in the messageʼs binary attachment.
Create:
// create a message var message = solace.SolclientFactory.createMessage(); // set content message.setSdtContainer(solace.SDTField.create(solace.SDTFieldType.STRING, "This is a text message.")); // set other message properties // ... // at this point the message is ready to be sent
Receive:
// assuming that message is a reference to a received message
var message;
if (message.getType() == solace.MessageType.TEXT)
{
var txtStr = message.getSdtContainer().getValue();
}
ObjectMessage
Non-JMS PubSub+ Messaging APIs do not have the ability to create a message that the PubSub+ JMS API would interpret as an ObjectMessage.
Non-Standard Messages
When the PubSub+ JMS API cannot interpret a message sent by a non-JMS PubSub+ Messaging API as a JMS BytesMessage, MapMessage, StreamMessage, or TextMessage, the message is handled as a predetermined default message type according to its message payload. This message conversion is described in the following table:
| XML Payload Present? | Binary Attachment Present? | Convert to Message Type... |
|---|---|---|
|
Yes |
Yes |
TextMessage |
|
Yes |
No |
TextMessage |
|
No |
Yes |
BytesMessage |
|
No |
No |
Message (contains only header information) |