Tibco Publisher Consumer JmsTemplate Spring Integration

Tibco Publisher Consumer JmsTemplate Spring Integration

As you saw many example of creating Tibco publisher and consumer in previous tutorials. In this example you will see how to create tibco publisher and consumer using Spring JmsTemplate.

Tools needed:

  • Eclipse 4.0 or above
  • JDK 1.8
  • Maven 3.2 or above
  • Tibco version : 5.1.2
  • JMS Version : 1.1

All are included in project. Only you will have to download latest eclipse from here and if you have not already download JDK 8 then download from here.

  • Create maven project name: TibcoSpringJMSTemplate and final structure is below:

2016-03-27 22_50_17-Java EE - Eclipse

  • pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javahonk.tibco</groupId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>TibcoSpringJMSTemplate</name>
	<url>http://maven.apache.org</url>

	<properties>
		<org.springframework.version>4.1.5.RELEASE</org.springframework.version>
		<log4j.version>1.2.16</log4j.version>
		<org.apache.log4j.version>2.1</org.apache.log4j.version>
		<com.tibco.version>5.1.2</com.tibco.version>
		<jms.version>1.1</jms.version>
	</properties>

	<dependencies>

		<!-- Application Context (depends on spring-core, spring-expression, spring-aop, 
			spring-beans) This is the central artifact for Spring's Dependency Injection 
			Container and is generally always defined -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!-- Log4j -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>${org.apache.log4j.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${org.apache.log4j.version}</version>
		</dependency>

		<!--TIBCO -->
		<dependency>
			<groupId>com.tibco</groupId>
			<artifactId>tibjms</artifactId>
			<version>${com.tibco.version}</version>
		</dependency>

		<dependency>
			<groupId>javax.jms</groupId>
			<artifactId>jms</artifactId>
			<version>${jms.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>		

	</dependencies>
	
	<artifactId>TibcoSpringJMSTemplate</artifactId>
</project>
  • tibco.properties: Please note that we have separate read and write credentials for publisher and subscriber so I have created separate properties:
tibco_url=tcp://url:portNumber
tibco_queue=queue_name
tibco_msgType=javahonk.message

#Publisher credentials
tibco_username_pub=publisher_user_name
tibco_password_pub=publisher_user_password

#Publisher credentials
tibco_username_sub=subscriber_user_name
tibco_password_sub=subscriber_user_name
  • tibco-subscriber-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jms="http://www.springframework.org/schema/jms"
       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-4.1.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">

	<bean id="jndiTemplate_sub" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">com.tibco.tibjms.naming.TibjmsInitialContextFactory</prop>
                <prop key="java.naming.provider.url">${tibco_url}</prop>
                <prop key="java.naming.security.principal">${tibco_username_sub}</prop>
                <prop key="java.naming.security.credentials">${tibco_password_sub}</prop>
            </props>
        </property>
    </bean>

	<bean id="jmsConnectionFactory_sub" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <property name="jndiTemplate" ref="jndiTemplate_sub" />
        <property name="jndiName" value="QueueConnectionFactory" />
    </bean>
	
	<bean id="authenticationConnectionFactory_sub" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
		<property name="targetConnectionFactory" ref="jmsConnectionFactory_sub" />
		<property name="username" value="${tibco_username_sub}" />
		<property name="password" value="${tibco_password_sub}" />
	</bean>
	
	<jms:listener-container container-type="default" connection-factory="authenticationConnectionFactory_sub" acknowledge="auto" destination-type="queue">
		<jms:listener destination="${tibco_queue}" selector="JMSType='${tibco_msgType}'" ref="tibcoMessageConsumer" />
	</jms:listener-container>
	
	<bean id="tibcoMessageConsumer" class="com.javahonk.tibco.TibcoMessageConsumer">		
	</bean>	

</beans>
  • tibco-publisher-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

	<bean id="jndiTemplate_pub" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">com.tibco.tibjms.naming.TibjmsInitialContextFactory</prop>
                <prop key="java.naming.provider.url">${tibco_url}</prop> <!--dynamic depending on your project-->
                <prop key="java.naming.security.principal">${tibco_username_pub}</prop>
                <prop key="java.naming.security.credentials">${tibco_password_pub}</prop>
            </props>
        </property>
    </bean>

	<bean id="jmsConnectionFactory_pub" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <property name="jndiTemplate" ref="jndiTemplate_pub" />
        <property name="jndiName" value="QueueConnectionFactory" />
    </bean>
	
	<bean id="authenticationConnectionFactory_pub" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
		<property name="targetConnectionFactory" ref="jmsConnectionFactory_pub" />
		<property name="username" value="${tibco_username_pub}" />
		<property name="password" value="${tibco_password_pub}" />
	</bean>
	
	<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiTemplate" ref="jndiTemplate_pub" />
		<property name="jndiName" value="${tibco_queue}" />
	</bean>
	
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="authenticationConnectionFactory_pub" />
		<property name="defaultDestination" ref="destination" />		
	</bean>
	
	<bean id="tibcoMessageProducer" class="com.javahonk.tibco.TibcoMessageProducer">
		<property name="jmsTemplate" ref="jmsTemplate" />
	</bean>

</beans>
  • spring-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	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-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	<context:annotation-config />
	<context:component-scan base-package="com.javahonk, com.javahonk.tibco" />
	
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:tibco.properties</value>							
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
	</bean>	

	<import resource="tibco-publisher-context.xml"/>
	
	<import resource="tibco-subscriber-context.xml"/>
		
</beans>
  • TibcoMessageProducer.java:
package com.javahonk.tibco;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class TibcoMessageProducer {
	
	private static final Logger logger = LogManager.getLogger(TibcoMessageProducer.class.getName());
	
	private JmsTemplate jmsTemplate;

	public void sendMessages() throws JMSException {
		
		getJmsTemplate().send(new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				
				logger.info("Sending message...");
				Message message = session.createTextMessage("Java Honk sent message on Tibco queue.");
				message.setJMSType("javahonk.message");
				return message;
			}
		});		
	}

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
	
	
}
  • TibcoMessageConsumer.java:
package com.javahonk.tibco;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TibcoMessageConsumer implements MessageListener {
	
	private static final Logger logger = LogManager.getLogger(TibcoMessageConsumer.class.getName());
	
	@Override
	public void onMessage(Message message) {
		
		try {
			
			logger.info("Received message on destination: {}", message.getJMSDestination().toString());
			
			if (message instanceof TextMessage) {
				
				TextMessage txtMsg = (TextMessage) message;
				logger.info("Received response {}", message);
				Object msgTextObj = txtMsg.getText();				
				logger.info(msgTextObj.toString());
			}
		} catch (JMSException ex) {
			throw new RuntimeException(ex);
		}

	}

	

}
  • To test this application please user this class: JavaHonkTibcoTestApp.java:
package com.javahonk;

import javax.jms.JMSException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javahonk.tibco.TibcoMessageProducer;



public class JavaHonkTibcoTestApp {
	
	private static final Logger logger = LogManager.getLogger(JavaHonkTibcoTestApp.class);

	public static void main(String[] args) throws JMSException, InterruptedException {
		
		logger.info("Tibco messaging started...");
		
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
		
		TibcoMessageProducer tibcoMessageProducer = context.getBean(TibcoMessageProducer.class);
		
		for (int i = 0; i < 10; i++) {
			tibcoMessageProducer.sendMessages();
		}
		
		keepApplicationRunning();
		
		((AbstractApplicationContext) context).close();
	}

	private static void keepApplicationRunning() {
		
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
			}
		});

		while (!thread.isInterrupted()) {
		}
	}

}

Reference:

Download projectTibcoSpringJMSTemplate

Extra for knowledge: You can also directly map your listener method in spring configuration as below:

<jms:listener-container container-type="default" connection-factory="authenticationConnectionFactory_sub" acknowledge="auto" destination-type="queue">
	<jms:listener destination="${tibco_queue}" selector="JMSType='${tibco_msgType}'" ref="tibcoMessageConsumer" method="handleMessage"/>
</jms:listener-container>
	
 <bean id="tibcoMessageConsumer" class="com.javahonk.tibco.TibcoMessageConsumer">		
 </bean>

As you see above I have included method=”handleMessage” which can be defined inside consumer class as below then you don’t have to implement MessageListener:

package com.javahonk.tibco;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TibcoMessageConsumer  {
	
	private static final Logger logger = LogManager.getLogger(TibcoMessageConsumer.class.getName());
	
	public void handleMessage(String message){
		logger.info("Message received: {}", message);
	}

	

}

Leave a Reply

Your email address will not be published. Required fields are marked *