下面列出了javax.jms.MapMessage#getMapNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @param message JMSMap message
* @return XML representation of JMS Map message
*/
public static OMElement convertJMSMapToXML(MapMessage message) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace jmsMapNS = OMAbstractFactory.getOMFactory().createOMNamespace(JMSConstants.JMS_MAP_NS, "");
OMElement jmsMap = fac.createOMElement(JMSConstants.JMS_MAP_ELEMENT_NAME, jmsMapNS);
try {
Enumeration names = message.getMapNames();
while (names.hasMoreElements()) {
String nextName = names.nextElement().toString();
String nextVal = message.getString(nextName);
OMElement next = fac.createOMElement(nextName.replace(" ", ""), jmsMapNS);
next.setText(nextVal);
jmsMap.addChild(next);
}
} catch (JMSException e) {
log.error("Error while processing the JMS Map Message. " + e.getMessage());
}
return jmsMap;
}
public static Map<String, String> createMapMessageValues(final MapMessage mapMessage) throws JMSException {
final Map<String, String> valueMap = new HashMap<>();
final Enumeration<?> enumeration = mapMessage.getMapNames();
while (enumeration.hasMoreElements()) {
final String name = (String) enumeration.nextElement();
final Object value = mapMessage.getObject(name);
if (value == null) {
valueMap.put(MAP_MESSAGE_PREFIX + name, "");
} else {
valueMap.put(MAP_MESSAGE_PREFIX + name, value.toString());
}
}
return valueMap;
}
@Test
public void publishMapMessage() throws Exception
{
final Map<String, Object> content = new HashMap<>();
content.put("key1", "astring");
content.put("key2", Integer.MIN_VALUE);
content.put("key3", Long.MAX_VALUE);
content.put("key4", null);
MapMessage message = publishMessageWithContent(content, MapMessage.class);
final Enumeration mapNames = message.getMapNames();
int entryCount = 0;
while(mapNames.hasMoreElements())
{
String key = (String) mapNames.nextElement();
assertThat("Unexpected map content for key : " + key, message.getObject(key), is(equalTo(content.get(key))));
entryCount++;
}
assertThat("Unexpected number of key/value pairs in map message", entryCount, is(equalTo(content.size())));
}
public static Map<String, String> createMapMessageValues(final MapMessage mapMessage) throws JMSException {
final Map<String, String> valueMap = new HashMap<>();
final Enumeration<?> enumeration = mapMessage.getMapNames();
while (enumeration.hasMoreElements()) {
final String name = (String) enumeration.nextElement();
final Object value = mapMessage.getObject(name);
if (value == null) {
valueMap.put(MAP_MESSAGE_PREFIX + name, "");
} else {
valueMap.put(MAP_MESSAGE_PREFIX + name, value.toString());
}
}
return valueMap;
}
/**
* Extract a Map from the given {@link MapMessage}.
* @param message the message to convert
* @return the resulting Map
* @throws JMSException if thrown by JMS methods
*/
@SuppressWarnings("unchecked")
protected Map<String, Object> extractMapFromMessage(MapMessage message) throws JMSException {
Map<String, Object> map = new HashMap<>();
Enumeration<String> en = message.getMapNames();
while (en.hasMoreElements()) {
String key = en.nextElement();
map.put(key, message.getObject(key));
}
return map;
}
/**
* Extract a Map from the given {@link MapMessage}.
* @param message the message to convert
* @return the resulting Map
* @throws JMSException if thrown by JMS methods
*/
@SuppressWarnings("unchecked")
protected Map<String, Object> extractMapFromMessage(MapMessage message) throws JMSException {
Map<String, Object> map = new HashMap<>();
Enumeration<String> en = message.getMapNames();
while (en.hasMoreElements()) {
String key = en.nextElement();
map.put(key, message.getObject(key));
}
return map;
}
@SuppressWarnings("rawtypes")
private static byte[] getMessageBytes(MapMessage message) throws JMSException {
Map<String, String> map = new HashMap<>();
Enumeration elements = message.getMapNames();
while (elements.hasMoreElements()) {
String key = (String) elements.nextElement();
map.put(key, message.getString(key));
}
return map.toString().getBytes();
}
/**
* Extract a Map from the given {@link MapMessage}.
* @param message the message to convert
* @return the resulting Map
* @throws JMSException if thrown by JMS methods
*/
@SuppressWarnings("unchecked")
protected Map<String, Object> extractMapFromMessage(MapMessage message) throws JMSException {
Map<String, Object> map = new HashMap<String, Object>();
Enumeration<String> en = message.getMapNames();
while (en.hasMoreElements()) {
String key = en.nextElement();
map.put(key, message.getObject(key));
}
return map;
}
/**
* Extract a Map from the given {@link MapMessage}.
*
* @param message the message to convert
* @return the resulting Map
* @throws JMSException if thrown by JMS methods
*/
protected Map<String, Object> extractMapFromMessage(MapMessage message) throws JMSException
{
Map<String, Object> map = new HashMap<String, Object>();
Enumeration<?> en = message.getMapNames();
while (en.hasMoreElements()) {
String key = (String)en.nextElement();
map.put(key, message.getObject(key));
}
return map;
}
/**
* Constructor for a foreign MapMessage
*
* @param foreign
* @throws JMSException
*/
public ActiveMQMapMessage(final MapMessage foreign, final ClientSession session) throws JMSException {
super(foreign, ActiveMQMapMessage.TYPE, session);
Enumeration<?> names = foreign.getMapNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
Object obj = foreign.getObject(name);
setObject(name, obj);
}
}
public void onMessage(Message message) {
try {
if (message != null) {
if (log.isDebugEnabled()) {
log.debug("Event received in JMS Event Receiver - " + message);
}
Topic jmsDestination = (Topic) message.getJMSDestination();
if (message instanceof MapMessage) {
MapMessage mapMessage = (MapMessage) message;
Map<String, Object> map = new HashMap<String, Object>();
Enumeration enumeration = mapMessage.getMapNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
map.put(key, mapMessage.getObject(key));
}
if (APIConstants.TopicNames.TOPIC_TOKEN_REVOCATION.equalsIgnoreCase(jmsDestination.getTopicName())) {
if (map.get(APIConstants.REVOKED_TOKEN_KEY) !=
null) {
/*
* This message contains revoked token data
* revokedToken - Revoked Token which should be removed from the cache
* expiryTime - ExpiryTime of the token if token is JWT, otherwise expiry is set to 0
*/
handleRevokedTokenMessage((String) map.get(APIConstants.REVOKED_TOKEN_KEY),
(Long) map.get(APIConstants.REVOKED_TOKEN_EXPIRY_TIME));
}
}
} else {
log.warn("Event dropped due to unsupported message type " + message.getClass());
}
} else {
log.warn("Dropping the empty/null event received through jms receiver");
}
} catch (JMSException e) {
log.error("JMSException occurred when processing the received message ", e);
}
}
@SuppressWarnings("rawtypes")
private static byte[] getMessageBytes(MapMessage message) throws JMSException {
Map<String, String> map = new HashMap<>();
Enumeration elements = message.getMapNames();
while (elements.hasMoreElements()) {
String key = (String) elements.nextElement();
map.put(key, message.getString(key));
}
return map.toString().getBytes();
}
public void onMessage(Message message) {
try {
if (message != null) {
if (log.isDebugEnabled()) {
log.debug("Event received in JMS Event Receiver - " + message);
}
Topic jmsDestination = (Topic) message.getJMSDestination();
if (message instanceof MapMessage) {
MapMessage mapMessage = (MapMessage) message;
Map<String, Object> map = new HashMap<String, Object>();
Enumeration enumeration = mapMessage.getMapNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
map.put(key, mapMessage.getObject(key));
}
if (APIConstants.TopicNames.TOPIC_NOTIFICATION.equalsIgnoreCase(jmsDestination.getTopicName())) {
if (map.get(APIConstants.EVENT_TYPE) !=
null) {
/*
* This message contains notification
* eventType - type of the event
* timestamp - system time of the event published
* event - event data
*/
handleNotificationMessage((String) map.get(APIConstants.EVENT_TYPE),
(Long) map.get(APIConstants.EVENT_TIMESTAMP),
(String) map.get(APIConstants.EVENT_PAYLOAD));
}
}
} else {
log.warn("Event dropped due to unsupported message type " + message.getClass());
}
} else {
log.warn("Dropping the empty/null event received through jms receiver");
}
} catch (JMSException e) {
log.error("JMSException occurred when processing the received message ", e);
}
}