首先对象必须是可序列化的实现Serializable接口
这里HashMap为例
producer端HashMap map=new HashMap();
map.put("param1", "姓名");
map.put("param2", "年龄");
map.put("param3", "性别");
ObjectMessage message=session.createObjectMessage(map);
System.out.println(map);
messageProducer.send(message);
consumer端while(true){
ObjectMessage objectMessage=(ObjectMessage)messageConsumer.receive(1000);
System.out.println(objectMessage);
if(objectMessage!=null){
System.out.println("收到的消息:"+objectMessage.getObject());
}else{
continue;
}
}
参考文章: