下面列出了javax.jms.Message#getObjectProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void verifyMessageProperties(final MessageDescription messageDescription,
final Message message) throws VerificationException
{
try
{
final ArrayList<String> actualPropertyNames =
Collections.list(((Enumeration<String>) message.getPropertyNames()));
final HashMap<String, Serializable> properties = messageDescription.getProperties();
for (Map.Entry<String, Serializable> entry : properties.entrySet())
{
final String key = entry.getKey();
verifyEquals(String.format("expected property '%s' not set", key),
true,
actualPropertyNames.contains(key));
final Object actualValue = message.getObjectProperty(key);
verifyEquals(String.format("Unexpected message property '%s'", key),
entry.getValue(),
actualValue);
}
}
catch (JMSException e)
{
throw new RuntimeException("Unexpected exception during message property verification", e);
}
}
public void onClientMessage(Message message) {
Message serverMessage = waitingList.poll();
try {
if (serverMessage == null)
exit("" + this + " failed: There is no next server message, but received: " + message);
Integer receivedId = (Integer) message.getObjectProperty("ID");
Integer serverId = (Integer) serverMessage.getObjectProperty("ID");
if (receivedId == null || serverId == null)
exit("" + this + " failed: message ID not found.\r\n" +
" received: " + message + "\r\n" +
" server: " + serverMessage);
if (!serverId.equals(receivedId))
exit("" + this + " failed: Received wrong message.\r\n" +
" received: " + message + "\r\n" +
" server: " + serverMessage);
checkDeliveryTime(message);
} catch (Throwable e) {
exit("" + this + ".onClientMessage failed.\r\n" +
" received: " + message + "\r\n" +
" server: " + serverMessage, e);
}
}
@Override
public void onMessage(Message message) {
count++;
try {
Object b = message.getObjectProperty("$b");
if (b != null) {
boolean c = message.getBooleanProperty("$c");
assertTrue("", c);
} else {
String d = message.getStringProperty("$d");
assertTrue("", "D1".equals(d) || "D2".equals(d));
}
} catch (JMSException e) {
e.printStackTrace();
exceptions.add(e);
}
}
/**
* Copies the standard JMS and user defined properties from the given source
* message to the specified target message. The copy can only handle the JMS
* specific message properties and known JMS Headers, any headers that are
* specific to the foreign message may be lost if not returned directly via
* the <code>propertyNames</code> method.
*
* @param connection
* The Connection instance that is requesting the transformation.
* @param source
* the message to take the properties from
* @param target
* the message to add the properties to
*
* @throws JMSException if an error occurs during the copy of message properties.
*/
public static void copyProperties(JmsConnection connection, Message source, JmsMessage target) throws JMSException {
target.setJMSMessageID(source.getJMSMessageID());
target.setJMSCorrelationID(source.getJMSCorrelationID());
target.setJMSReplyTo(transformDestination(connection, source.getJMSReplyTo()));
target.setJMSDestination(transformDestination(connection, source.getJMSDestination()));
target.setJMSDeliveryMode(source.getJMSDeliveryMode());
target.setJMSDeliveryTime(getForeignMessageDeliveryTime(source));
target.setJMSRedelivered(source.getJMSRedelivered());
target.setJMSType(source.getJMSType());
target.setJMSExpiration(source.getJMSExpiration());
target.setJMSPriority(source.getJMSPriority());
target.setJMSTimestamp(source.getJMSTimestamp());
Enumeration<?> propertyNames = source.getPropertyNames();
while (propertyNames.hasMoreElements()) {
String name = propertyNames.nextElement().toString();
Object obj = source.getObjectProperty(name);
target.setObjectProperty(name, obj);
}
}
public void onClientMessage(Message message) {
Message serverMessage = waitingList.poll();
try {
Integer receivedId = (Integer) message.getObjectProperty("ID");
if (processed != null && processed.contains(receivedId))
LOG.info("! Message has been processed before. " + this + " message = " + message);
if (serverMessage == null)
exit("" + this + " failed: There is no next server message, but received: " + message);
Integer serverId = (Integer) serverMessage.getObjectProperty("ID");
if (receivedId == null || serverId == null)
exit("" + this + " failed: message ID not found.\r\n" + " received: " + message + "\r\n" + " server: " + serverMessage);
if (!serverId.equals(receivedId)) {
String detail = processed != null ? Arrays.toString(processed.toArray()) + "\n" : "";
exit(detail + this + " failed: Received wrong message.\r\n" + " received: " + message + "\r\n" + " server: " + serverMessage);
}
checkDeliveryTime(message);
if (processed != null)
processed.add(receivedId);
} catch (Throwable e) {
exit("" + this + ".onClientMessage failed.\r\n" + " received: " + message + "\r\n" + " server: " + serverMessage, e);
}
}
protected void assertMessagesReceivedAreValid2(List<Message> receivedMessages) throws JMSException {
super.assertMessagesReceivedAreValid(receivedMessages);
// lets assert that the user ID is set
for (Message message : receivedMessages) {
String userID = (String) message.getObjectProperty("JMSXUserID");
LOG.info("Received message with userID: " + userID);
assertEquals("JMSXUserID header", userName, userID);
}
}
protected void outputProperties(Message msg) throws JMSException {
output("Message Properties");
@SuppressWarnings("unchecked")
Enumeration<String> en = msg.getPropertyNames();
while (en.hasMoreElements()) {
String name = en.nextElement();
try {
Object property = msg.getObjectProperty(name);
output(" ", name, ": ", null != property ? property.toString() : "[null]");
} catch ( Exception e) {
output(" ", name, ": Error loading property (" + e.getMessage() + ")");
}
}
}
/**
* Same as {@link Message#getStringProperty(String)}, just doesn't throw or coerce non-strings to
* strings.
*/
@Nullable static String getPropertyIfString(Message message, String name) {
try {
Object o = message.getObjectProperty(name);
if (o instanceof String) return o.toString();
return null;
} catch (Throwable t) {
propagateIfFatal(t);
log(t, "error getting property {0} from message {1}", name, message);
return null;
}
}
/**
* Extract transport level headers from JMS message into a Map
*
* @param message JMS message
* @param msgContext axis2 message context
* @return a Map of the transport headers
*/
public static Map<String, Object> getTransportHeaders(Message message, MessageContext msgContext) {
// create a Map to hold transport headers
Map<String, Object> map = new HashMap<>();
try {
Enumeration<?> propertyNamesEnm = message.getPropertyNames();
while (propertyNamesEnm.hasMoreElements()) {
String headerName = (String) propertyNamesEnm.nextElement();
Object headerValue = message.getObjectProperty(headerName);
if (headerValue instanceof String) {
if (isHyphenReplaceMode(msgContext)) {
map.put(inverseTransformHyphenatedString(headerName), message.getStringProperty(headerName));
} else {
map.put(headerName, message.getStringProperty(headerName));
}
} else if (headerValue instanceof Integer) {
map.put(headerName, message.getIntProperty(headerName));
} else if (headerValue instanceof Boolean) {
map.put(headerName, message.getBooleanProperty(headerName));
} else if (headerValue instanceof Long) {
map.put(headerName, message.getLongProperty(headerName));
} else if (headerValue instanceof Double) {
map.put(headerName, message.getDoubleProperty(headerName));
} else if (headerValue instanceof Float) {
map.put(headerName, message.getFloatProperty(headerName));
} else {
map.put(headerName, headerValue);
}
}
} catch (JMSException e) {
log.error("Error while reading the Transport Headers from JMS Message", e);
}
// remove "INTERNAL_TRANSACTION_COUNTED" header from the transport level headers map.
// this property will be maintained in the message context. Therefore, no need to set this in the transport
// headers.
map.remove(BaseConstants.INTERNAL_TRANSACTION_COUNTED);
return map;
}
public static Map<String, String> createAttributeMap(final Message message) throws JMSException {
final Map<String, String> attributes = new HashMap<>();
final Enumeration<?> enumeration = message.getPropertyNames();
while (enumeration.hasMoreElements()) {
final String propName = (String) enumeration.nextElement();
final Object value = message.getObjectProperty(propName);
if (value == null) {
attributes.put(ATTRIBUTE_PREFIX + propName, "");
attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, "Unknown");
continue;
}
final String valueString = value.toString();
attributes.put(ATTRIBUTE_PREFIX + propName, valueString);
final String propType;
if (value instanceof String) {
propType = PROP_TYPE_STRING;
} else if (value instanceof Double) {
propType = PROP_TYPE_DOUBLE;
} else if (value instanceof Float) {
propType = PROP_TYPE_FLOAT;
} else if (value instanceof Long) {
propType = PROP_TYPE_LONG;
} else if (value instanceof Integer) {
propType = PROP_TYPE_INTEGER;
} else if (value instanceof Short) {
propType = PROP_TYPE_SHORT;
} else if (value instanceof Byte) {
propType = PROP_TYPE_BYTE;
} else if (value instanceof Boolean) {
propType = PROP_TYPE_BOOLEAN;
} else {
propType = PROP_TYPE_OBJECT;
}
attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, propType);
}
if (message.getJMSCorrelationID() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_CORRELATION_ID, message.getJMSCorrelationID());
}
if (message.getJMSDestination() != null) {
String destinationName;
if (message.getJMSDestination() instanceof Queue) {
destinationName = ((Queue) message.getJMSDestination()).getQueueName();
} else {
destinationName = ((Topic) message.getJMSDestination()).getTopicName();
}
attributes.put(ATTRIBUTE_PREFIX + JMS_DESTINATION, destinationName);
}
if (message.getJMSMessageID() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_MESSAGE_ID, message.getJMSMessageID());
}
if (message.getJMSReplyTo() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_REPLY_TO, message.getJMSReplyTo().toString());
}
if (message.getJMSType() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_TYPE, message.getJMSType());
}
attributes.put(ATTRIBUTE_PREFIX + JMS_DELIVERY_MODE, String.valueOf(message.getJMSDeliveryMode()));
attributes.put(ATTRIBUTE_PREFIX + JMS_EXPIRATION, String.valueOf(message.getJMSExpiration()));
attributes.put(ATTRIBUTE_PREFIX + JMS_PRIORITY, String.valueOf(message.getJMSPriority()));
attributes.put(ATTRIBUTE_PREFIX + JMS_REDELIVERED, String.valueOf(message.getJMSRedelivered()));
attributes.put(ATTRIBUTE_PREFIX + JMS_TIMESTAMP, String.valueOf(message.getJMSTimestamp()));
return attributes;
}
public void onClientMessage(Message message) {
Message serverMessage = waitingList.poll();
try {
Integer receivedId = (Integer) message.getObjectProperty("ID");
if (processed != null && processed.contains(receivedId))
LOG.info("! Message has been processed before. " + this + " redeliveredFlag=" + message.getJMSRedelivered() + ", message = " + message);
if (serverMessage == null)
exit("" + this + " failed: There is no next server message, but received: " + message);
Integer serverId = (Integer) serverMessage.getObjectProperty("ID");
if (receivedId == null || serverId == null)
exit("" + this + " failed: message ID not found.\r\n" + " received: " + message + "\r\n" + " server: " + serverMessage);
if (!serverId.equals(receivedId)) {
StringBuilder missingList = new StringBuilder();
Object lastTrans = null;
int transCount = 0;
Message nextServerMessage = serverMessage;
do {
Integer nextServerId = (Integer) nextServerMessage.getObjectProperty("ID");
if (nextServerId.equals(receivedId)) {
if (lastTrans != null)
missingList.append("Missing TRANS=").append(lastTrans).append(", size=").append(transCount).append("\r\n");
break;
}
Object trans = nextServerMessage.getObjectProperty("TRANS");
if (!trans.equals(lastTrans)) {
if (lastTrans != null)
missingList.append("Missing TRANS=").append(lastTrans).append(", size=").append(transCount).append("\r\n");
lastTrans = trans;
transCount = 1;
} else
transCount++;
} while ((nextServerMessage = waitingList.poll()) != null);
exit("Missing messages!\r\n" + missingList +
"Received message: " + message + "\r\n" +
"Expected message: " + serverMessage);
}
checkDeliveryTime(message);
if (processed != null)
processed.add(receivedId);
} catch (Throwable e) {
exit("" + this + ".onClientMessage failed.\r\n" + " received: " + message + "\r\n" + " server: " + serverMessage, e);
}
}
public MessageDump toDumpMessage(Message msg) throws JMSException{
MessageDump dump = new MessageDump();
dump.JMSCorrelationID = msg.getJMSCorrelationID();
dump.JMSMessageID = msg.getJMSMessageID();
dump.JMSType = msg.getJMSType();
dump.JMSDeliveryMode = msg.getJMSDeliveryMode();
dump.JMSExpiration = msg.getJMSExpiration();
dump.JMSRedelivered = msg.getJMSRedelivered();
dump.JMSTimestamp = msg.getJMSTimestamp();
dump.JMSPriority = msg.getJMSPriority();
@SuppressWarnings("rawtypes")
Enumeration propertyNames = msg.getPropertyNames();
while(propertyNames.hasMoreElements()){
String property = (String) propertyNames.nextElement();
Object propertyValue = msg.getObjectProperty(property);
if( propertyValue instanceof String){
dump.stringProperties.put(property, (String)propertyValue);
} else if ( propertyValue instanceof Integer ){
dump.intProperties.put(property, (Integer)propertyValue);
} else if ( propertyValue instanceof Long) {
dump.longProperties.put(property, (Long)propertyValue);
} else if( propertyValue instanceof Double) {
dump.doubleProperties.put(property, (Double) propertyValue);
} else if (propertyValue instanceof Short) {
dump.shortProperties.put(property, (Short)propertyValue);
} else if (propertyValue instanceof Float) {
dump.floatProperties.put(property, (Float) propertyValue);
} else if (propertyValue instanceof Byte) {
dump.byteProperties.put(property, (Byte)propertyValue);
} else if (propertyValue instanceof Boolean) {
dump.boolProperties.put(property, (Boolean)propertyValue);
} else if (propertyValue instanceof Serializable){
// Object property.. if it's on Classpath and Serializable
byte[] propBytes = SerializationUtils.serialize((Serializable) propertyValue);
dump.objectProperties.put(property, Base64.encodeBase64String(propBytes));
} else {
// Corner case.
throw new IllegalArgumentException("Property of key '"+ property +"' is not serializable. Type is: " + propertyValue.getClass().getCanonicalName());
}
}
dump.body = "";
dump.type = "";
if (msg instanceof TextMessage) {
dump.body = ((TextMessage)msg).getText();
dump.type = "TextMessage";
} else if (msg instanceof BytesMessage) {
BytesMessage bm = (BytesMessage)msg;
byte[] bytes = new byte[(int) bm.getBodyLength()];
bm.readBytes(bytes);
dump.body = Base64.encodeBase64String(bytes);
dump.type = "BytesMessage";
} else if (msg instanceof ObjectMessage) {
ObjectMessage om = (ObjectMessage)msg;
byte[] objectBytes = SerializationUtils.serialize(om.getObject());
dump.body = Base64.encodeBase64String(objectBytes);
dump.type = "ObjectMessage";
}
return dump;
}
public static Map<String, String> createAttributeMap(final Message message) throws JMSException {
final Map<String, String> attributes = new HashMap<>();
final Enumeration<?> enumeration = message.getPropertyNames();
while (enumeration.hasMoreElements()) {
final String propName = (String) enumeration.nextElement();
final Object value = message.getObjectProperty(propName);
if (value == null) {
attributes.put(ATTRIBUTE_PREFIX + propName, "");
attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, "Unknown");
continue;
}
final String valueString = value.toString();
attributes.put(ATTRIBUTE_PREFIX + propName, valueString);
final String propType;
if (value instanceof String) {
propType = PROP_TYPE_STRING;
} else if (value instanceof Double) {
propType = PROP_TYPE_DOUBLE;
} else if (value instanceof Float) {
propType = PROP_TYPE_FLOAT;
} else if (value instanceof Long) {
propType = PROP_TYPE_LONG;
} else if (value instanceof Integer) {
propType = PROP_TYPE_INTEGER;
} else if (value instanceof Short) {
propType = PROP_TYPE_SHORT;
} else if (value instanceof Byte) {
propType = PROP_TYPE_BYTE;
} else if (value instanceof Boolean) {
propType = PROP_TYPE_BOOLEAN;
} else {
propType = PROP_TYPE_OBJECT;
}
attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, propType);
}
if (message.getJMSCorrelationID() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_CORRELATION_ID, message.getJMSCorrelationID());
}
if (message.getJMSDestination() != null) {
String destinationName;
if (message.getJMSDestination() instanceof Queue) {
destinationName = ((Queue) message.getJMSDestination()).getQueueName();
} else {
destinationName = ((Topic) message.getJMSDestination()).getTopicName();
}
attributes.put(ATTRIBUTE_PREFIX + JMS_DESTINATION, destinationName);
}
if (message.getJMSMessageID() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_MESSAGE_ID, message.getJMSMessageID());
}
if (message.getJMSReplyTo() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_REPLY_TO, message.getJMSReplyTo().toString());
}
if (message.getJMSType() != null) {
attributes.put(ATTRIBUTE_PREFIX + JMS_TYPE, message.getJMSType());
}
attributes.put(ATTRIBUTE_PREFIX + JMS_DELIVERY_MODE, String.valueOf(message.getJMSDeliveryMode()));
attributes.put(ATTRIBUTE_PREFIX + JMS_EXPIRATION, String.valueOf(message.getJMSExpiration()));
attributes.put(ATTRIBUTE_PREFIX + JMS_PRIORITY, String.valueOf(message.getJMSPriority()));
attributes.put(ATTRIBUTE_PREFIX + JMS_REDELIVERED, String.valueOf(message.getJMSRedelivered()));
attributes.put(ATTRIBUTE_PREFIX + JMS_TIMESTAMP, String.valueOf(message.getJMSTimestamp()));
return attributes;
}