Tibco Publisher Consumer Java Example
In last tutorial you saw how to integrate Tibco with Spring and create publisher consumer to send receive message on the queue. In this example I will show you basic simple java example to create publisher and consumer. To understand better I will create small maven project:
Tools needed:
- Eclipse 4.0 or above
- JDK 1.8
- JMS Version 1.1
- Tibco version 5.1.2
- Maven 3.1.2 or latest version
Maven project PureJavaTibcoPublisherConsumer structure:
- pom.xml to show what are dependencies are needed:
<?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.purejavatibcopublisherconsumer</groupId> <version>1.0-SNAPSHOT</version> <artifactId>PureJavaTibcoPublisherConsumer</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <com.tibco.version>5.1.2</com.tibco.version> <jms.version>1.1</jms.version> </properties> <dependencies> <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> </dependencies> </project>
- LoadProperties.java: Class which load all properties from Tibco.properies file and keep all data in map and we will call method getPropKeyValue to get properties value wherever needed:
package com.javahonk.messaging; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class LoadProperties { private static Map<String, String> propKeyValue = new HashMap<>(); private static void getPropertyValue() { Properties properties = new Properties(); InputStream input = null; try { input = new FileInputStream("src/main/resources/Tibco.properties"); properties.load(input); Enumeration<?> enumeration =properties.propertyNames(); while (enumeration.hasMoreElements()) { String keyVal = (String) enumeration.nextElement(); propKeyValue.put(keyVal, properties.getProperty(keyVal)); } } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static String getPropKeyValue(String key) { if (propKeyValue.get(key) == null) { getPropertyValue(); return propKeyValue.get(key); }else { return propKeyValue.get(key); } } }
- Tibco.properties: Here you could mentioned all Tibco related properties and replace these dummy value with your environment related actual values:
TIBCO_URL=tcp://javahonk.com:8222 PUB_USER_ID=javahonk PUB_PASS=javahonk SUB_USER_ID=javahonk SUB_PASS=javahonk TIBCO_QUEUE_NAMEjavahonk.Input
- JavaTibcoPublisher.java:
package com.javahonk.messaging; import javax.jms.Connection; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import static com.javahonk.messaging.LoadProperties.*; import com.tibco.tibjms.TibjmsConnectionFactory; public class JavaTibcoPublisher { public static void main(String[] args) throws InterruptedException { try { TibjmsConnectionFactory factory = new TibjmsConnectionFactory(getPropKeyValue("TIBCO_URL")); Connection connection = factory.createConnection(getPropKeyValue("PUB_USER_ID"), getPropKeyValue("PUB_PASS")); Session session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); Queue outputQueue = session.createQueue(getPropKeyValue("TIBCO_QUEUE_NAME")); MessageProducer msgProducer = session.createProducer(outputQueue); connection.start(); TextMessage msg = session.createTextMessage("Send message on Ticco queue."); msg.setJMSType("Test.Data"); for (int i = 0; i < 15; i++) { msgProducer.send(msg); Thread.sleep(1000); } } catch (JMSException e) { e.printStackTrace(); } } }
- JavaTibcoConsumer.java:
package com.javahonk.messaging; import static com.javahonk.messaging.LoadProperties.getPropKeyValue; import javax.jms.Connection; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import com.tibco.tibjms.TibjmsConnectionFactory; public class JavaTibcoConsumer implements MessageListener{ public static void main(String[] args) { JavaTibcoConsumer javaTibcoConsumer = new JavaTibcoConsumer(); javaTibcoConsumer.startSubscriber(); } private void startSubscriber() { Queue outputQueue; MessageConsumer msgConsumer; try { TibjmsConnectionFactory factory = new TibjmsConnectionFactory(getPropKeyValue("TIBCO_URL")); Connection connection = factory.createConnection(getPropKeyValue("SUB_USER_ID"), getPropKeyValue("SUB_PASS")); Session session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); outputQueue = session.createQueue(getPropKeyValue("TIBCO_QUEUE_NAME")); msgConsumer = session.createConsumer(outputQueue); msgConsumer.setMessageListener(this); connection.start(); } catch (JMSException e) { e.printStackTrace(); } } @Override public void onMessage(Message msg) { try { TextMessage txtMsg = (TextMessage) msg; System.out.println("Received response: "+txtMsg.getText()); System.out.println("Message type: "+msg.getJMSType()); System.out.println("Message details: "+txtMsg); } catch (Exception e) { e.printStackTrace(); } } }
Reference:
Download project:PureJavaTibcoPublisherConsumer