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

阅读更多

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


  
       dataSource="#dataSource" createTablesOnStartup="true"
    useDatabaseLock="false" />
   
  

  
   
  

 

 

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

 

public Message toMessage(Object obj, Session session) throws JMSException {
  // check Type
  if (obj instanceof IPAddressInfo) {
   ActiveMQObjectMessage objMsg = (ActiveMQObjectMessage) session
     .createObjectMessage();
   Map map = new HashMap();
   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;
 }

 

你可能感兴趣的:(ActiveMQ,HSQLDB,JMS,MySQL,SQL,Server)