package com.andyao.activemq;
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 {
System.out.println("转化器:toMessage");
// 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;
}
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();
System.out.println("转化器:fromMessage");
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");
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jms activeMQ 经典代码
共82个文件
class:30个
jar:20个
java:13个
需积分: 3 18 下载量 157 浏览量
2011-03-18
11:44:32
上传
评论
收藏 5.91MB RAR 举报
温馨提示
写spring配置文件的时候, 要把MessageProducer, MessageConsumer,MessageListener,MessageListenerContainer几个地方弄清楚: 1.可以有一个或者多个消息生产者向同一个destination发送消息. 2.queue类型的只能有一个消息消费者. 3.topic类型的可以有多个消息消费者. 4.每个消费者对应一个MessageListener和一个MessageListenerContainer.
资源推荐
资源详情
资源评论
收起资源包目录
jmstest.rar (82个子文件)
jmstest
.project 591B
out
test
jmstest
production
jmstest
log4j.properties 2KB
com
andyao
listener
TestListener.class 790B
TestEvent.class 994B
applicationContext.xml 545B
TestMain.class 883B
TestBean.class 1KB
activemq
QueueConsumer.class 927B
TopicConsumerA.class 1KB
TopicConsumerA$1.class 1KB
TopicConsumerB.class 932B
CoverterHolder.class 3KB
TestMain.class 2KB
FooMessage.class 516B
QueueMessageProducer.class 998B
TopicMessageProducer.class 998B
DefaultMessageConverter.class 3KB
applicationContext-activemq.xml 5KB
bin
log4j.properties 2KB
com
andyao
listener
TestListener.class 790B
TestEvent.class 934B
applicationContext.xml 545B
TestMain.class 885B
TestBean.class 1KB
activemq
QueueConsumer.class 880B
TopicConsumerA.class 1KB
TopicConsumerA$1.class 1KB
TopicConsumerB.class 896B
TestMain.class 2KB
FooMessage.class 516B
QueueMessageProducer.class 998B
TopicMessageProducer.class 998B
DefaultMessageConverter.class 4KB
applicationContext-activemq.xml 5KB
ThreadTest$Thd1.class 890B
ThreadTest$Thd.class 887B
ThreadTest.class 1KB
.settings
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 629B
spring-form.tld 69KB
activemq-data
localhost
tmp_storage
src
log4j.properties 2KB
com
andyao
listener
TestListener.java 429B
TestBean.java 1KB
TestEvent.java 427B
applicationContext.xml 545B
TestMain.java 410B
activemq
QueueMessageProducer.java 587B
FooMessage.java 234B
DefaultMessageConverter.java 2KB
TopicConsumerA.java 764B
TopicConsumerB.java 231B
TopicMessageProducer.java 501B
QueueConsumer.java 275B
TestMain.java 848B
applicationContext-activemq.xml 5KB
ThreadTest.java 610B
spring.tld 17KB
.springBeans 415B
jmstest.ipr 20KB
jmstest.iws 30KB
.classpath 9KB
jmstest.iml 26KB
lib
activemq-protobuf-1.1.jar 144KB
commons-logging-1.1.jar 52KB
activemq-console-5.4.0.jar 109KB
camel-spring-2.4.0.jar 180KB
activemq-camel-5.4.0.jar 43KB
stax-api-1.0.1.jar 26KB
camel-jms-2.4.0.jar 119KB
geronimo-j2ee-management_1.1_spec-1.0.1.jar 20KB
geronimo-jta_1.0.1B_spec-1.0.1.jar 14KB
activemq-web-5.4.0.jar 94KB
activation-1.1.jar 62KB
kahadb-5.4.0.jar 151KB
activemq-jaas-5.4.0.jar 27KB
camel-core-2.4.0.jar 1.45MB
jaxb-impl-2.1.6.jar 830KB
camel-jetty-2.4.0.jar 38KB
jaxb-api-2.1.jar 88KB
activemq-core-5.4.0.jar 3.14MB
stax-1.2.0.jar 175KB
geronimo-jms_1.1_spec-1.1.1.jar 32KB
共 82 条
- 1
资源评论
gy_gengyuan
- 粉丝: 2
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功