import org.springframework.jms.core.JmsTemplate;
/**
* the class use to receive the MQ message
*/
public class JmsLogConsumer
{
private JmsTemplate jmsTemplate;
/**
* @return Returns the jmsTemplate.
*/
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
/**
* @param jmsTemplate The jmsTemplate to set.
*/
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public Object receiveMessage() {
return this.jmsTemplate.receiveAndConvert();
}
}
消费者receiveMessage调用converter,converter在spring中配置,返回对象
import java.util.Calendar;
import java.util.Enumeration;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.JMSException;
import java.util.Date;
import javax.jms.Session;
import org.springframework.jms.support.converter.SimpleMessageConverter;
public class ILogMessageConverter extends SimpleMessageConverter {
public Object fromMessage(Message msg) {
try {
MapMessage message = (MapMessage) msg;
Enumeration enumer = message.getMapNames();
String wholename = message.getString("class");
Class clz = Class.forName(wholename.substring(wholename
.lastIndexOf(" ") + 1));
Object obj = clz.newInstance();
while (enumer.hasMoreElements()) {
String property = enumer.nextElement().toString();
//System.out.print("current is"+currentObj.toString());
if ("class".equals(property)||"id".equals(property)) {
continue;
}
Class field = clz.getDeclaredField(property).getType();
Object value = message.getObject(property);
initialProperty(clz, obj, field, property, message);
}
return obj;
} catch (Exception e) {
return null;
}
}
private void initialProperty(Class clz, Object obj, Class field,
String property, MapMessage message) throws Exception {
String methodName = this.getSetMethod(property);
if (field.isPrimitive()) {
if (Integer.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Integer.TYPE }).invoke(
obj,
new Object[] { new Integer(message.getInt(property)) });
return;
}
if (Long.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Long.TYPE }).invoke(
obj,
new Object[] { new Long(message.getLong(property)) });
return;
}
if (Boolean.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Boolean.TYPE })
.invoke(
obj,
new Object[] { new Boolean(message
.getBoolean(property)) });
return;
}
if (Double.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Double.TYPE })
.invoke(
obj,
new Object[] { new Double(message
.getDouble(property)) });
return;
}
if (Float.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Float.TYPE }).invoke(
obj,
new Object[] { new Float(message.getFloat(property)) });
return;
}
if (Character.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Character.TYPE })
.invoke(
obj,
new Object[] { new Character(message
.getChar(property)) });
return;
}
if (Short.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Short.TYPE }).invoke(
obj,
new Object[] { new Short(message.getShort(property)) });
return;
}
if (Byte.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Byte.TYPE }).invoke(
obj,
new Object[] { new Byte(message.getByte(property)) });
return;
}
} else {
if (Long.class.equals(field)) {
clz.getMethod(methodName, new Class[] { Long.class }).invoke(
obj,
new Object[] {new Long(message.getLong(property)) });
return;
}
if(String.class.equals(field))
{
clz.getMethod(methodName, new Class[] { String.class }).invoke(
obj,
new Object[] {message.getString(property) });
return;
}
if (Date.class.equals(field)) {
Date date = new Date(message.getString(property));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY,-14);
clz.getMethod(methodName, new Class[] { Date.class }).invoke(
obj,
new Object[] {cal.getTime()});
return;
}
if (Integer.class.equals(field)) {
clz.getMethod(methodName, new Class[] { Integer.class }).invoke(
obj,
new Object[] { new Integer(message.getInt(property)) });
return;
}
if (Boolean.class.equals(field)) {
clz.getMethod(methodName, new Class[] { Boolean.class })
.invoke(
obj,
new Object[] { new Boolean(message
.getBoolean(property)) });
return;
}
if (Double.class.equals(field)) {
clz.getMethod(methodName, new Class[] { Double.class })
.invoke(
obj,
new Object[] { new Double(message
.getDouble(property)) });
return;
}
if (Float.TYPE.equals(field)) {
clz.getMethod(methodName, new Class[] { Float.TYPE }).invoke(
obj,
new Object[] { new Float(message.getFloat(property)) });
return;
}
if (Character.class.equals(field)) {
clz.getMethod(methodName, new Class[] { Character.class })
.invoke(
obj,
new Object[] { new Character(message
.getChar(property)) });
return;
}
if (Short.class.equals(field)) {
clz.getMethod(methodName, new Class[] { Short.class }).invoke(
obj,
new Object[] { new Short(message.getShort(property)) });
return;
}
if (Byte.class.equals(field)) {
clz.getMethod(methodName, new Class[] { Byte.class }).invoke(
obj,
new Object[] { new Byte(message.getByte(property)) });
return;
}
}
}
/**
* get the set methodname
* @param property
* @return
*/
private String getSetMethod(String property) {
return "set" + property.substring(0, 1).toUpperCase()
+ property.substring(1);
}
public Message toMessage(Object obj, Session session) throws JMSException
{
MapMessage message = session.createMapMessage();
return message;
}
}
converter具体过程.
class="org.springframework.jms.core.JmsTemplate102">
${RECEIVER_TIME_OUT}
class="ILogMessageConverter">
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8503278/viewspace-889978/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8503278/viewspace-889978/