Sep 30, 2018

Java Client to publish message in tibco queue

TIBCO Enterprise Message Service is a standards-based messaging platform (fully TCK certified to both the JMS 1.1 and 2.0 standards) that simplifies and accelerates the integration and management of data distribution in high-performance, enterprise environments.(Source: Wiki). In this post we will create a Java client which publishes message in Tiibco Queue.

1. Create a Maven project and add following dependencies in pom.xml.
<dependencies>
    <!-- https://mvnrepository.com/artifact/javax.jms/javax.jms-api -->
    <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>jms</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>tibco.jms</groupId>
        <artifactId>tibco_jms</artifactId>
        <version>6.1</version>
    </dependency>
</dependencies> 

2. Create a Java class TibcoQueue which manages tibco queue details.
/**
 * @author www.devinline.com(nikhil)
 *
 */
public class TibcoQueue {
 private String tibcoInstance;
 private String tibcoQueueName;
 private String userName;
 private String passWord;

 /**
  * @param tibcoInstance
  * @param tibcoQueueName
  * @param userName
  * @param passWord
  */
 public TibcoQueue(String tibcoInstance, String tibcoQueueName, 
   String userName, String passWord) {
  this.tibcoInstance = tibcoInstance;
  this.tibcoQueueName = tibcoQueueName;
  this.userName = userName;
  this.passWord = passWord;
 }

 public String getTibcoInstance() {
  return tibcoInstance;
 }

 public void setTibcoInstance(String tibcoInstance) {
  this.tibcoInstance = tibcoInstance;
 }

 public String getTibcoQueueName() {
  return tibcoQueueName;
 }

 public void setTibcoQueueName(String tibcoQueueName) {
  this.tibcoQueueName = tibcoQueueName;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassWord() {
  return passWord;
 }

 public void setPassWord(String passWord) {
  this.passWord = passWord;
 }
}

3. Create a Java class which does all configuration and send message to queue.
import java.util.List;

import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.tibco.tibjms.TibjmsQueueConnectionFactory;

/**
 * @author www.devinline.com (nikhil)
 *
 */

public class TibcoQueueUtils {
 private QueueConnection queueConnection;
 private QueueSession queueSession;
 private Queue sendQueue;
 private MessageProducer messageProducer;

 private void configureTibcoConnection(TibcoQueue tibcoQueue) throws Exception {
  QueueConnectionFactory queueConnectionFactory = 
    new TibjmsQueueConnectionFactory(tibcoQueue.getTibcoInstance());
  queueConnection = queueConnectionFactory.createQueueConnection(tibcoQueue.getUserName(),
    tibcoQueue.getPassWord());
  queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  sendQueue = queueSession.createQueue(tibcoQueue.getTibcoQueueName());
 }

 private void configureProducer(TibcoQueue tibcoQueue) throws Exception {
  configureTibcoConnection(tibcoQueue);
  messageProducer = queueSession.createProducer(sendQueue);
  messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  queueConnection.start();
 }

 private void closeProducer() throws Exception {
  messageProducer.close();
  closeTibcoConnection();
 }

 private void closeTibcoConnection() throws Exception {
  queueSession.close();
  queueConnection.stop();
  queueConnection.close();
 }

 public synchronized void sendMessage(String messageText, 
   TibcoQueue tibcoQueue) throws Exception {
  configureProducer(tibcoQueue);
  TextMessage message = queueSession.createTextMessage(messageText);
  messageProducer.send(message);
  closeProducer();
 }

 public synchronized void sendMessages(List<String> messageTexts, 
   TibcoQueue tibcoQueue) throws Exception {
  messageTexts.forEach(messageText -> {
   try {
    sendMessage(messageText, tibcoQueue);
   } catch (Exception e) {
    e.printStackTrace();
   }
  });
 }
}

4. Create Java class with main method which executes sendMessage() method and initiates message sending to queue.
import com.devinline.jms.tibco.utils.TibcoQueue;
import com.devinline.jms.utils.JMSConstants;
import com.devinline.jms.tibco.utils.TibcoQueueUtils;
/**
 * @author www.devinline.com (nikhil)
 *
 */
public class TibcoQueueClientPublisher {
 public static void main(String[] args) throws Exception {
  String message = "{\"order\":{\"orderNo\":\"XYZ1234\",\"status\":\"COMPLETED\"}}";
  /**/
  TibcoQueue tibcoQueue = new TibcoQueue(
    JMSConstants.BROKER_URL, 
    JMSConstants.QUEUE_NAME,
    JMSConstants.QUEUE_USERNAME, 
    JMSConstants.QUEUE_PASSWORD);
  
  TibcoQueueUtils queueUtil = new TibcoQueueUtils();
  System.out.println("Sending message");
  queueUtil.sendMessage(message,tibcoQueue);
  System.out.println("Message sent successfully!!");
 }
}

Note: Create JMSConstants class with fields broker_url, queueName, username and password and use it in above class accordingly.

Location: Bengaluru, Karnataka, India