spring+mq常見錯誤

1、org.springframework.jms.support.converter.MessageConversionException: Cannot convert object of type [] to JMS message

Request processing failed; nested exception is org.springframework.jms.support.converter.MessageConversionException: Cannot convert object of type [] to JMS message. Supported message payloads are: String, byte array, Map<String,?>, Serializable object.] with root cause
org.springframework.jms.support.converter.MessageConversionException: Cannot convert object of type [] to JMS message. Supported message payloads are: String, byte array, Map<String,?>, Serializable object.

通常情況下,mq支持String, byte數組, Map<String,?>, 序列化對象這4種類型的傳輸。對於自定義類(User),還必須要實現序列化接口:
User.java

public class User implements Serializable  {
	private String userName;
	private Integer passwoed;
	
	public User(String userName, Integer passwoed) {
	    this.userName = userName;
	    this.passwoed = passwoed;
	}
	
	public String getUserName() {
	    return userName;
	}
	
	public void setUserName(String userName) {
	    this.userName = userName;
	}
	
	public Integer getPasswoed() {
	    return passwoed;
	}
	
	public void setPasswoed(Integer passwoed) {
	    this.passwoed = passwoed;
	}
	
	@Override
	public String toString() {
	    String s = "name:" + this.getUserName() + ",password:" + this.getPasswoed();
	    return s;
	}
}

2、javax.jms.JMSException: Failed to build body from content.

javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: Forbidden class User! This class is not trusted to be serialized as ObjectMessage payload. Please take a look at http://activemq.apache.org/objectmessage.html for more information on how to configure trusted classes.

貌似剛解決第一個,第二個問題就來了。怪我!!
這個錯誤是說,User類並不被mq允許,解決辦法也簡單,就是在mq的xml文件中,找到<amq:connectionFactory>標籤,加入一個屬性:trustAllPackages=“true”(直接寫死得了)

<!--設置activeMq的屬性,包括用戶名密碼-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="${spring.activemq.broker-url}"
                       userName="${spring.activemq.user}" password="${spring.activemq.password}" trustAllPackages="true"/>

問題迎刃而解
3、Attribute ‘trustAllPackages’ is not allowed to appear in element ‘amq:connectionFactory’
這個問題是在第二個基礎上產生的,筆者目前還沒解決。乾脆不用對象了,實在不行用JSON吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章