Universal Messaging 10.1 | Developer Guide | Enterprise APIs | Enterprise Developer's Guide for Java | Provider for JMS | JMS Message Type Conversion
 
JMS Message Type Conversion
JMS message types are exposed so that you can publish a native nConsumeEvent and have it received by JMS subscribers in the specified type instead of in the default BytesMessage type.
The JMS Message types are assigned integer values as shown below. The integer values can be used directly when setting the message type, or accessed via the following public static constants in nEventAttributes:
JMS_BASE_MESSAGE_TYPE = 0
JMS_MAP_MESSAGE_TYPE = 1
JMS_BYTES_MESSAGE_TYPE = 2
JMS_OBJECT_MESSAGE_TYPE = 3
JMS_STREAM_MESSAGE_TYPE = 4
JMS_TEXT_MESSAGE_TYPE = 5
You can set the message type on an nConsumeEvent in the following way (using the message type JMS_OBJECT_MESSAGE_TYPE as an example):
nEventAttributes eventAttributes = new nEventAttributes();
eventAttributes.setMessageType(nEventAttributes.JMS_OBJECT_MESSAGE_TYPE);
nConsumeEvent evt = new nConsumeEvent("Message", bytes);
evt.setAttributes(eventAttributes);
The data portion of the nConsumeEvent will contain the message body and will need to be set according to the message type as described in the following sections:
JMS_BASE_MESSAGE_TYPE
Publisher:
nEventAttributes attributes = new nEventAttributes();
attributes.setMessageType(nEventAttributes.JMS_BASE_MESSAGE_TYPE);
nConsumeEvent evt = new nConsumeEvent("Message", byteArray);
evt.setAttributes(attributes);
JMS Subscriber:
Message message = topicConsumer.receive(2000);
MessageImpl baseMessageImpl = (MessageImpl)message;
baseMessageImpl.getBuffer();
The buffer will be equal to the byteArray sent in the data payload of the nConsumeEvent.
JMS_BYTES_MESSAGE_TYPE
Publisher:
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(out);
os.writeInt(32);
os.writeInt(1); //true
os.writeBoolean(true);
os.flush();

nEventAttributes attributes = new nEventAttributes();
attributes.setMessageType(nEventAttributes.JMS_BYTES_MESSAGE_TYPE);
nConsumeEvent evt = new nConsumeEvent("Message", out.toByteArray());
evt.setAttributes(attributes);
JMS Subscriber:
Message message = topicConsumer.receive(2000);
BytesMessage bytemessage = (BytesMessage)message;
bytemessage.readInt(); //32
bytemessage.readInt(); //1
bytemessage.readBoolean(); //true
JMS_OBJECT_MESSAGE_TYPE
Publisher:
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(anyObjectThatIsSerializable);

nEventAttributes attributes = new nEventAttributes();
attributes.setMessageType(nEventAttributes.JMS_OBJECT_MESSAGE_TYPE);
nConsumeEvent evt = new nConsumeEvent("Message", out.toByteArray());
evt.setAttributes(attributes);
JMS Subscriber:
Message message = topicConsumer.receive(2000);
ObjectMessage objectMessage = (ObjectMessage) message;
Object object = objectMessage.getObject();
The returned object will be the deserialization of the object sent in the data payload of the nConsumeEvent.
JMS_MAP_MESSAGE_TYPE
Publisher:
No direct mapping can be made as Universal Messaging uses internal collections to construct the underlying map message. The publisher will not be able to send an nConsumeEvent with the data portion containing this as we do not expose the serialization of our internals. An option that is however available and can also be applied to other JMS Message types is the use of nEventProperties.
nEventProperties props = new nEventProperties();//populate with key/value properties
nEventAttributes attributes = new nEventAttributes();
attributes.setMessageType(nEventAttributes.JMS_MAP_MESSAGE_TYPE);
nConsumeEvent evt = new nConsumeEvent("Message", null);
evt.setAttributes(attributes);
evt.setProperties(props);
JMS Subscriber:
Message message = topicConsumer.receive(2000);
MapMessage mapMessage = (MapMessage)message;
Users can then get data using the 'key' of any of the entries in nEventProperties set on the nConsumeEvent by invoking for example:
mapMessage.getObjectProperty("key"); //if the type is unknown
mapMessage.getIntProperty("key");
and various other built-in getter methods that return different types of properties.
JMS_STREAM_MESSAGE_TYPE
Publisher:
ByteArrayOutputStream out = new ByteArrayOutputStream();

ObjectOutputStream os = new ObjectOutputStream(out);

Vector<Object> vector = new Vector<Object>();
vector.add(true);
vector.add(110110110);
vector.add(1.05f);
vector.add("abcdef");

os.writeObject(vector);

nEventAttributes attributes = new nEventAttributes();
attributes.setMessageType(nEventAttributes.JMS_STREAM_MESSAGE_TYPE);
nConsumeEvent evt = new nConsumeEvent("Message", out.toByteArray());
evt.setAttributes(attributes);
JMS Subscriber:
Message message = topicConsumer.receive(2000);
StreamMessage streamMessage = (StreamMessage)message;

streamMessage.readBoolean(); //true
streamMessage.readLong(); //110110110
streamMessage.readFloat(); //1.05f
streamMessage.readString(); //abcdef
JMS_TEXT_MESSAGE_TYPE
Publisher:
String textMessage = "This will be in the text portion of the message";

ByteArrayOutputStream baos = new ByteArrayOutputStream();

baos.write(textMessage.getBytes(StandardCharsets.UTF_8));

nEventAttributes attributes = new nEventAttributes();
attributes.setMessageType(nEventAttributes.JMS_TEXT_MESSAGE_TYPE);
nConsumeEvent evt = new nConsumeEvent("Message", baos.toByteArray());
evt.setAttributes(attributes);
JMS Subscriber:
Message message_received = topicConsumer.receive(2000);
TextMessage received = (TextMessage)message_received;
String text = received.getText());
This will return the same string sent in the data payload of the nConsumeEvent.