Skip to content

Instantly share code, notes, and snippets.

@robmadden
Last active December 28, 2015 21:59
Show Gist options
  • Save robmadden/7568748 to your computer and use it in GitHub Desktop.
Save robmadden/7568748 to your computer and use it in GitHub Desktop.
ActivemMQ Java onMessage
@Override public void onMessage(Message msg) {
if (msg instanceof BytesMessage) {
BytesMessage bMsg = (BytesMessage) msg;
StringBuilder buffer = new StringBuilder();
try {
for (int i = 0; i < (int)bMsg.getBodyLength(); i++) {
buffer.append((char) bMsg.readByte());
}
} catch (JMSException e) {
System.out.println("Failed to convert byte msg to string");
}
String text = buffer.toString().trim();
FailoverPojo pojo = new FailoverPojo();
Gson gson = new Gson();
pojo = gson.fromJson(text, FailoverPojo.class);
System.out.println("Received message: " + pojo.asJson()); // Must call acknowledge on Message object specifically.
try {
msg.acknowledge();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment