Recently I had to publish 100+ messages on our Tibco EMS to test out behavior of application server that consumes them, with different configurations. Surprisingly, there are no easy to use blogs on using Tibco EMS with Spring JMS to send messages. So, I had to scramble and here is generalized code inspired from that work. Obviously, this is far from production ready.
In this maven example we will read message from pre-defined location in file system and send to a Tibco EMS Queue.
application-context.xml
MessagePublisher.java
pom.xml
In this maven example we will read message from pre-defined location in file system and send to a Tibco EMS Queue.
application-context.xml
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
<?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: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.xsd"> | |
<context:component-scan base-package="com.abd.jms" /> | |
<context:property-placeholder location="classpath*:publisher.properties"/> | |
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> | |
<property name="connectionFactory" ref="tibcoConnectionFactory" /> <!-- authenticationConnectionFactory --> | |
</bean> | |
<!-- TIBCO Connection Factory Bean --> | |
<bean id="tibcoConnectionFactory" class="com.tibco.tibjms.TibjmsConnectionFactory"> | |
<constructor-arg value="tcp://abd-emsserver:5000"/> | |
<property name="userName" value="myusername"/> | |
<property name="userPassword" value="mypassword"/> | |
<property name="connAttemptCount" value="3"/> | |
<property name="connAttemptDelay" value="1500"/> | |
<property name="connAttemptTimeout" value="1000"/> | |
<property name="reconnAttemptCount" value="0"/> | |
<property name="reconnAttemptDelay" value="0"/> | |
<property name="reconnAttemptTimeout" value="0"/> | |
</bean> | |
</beans> |
MessagePublisher.java
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
package com.abd.jms; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.Session; | |
import javax.jms.TextMessage; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.jms.core.JmsTemplate; | |
import org.springframework.jms.core.MessageCreator; | |
import org.springframework.stereotype.Service; | |
@Service | |
public class MessagePublisher { | |
@Autowired | |
JmsTemplate jmsTemplate; | |
@Value("${messageFileLocation}") | |
private String messageFileLocation; | |
@Value("${queueName}") | |
private String queueName; | |
private static final Logger LOGGER = LoggerFactory.getLogger(MessagePublisher.class); | |
public void sendAMessage(){ | |
final String messageText = getFileContents(); | |
jmsTemplate.send(queueName, new MessageCreator() { | |
@Override | |
public Message createMessage(Session session) throws JMSException { | |
TextMessage message = session.createTextMessage(messageText); | |
return message; | |
} | |
}); | |
} | |
public String getFileContents (){ | |
final StringBuilder builder = new StringBuilder(); | |
try { | |
final BufferedReader reader = new BufferedReader(new FileReader(new File(messageFileLocation))); | |
String line = null; | |
while((line = reader.readLine()) != null) { | |
builder.append(line); | |
} | |
reader.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return builder.toString(); | |
} | |
} |
pom.xml
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
<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.abd.jms</groupId> | |
<artifactId>testMsgPublisher</artifactId> | |
<version>1.0.0.BUILD-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>MessagePublisher</name> | |
<prerequisites> | |
<maven>2.2.1</maven> | |
</prerequisites> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<slf4j-version>1.7.10</slf4j-version> | |
<log4j.version>1.2.17</log4j.version> | |
<junit.version>4.11</junit.version> | |
<spring.jms.version>3.1.1.RELEASE</spring.jms.version> | |
<tibjms.version>5.1.0</tibjms.version> | |
<tibjms.jarLocation>C:\Gems3.4\Gems\emslib\tibjms.jar</tibjms.jarLocation> | |
<jms.version>1.1</jms.version> | |
</properties> | |
<repositories> | |
<repository> | |
<id>repo.springsource.org.milestone</id> | |
<name>Spring Framework Maven Milestone Repository</name> | |
<url>https://repo.springsource.org/milestone</url> | |
</repository> | |
</repositories> | |
<dependencies> | |
<!-- Testing --> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>${junit.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<!-- Spring JMS --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jms</artifactId> | |
<version>${spring.jms.version}</version> | |
</dependency> | |
<!-- JMS --> | |
<dependency> | |
<groupId>javax.jms</groupId> | |
<artifactId>jms</artifactId> | |
<version>${jms.version}</version> | |
</dependency> | |
<!-- Tibco EMS --> | |
<dependency> | |
<groupId>com.tibco</groupId> | |
<artifactId>tibjms</artifactId> | |
<version>${tibjms.version}</version> | |
<scope>system</scope> | |
<systemPath>${tibjms.jarLocation}</systemPath> | |
</dependency> | |
<!-- Logging --> | |
<dependency> | |
<groupId>log4j</groupId> | |
<artifactId>log4j</artifactId> | |
<version>${log4j.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
<version>${slf4j-version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-log4j12</artifactId> | |
<version>${slf4j-version}</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.1</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
<compilerArgument>-Xlint:all</compilerArgument> | |
<showWarnings>true</showWarnings> | |
<showDeprecation>true</showDeprecation> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |