发现运行时会报错误, Object is not a primitive:

ActiveMQHSQLDBJMSMySQLSQL Server

在开发过程中出现了"发现运行时会报错误, Object is not a primitive: "的异常,开始以为该实体对象没有实现序列化,打开实体看后,发现已经实现了,后来发现是在MQ的配置中,将实体写入到数据库才会出现这个异常,如果不写入到数据库,则不会;配置如下:

<amq:broker useJmx="false" persistent="true">
<amq:persistenceAdapter>
<amq:jdbcPersistenceAdapter id="jdbcAdapter"
dataSource="#dataSource" createTablesOnStartup="true"
useDatabaseLock="false" />
<!-- Mysql can setup useDatabaseLock="true",this is defualt HSQLDB,MSSQL
plz setup useDatabaseLock="false", if u setup useDatabaseLock="true",u will
catch error: MSSQL Error Info:FOR UPDATE clause allowed only for DECLARE
CURSOR HSQLDB Error Info:FOR in statement [SELECT * FROM ACTIVEMQ_LOCK FOR
UPDATE] see http://www.nabble.com/ActiveMQ-JDBC-Persistence-with-SQL-Server-tf2022248.html#a5560296 -->
</amq:persistenceAdapter>
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:0" />
</amq:transportConnectors>
</amq:broker>

解决方法为:改写MessageConverter类中的实现方法,通过ObjectOutputStream流来进行处理!方法如下:

public Message toMessage(Object obj, Session session) throws JMSException {
// check Type
if (obj instanceof IPAddressInfo) {
ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session
.createObjectMessage();
Map<String, byte[]> map = new HashMap<String, byte[]>();
byte[] bytes = null;
ByteArrayOutputStream buf = null;
ObjectOutputStream o = null;
try {
buf = new ByteArrayOutputStream();
o = new ObjectOutputStream(buf);
o.writeObject(obj);
bytes = buf.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (o != null) {
try {
o.flush();
o.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (buf != null) {
try {
buf.flush();
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
map.put("Order", bytes);
objMsg.setObjectProperty("Map", map);
return objMsg;
} else {
throw new JMSException("Object:[" + obj + "] is not Order");
}

}

/*
* (non-Javadoc)
*
* @see
* org.springframework.jms.support.converter.MessageConverter#fromMessage
* (javax.jms.Message)
*/
public Object fromMessage(Message msg) throws JMSException {
if (msg instanceof ObjectMessage) {
if (msg != null) {
Object obj = null;
byte[] bytes = (byte[]) ((Map) ((ObjectMessage) msg)
.getObjectProperty("Map")).get("Order");
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new ByteArrayInputStream(bytes));
obj = in.readObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return obj;
}
}
return msg;
}

你可能感兴趣的:(发现运行时会报错误, Object is not a primitive:)