Receive Object Messages with Message Topic in Spring JMS

Use JAR files which are listed below:

activemq-core-5.7.0.jar
activemq-spring-5.12.0.jar
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjrt-1.8.9.jar
aspectjweaver-1.8.9.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
commons-dbcp2-2.1.1.jar
commons-logging-1.1.3.jar
commons-pool2-2.4.2.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.5.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
javax.jms-api-2.0.1.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
jmxtools-1.2.1.jar
mysql-connector-java-5.1.36.jar
spring-aop-4.2.0.RELEASE.jar
spring-aspects-4.3.3.RELEASE.jar
spring-beans-4.2.0.RELEASE.jar
spring-context-4.2.0.RELEASE.jar
spring-context-support-5.0.0.RELEASE.jar
spring-core-4.2.0.RELEASE.jar
spring-expression-4.2.0.RELEASE.jar
spring-jdbc-4.2.0.RELEASE.jar
spring-jms-4.2.0.RELEASE.jar
spring-messaging-5.0.3.RELEASE.jar
spring-orm-4.2.0.RELEASE.jar
spring-oxm-4.2.0.RELEASE.jar
spring-tx-4.2.0.RELEASE.jar

Create Java Project in Eclipse. Right click the project select Build Path\Configure Build Path menu. In Libraries tab, select all jar files above to the project. Besides, we need add appserv-rt library in glassfish4\glassfish\lib folder from GlassFish Server as photo below:




Create entity class: Invoice.java as below:

Invoice.java

package demo.entities;

import java.io.Serializable;
import java.util.Date;

public class Invoice implements Serializable {

	private String id;
	private String name;
	private Date dateCreation;
	private double total;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

}

This class contains methods to receive messages from destination

package demo.spring_jms;

import java.text.SimpleDateFormat;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import demo.entities.Invoice;

public class ObjectMessageListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		try {
			if (message instanceof ObjectMessage) {
				ObjectMessage objectMessage = (ObjectMessage) message;
				Invoice invoice = objectMessage.getBody(Invoice.class);
				System.out.println("Invoice Info");
				System.out.println("Id: " + invoice.getId());
				System.out.println("Name: " + invoice.getName());
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
				System.out.println("Date Creation: " + simpleDateFormat.format(invoice.getDateCreation()));
				System.out.println("Total: " + invoice.getTotal());
				System.out.println("==============================");
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

}

The Spring configuration shows a context:component-scan that picks up the JMS listener.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jms="http://www.springframework.org/schema/jms"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/jms
   						http://www.springframework.org/schema/jms/spring-jms.xsd">

	<context:component-scan base-package="demo.*"></context:component-scan>

	<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
		<property name="environment">
			<props>
				<prop key="java.naming.factory.initial">com.sun.enterprise.naming.SerialInitContextFactory
				</prop>
				<prop key="java.naming.factory.url.pkgs">com.sun.enterprise.naming</prop>
				<prop key="java.naming.factory.state">com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
				</prop>
				<prop key="org.omg.CORBA.ORBInitialHost">localhost</prop>
				<prop key="org.omg.CORBA.ORBInitialPort">3700</prop>
			</props>
		</property>
	</bean>

	<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiTemplate" ref="jndiTemplate" />
		<property name="jndiName" value="topic_connection_factory"></property>
	</bean>

	<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiTemplate" ref="jndiTemplate" />
		<property name="jndiName" value="spring_jms_topic_destination"></property>
	</bean>

	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"></property>
		<property name="defaultDestination" ref="destination"></property>
	</bean>

	<bean id="objectMessageListener" class="demo.spring_jms.ObjectMessageListener"></bean>

	<jms:listener-container connection-factory="connectionFactory"
		acknowledge="auto" destination-type="topic">
		<jms:listener destination="spring_jms_topic_destination"
			ref="objectMessageListener" method="onMessage" />
	</jms:listener-container>

</beans>




The program will automatically send the object message every 1 second

package demo.spring_jms;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {

		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

	}

}
Invoice Info
Id: iv1
Name: Name 1
Date Creation: 01/30/2018
Total: 1.0
==============================
Invoice Info
Id: iv2
Name: Name 2
Date Creation: 01/30/2018
Total: 2.0
==============================
Invoice Info
Id: iv3
Name: Name 3
Date Creation: 01/30/2018
Total: 3.0
==============================
Invoice Info
Id: iv4
Name: Name 4
Date Creation: 01/30/2018
Total: 4.0
==============================
Invoice Info
Id: iv5
Name: Name 5
Date Creation: 01/30/2018
Total: 5.0
==============================