This is another problem that found when migrating EmbeddedJMS to EmbeddedActiveMQ: How to migrate from deprecated EmbeddedJMS to recommended EmbeddedActiveMQ
Previously to send message with object used
try {
producer.send(session.createObjectMessage(message));
session.commit();
} catch (JMSException e) {
log.error(e);
}
Where message object is just serializable.
With new approach those classes does not match and use this to produce message:
try {
final ClientMessage msg = clientSession.createMessage(true);
try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(message);
oos.flush();
msg.writeBodyBufferBytes(baos.toByteArray());
clientProducer.send(msg);
clientSession.commit();
}
} catch(ActiveMQException | IOException e) {
throw new RuntimeException(e);
}
To read it assumed that need to do it backwards like this:
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(clientMessage.getBodyBuffer().toByteBuffer().array());
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(arrayInputStream);
objectInputStream.readObject();
} catch(IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
However this does not work, there is exception:
java.io.StreamCorruptedException: invalid stream header: 000000B2
Anything that I'm missing? Is there any more efficient/elegant way to do this?