Send Object Message JMS
In previous example you saw how to send simple string message and XML file using JMS. Today I will show you how to send Object thorough message. JMS API support sending object through message only condition is object should be Serializable before sending. We will use Person.java class which implements Serializable interface with three attribute. As this class is implementing Serializable interface so that we can send through message. Please follow steps below:
- JBoss application server jboss-6.1.0.Final community version (You can use any version of JBoss application server which you could download from here). If you are using version other than jboss-6.1.0.Final community version only JMS configuration will be different and use this tutorial to configure Queue and topic.
- Eclipse Kepler (You could use any version of eclipse above 4.2)
- Configure JMS Queue destination on jboss server : Go to jboss-6.1.0.Final\server\default\deploy\hornetq\hornetq-jms.xml and add below Queue:
- Create maven project JMSSendObjectMessage and below is final project snap shot where we have create publisher subscriber and one model class Person which will be send through messaging
- Configure JBoss application server in eclipse using this tutorial
- To include include all JBoss related jars to to class path: right click project –> properties –> Java build path –> on library tab click add library… –> Server runtime –> if you configured Jboss in eclipse you will see JBoss Runtime server choose it –> click finish.
- Person.java that will be send through message:
- Start JBoss server: To run above program you will have start JBoss first. You can start JBoss either form inside your eclipse or go to %JBOSS_HOME%/bin –> Double click run.bat to start the server. Once server is started it will pick up configured topic automatically.
- Both class has main method start them separately as java application. Once both started go to publisher console and pass value which you want to send to the subscriber. Below is example:
Publisher console – Enter value separated by space for firstName, lastName and location then press enter:
Subscriber console – Subscriber will process Person object and print its value on console:
- That’s it for more details on JMS messaging please refer oracle here