下面列出了javax.jms.MapMessage#getObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
private void receiveMapMessage(boolean useCore) throws Exception {
MapMessage mapMessage = (MapMessage) receiveMessage(useCore);
boolean booleanVal = mapMessage.getBoolean("boolean-type");
assertTrue(booleanVal);
byte byteVal = mapMessage.getByte("byte-type");
assertEquals((byte) 10, byteVal);
byte[] bytesVal = mapMessage.getBytes("bytes-type");
byte[] originVal = TEXT.getBytes();
assertEquals(originVal.length, bytesVal.length);
for (int i = 0; i < bytesVal.length; i++) {
assertTrue(bytesVal[i] == originVal[i]);
}
char charVal = mapMessage.getChar("char-type");
assertEquals('A', charVal);
double doubleVal = mapMessage.getDouble("double-type");
assertEquals(55.3D, doubleVal, 0.1D);
float floatVal = mapMessage.getFloat("float-type");
assertEquals(79.1F, floatVal, 0.1F);
int intVal = mapMessage.getInt("int-type");
assertEquals(37, intVal);
long longVal = mapMessage.getLong("long-type");
assertEquals(56652L, longVal);
Object objectVal = mapMessage.getObject("object-type");
Object origVal = new String("VVVV");
assertTrue(objectVal.equals(origVal));
short shortVal = mapMessage.getShort("short-type");
assertEquals((short) 333, shortVal);
String strVal = mapMessage.getString("string-type");
assertEquals(TEXT, strVal);
}
/**
* 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);
}
}
@Test
public void test() throws JMSException {
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
JmsAppender appender = new JmsAppender();
appender.connectionFactory = cf;
Dictionary<String, Object> config = new Hashtable<>();
config.put("message.type", "map");
appender.activate(config);
Connection con = cf.createConnection();
con.start();
Session sess = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = sess.createConsumer(sess.createQueue("decanter"));
Map<String, Object> props = new HashMap<String, Object>();
props.put("timestamp", 1l);
props.put("string", "test");
props.put("boolean", true);
props.put("integer", 1);
props.put("testnull", null);
props.put("map", new HashMap<String, String>());
appender.handleEvent(new Event("decanter/collect", props));
MapMessage message = (MapMessage)consumer.receive(1000);
consumer.close();
sess.close();
con.close();
Assert.assertEquals(1l, message.getObject("timestamp"));
Assert.assertEquals("test", message.getObject("string"));
Assert.assertEquals(true, message.getObject("boolean"));
Assert.assertEquals(1, message.getObject("integer"));
Object map = message.getObject("map");
Assert.assertTrue(map instanceof Map);
}
@Test
public void testWithFilter() throws JMSException {
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
JmsAppender appender = new JmsAppender();
appender.connectionFactory = cf;
Dictionary<String, Object> config = new Hashtable<>();
config.put("message.type", "map");
config.put(EventFilter.PROPERTY_NAME_EXCLUDE_CONFIG, ".*refused.*");
config.put(EventFilter.PROPERTY_VALUE_EXCLUDE_CONFIG, ".*refused.*");
appender.activate(config);
Connection con = cf.createConnection();
con.start();
Session sess = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = sess.createConsumer(sess.createQueue("decanter"));
Map<String, Object> data = new HashMap<String, Object>();
data.put("timestamp", 1l);
data.put("string", "test");
data.put("boolean", true);
data.put("integer", 1);
data.put("testnull", null);
data.put("map", new HashMap<String, String>());
appender.handleEvent(new Event("decanter/collect", data));
data = new HashMap<>();
data.put("refused_property", "value");
appender.handleEvent(new Event("decanter/collect", data));
data = new HashMap<>();
data.put("property", "refused_value");
appender.handleEvent(new Event("decanter/collect", data));
MapMessage message = (MapMessage)consumer.receive(1000);
consumer.close();
sess.close();
con.close();
Assert.assertEquals(1l, message.getObject("timestamp"));
Assert.assertEquals("test", message.getObject("string"));
Assert.assertEquals(true, message.getObject("boolean"));
Assert.assertEquals(1, message.getObject("integer"));
Object map = message.getObject("map");
Assert.assertTrue(map instanceof Map);
}