Last active
December 28, 2015 21:59
-
-
Save robmadden/7568748 to your computer and use it in GitHub Desktop.
ActivemMQ Java onMessage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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