我收到以下错误:

SEVERE: null
javax.jms.MessageFormatException: [C4017]: Invalid message format.
at com.sun.messaging.jmq.jmsclient.MapMessageImpl.checkValidObjectType(MapMessageImpl.java:653)
at com.sun.messaging.jmq.jmsclient.MapMessageImpl.setObject(MapMessageImpl.java:632)
at buyer.Main.sendCart(Main.java:287)


我尝试通过JMS系统中的MapMessage发送持久性对象后。而且我不太确定为什么会发生这种情况,因为MapMessage仅接受可序列化的对象作为值,而持久性实体是可序列化的。我将不胜感激任何帮助!我的Java代码如下。

package entities;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "carts")
@NamedQuery(
    name = "carts.findAll",
    query = "select c from Cart c"
)
public class Cart implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "buyer_id")
private Long buyerId;

@Column(name = "card_id")
private Long cardId;

@Column(name = "successful")
private boolean successful;

@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name = "buyer_id")
private Buyer buyer;

@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn(name = "card_id")
private Card card;

@OneToMany(mappedBy = "cart", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
private List<CartItem> cartItems;

public Cart() {
}

public Cart(Long buyerId, Long cardId, boolean successful) {
    this.buyerId = buyerId;
    this.cardId = cardId;
    this.successful = successful;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public Long getBuyerId() {
    return buyerId;
}

public void setBuyerId(Long buyerId) {
    this.buyerId = buyerId;
}

public Long getCardId() {
    return cardId;
}

public void setCardId(Long cardId) {
    this.cardId = cardId;
}

public boolean isSuccessful() {
    return successful;
}

public void setSuccessful(boolean successful) {
    this.successful = successful;
}

public Buyer getBuyer() {
    return buyer;
}

public void setBuyer(Buyer buyer) {
    this.buyer = buyer;
}

public Card getCard() {
    return card;
}

public void setCard(Card card) {
    this.card = card;
}

public List<CartItem> getCartItems() {
    return cartItems;
}

public void setCartItems(List<CartItem> cartItems) {
    this.cartItems = cartItems;
}

@Override
public int hashCode() {
    int hash = 7;
    hash = 53 * hash + Objects.hashCode(this.id);
    hash = 53 * hash + Objects.hashCode(this.buyerId);
    hash = 53 * hash + Objects.hashCode(this.cardId);
    hash = 53 * hash + (this.successful ? 1 : 0);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Cart other = (Cart) obj;
    if (this.successful != other.successful) {
        return false;
    }
    if (!Objects.equals(this.id, other.id)) {
        return false;
    }
    if (!Objects.equals(this.buyerId, other.buyerId)) {
        return false;
    }
    if (!Objects.equals(this.cardId, other.cardId)) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Cart{" + "id=" + id + ", buyerId=" + buyerId + ", cardId=" + cardId + ", successful=" + successful + '}';
}
}


发送消息的相关方法。

private static Buyer sendCart(Cart cart, String tempId, Buyer buyer) {
    JMSContext context = connectionFactory.createContext(2);

    try {
        Destination queue = context.createQueue("mediator");
        JMSProducer producer = context.createProducer();

        MapMessage message = context.createMapMessage();

        message.setObject("data", cart);
        message.setObject("tempid", tempId);
        message.setObject("type", MessageType.BUYER_SENDING_CART);

        producer.send(queue, message);

    } catch (JMSException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

最佳答案

问题是MapMessage。根据JavaDoc


  MapMessage对象用于发送一组名称/值对。的
  名称是String对象,值是
  Java编程语言。名称中的值必须不是
  null,不是一个空字符串。可以访问条目
  按名称顺序或随机排列。条目的顺序是
  未定义。 MapMessage继承自Message接口,并添加了一个
  包含地图的邮件正文。


和:


  它们也可以作为对象被一般地读取或写入。


因此,即使MapMessage提供了“ setObject”方法,使用的参数也必须是原始类型。

要传递非基本类型,请使用ObjectMessage

10-01 14:47