Just For The Record, here are the two methods I just wrote to transform an
Object into a HashMap, which I then poked into an ObjectMessage which
actually works. The JMS MapMessage was not used as it only supports
primitives and Strings and I have a couple of DATE objects which I want to
send.
There are two privisos, 1) the fields have to generally be primitives and/or
simple objects like Dates etc (not really but of course in my case JRun JMS
will cark it if they are not) and 2) On decode, you have to know what sort
of Object is represented in the map, because you have to instantiate it
first for example before passing to the decode method (this could be
extended quite simply to work around that requirement, it was uneccessary
however for my personal requirement).
Thanks to everyone who tried to help. I will probably go for SonicMQ message
queue once the first part of this project is completed and then re-engineer
all of the below.
Here's the transmission method;
/**
* sends the FixMessage onto the queue for processing at the backend.
*/
public void transmitFix(FIXMessage fix) throws JMSException {
ObjectMessage om = queueSession.createObjectMessage();
HashMap map = FIXUtils.introspectMapFromObject(fix);
om.setObject(map);
sender.send(om);
System.out.println("Transmitted: " + fix);
}
Here's the code fragment from the onMessage() method of the MDB that listens
to the queue;
if (msg instanceof ObjectMessage) {
try {
ObjectMessage om = (ObjectMessage) msg;
Object o = om.getObject();
if (o instanceof Map) {
FIXMessage fix = new FIXMessage();
Map map = (Map) o;
fix = (FIXMessage)
FIXUtils.introspectObjectFromMap(map, fix);
System.out.println(fix);
getFixProcessor().doProcess(fix);
}
} catch (JMSException e) {
System.out.println("ObjectMessage Failure " +
e.toString());
}
}
Here's the methods that can map any object to a HashMap and back, Error
handling is not set correctly in either of these methods which I leave as an
exercise to the reader (and for me, later);
/**
* Gets any object, returns a Map version of the object's public fields
*/
public static HashMap introspectMapFromObject(Object obj) {
HashMap map = new HashMap();
Field[] f = obj.getClass().getFields();
try {
for (int i = 0; i < f.length; i++) {
try {
String name = f[i].getName();
Object value = f[i].get(obj);
if (value != null) {
map.put(name, value);
}
} catch (NullPointerException npe) {
}
}
} catch (IllegalAccessException e) {
}
return map;
}
/**
* Gets a Map, and an already instantiated version of your object,
* puts any non-null values into your object.
*/
public static Object introspectObjectFromMap(Map map, Object obj) {
Field[] f = obj.getClass().getFields();
try {
for (int i = 0; i < f.length; i++) {
String fname = f[i].getName();
if (map.containsKey(fname)) {
Object o = map.get(fname);
if (o != null) {
f[i].set(obj, o);
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________