user3477008
user3477008

Reputation: 94

JMS ObjectMessage classpath

I'm trying to create a simple example with Java EE JMS.

If i try to receive an ObjectMessage, i need to have exactly the same path (packagename) as the other project, which sends the ObjectMessage.

For example i have in my sender project a class called Person in the packege "org.queue.sender" and exactly the same class in my receiver project in the package "org.queue.receiver".

As already said, if i try to get the objectmessage i get the following Exception: java.lang.ClassNotFoundException: org.queue.sender.Person

If i create a new package in my receiver project named org.queue.sender and transfer the class Peron there, then it run. but i think i couldn't be the really solution.

Is there a better solution?

Upvotes: 1

Views: 912

Answers (1)

wassgren
wassgren

Reputation: 19231

From the JavaDoc:

An ObjectMessage object is used to send a message that contains a serializable object in the Java programming language ("Java object"). It inherits from the Message interface and adds a body containing a single reference to an object. Only Serializable Java objects can be used.

So, objects passed via ObjectMessages must be Serializable i.e. it must be the same class and the exact same package.

If you need more flexible handling of messages I suggest that you use e.g. TextMessage and serialize/deserialize the objects using e.g. JSON or XML.

ObjectMapper mapper = ... ; // Get hold of a Jackson ObjectMapper
session.createTextMessage(mapper.writeValueAsString(myPojo));

// and on the receiving side
TextMessage message = ....; // From the message receiver
MyPojo myPojo = mapper.readValue(message.getText(), MyPojo.class);

Upvotes: 1

Related Questions