activemq 转换器
程序员文章站
2022-05-19 15:30:32
...
导入spring包
package avtivemq.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.support.converter.MessageConverter;
public class DefaultMessageConverter implements MessageConverter {
/**
* Logger for this class
*/
private static final Log log = LogFactory.getLog(DefaultMessageConverter.class);
public Message toMessage(Object obj, Session session) throws JMSException {
if (log.isDebugEnabled()) {
log.debug("toMessage(Object, Session) - start");
}
// check Type
ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session.createObjectMessage();
HashMap<String, byte[]> map = new HashMap<String, byte[]>();
try {
// POJO must implements Seralizable
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
map.put("POJO", bos.toByteArray());
objMsg.setObjectProperty("Map", map);
} catch (IOException e) {
log.error("toMessage(Object, Session)", e);
}
return objMsg;
}
@SuppressWarnings("unchecked")
public Object fromMessage(Message msg) throws JMSException {
if (log.isDebugEnabled()) {
log.debug("fromMessage(Message) - start");
}
if (msg instanceof ObjectMessage) {
HashMap<String, byte[]> map = (HashMap<String, byte[]>) ((ObjectMessage) msg).getObjectProperty("Map");
try {
// POJO must implements Seralizable
ByteArrayInputStream bis = new ByteArrayInputStream(map.get("POJO"));
ObjectInputStream ois = new ObjectInputStream(bis);
Object returnObject = ois.readObject();
return returnObject;
} catch (IOException e) {
log.error("fromMessage(Message)", e);
} catch (ClassNotFoundException e) {
log.error("fromMessage(Message)", e);
}
return null;
} else {
throw new JMSException("Msg:[" + msg + "] is not Map");
}
}
}
推荐阅读
-
如何去掉电影片头片尾?狸窝全能视频转换器将电影片头片尾去除的方法介绍
-
狸窝全能视频转换器如何转换WAV格式?转换WAV格式的具体操作流程
-
视频格式转换器怎么转换苹果视频格式
-
小米有品开卖手游键鼠转换器:自带压枪 苹果安卓通用
-
浅谈Java消息队列总结篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka)
-
迅捷在线PDF转换器使用教程全程图解
-
Spring MVC自定义日期类型转换器实例详解
-
安装歌词转换器遇到DllRegisterServer的调用失败怎么办?
-
GIF转SWF转换器ThunderSoft GIF to SWF Converter激活教程(附补丁)
-
Axure怎么制作英文字母大小写转换器?