Hi Lavanya,
can you check your code aginst this sample?
import com.webmethods.jms.WmTopicConnection;
import com.webmethods.jms.WmTopicConnectionFactory;
import com.webmethods.jms.WmConnection;
import com.webmethods.jms.WmConnectionFactory;
import com.webmethods.jms.WmJMSConfig;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
public class SampleClient {
public SampleClient(String username, String password, List messageText) {
System.out.println("running SampleClient.");
Properties jndiProperties = null;
Context jndiContext = null;
TopicConnectionFactory tcf = null;
TopicConnection connection = null;
Topic topic = null;
try {
// load the jndi.properties file
try {
jndiProperties = new Properties();
File file = new File("jms.properties");
jndiProperties.load(new FileInputStream(file));
System.out.println("loaded " + file.getAbsolutePath());
System.out.println(jndiProperties.toString());
} catch (IOException ex) {
System.out.println("ERROR: jndi.properties not loaded");
ex.printStackTrace();
System.exit(0);
}
// connect to JNDI using jndi.properties
try {
jndiContext = new InitialContext(jndiProperties);
System.out.println("connected to JNDI provider");
} catch (NamingException ex) {
System.out.println("ERROR: failed to connect to JNDI provider");
ex.printStackTrace();
System.exit(0);
}
// lookup the administered objects
try {
tcf = (TopicConnectionFactory) jndiContext.lookup("<ConnectionFactoryName>");
System.out.println("found SampleClientConnectionFactory in JNDI");
topic = (Topic) jndiContext.lookup("<TopicName>");
System.out.println("found SampleTopic in JNDI");
} catch (NamingException ex) {
System.out.println("ERROR: JNDI lookup failed");
ex.printStackTrace();
System.exit(0);
}
// create a connection to the Broker
try {
System.out.println(((WmConnectionFactory)tcf).getSSLEncrypted());
System.out.println(((WmConnectionFactory)tcf).getSSLKeystore());
System.out.println(((WmConnectionFactory)tcf).getSSLKeystoreType());
System.out.println(((WmConnectionFactory)tcf).getSSLTruststore());
System.out.println(((WmConnectionFactory)tcf).getSSLTruststoreType());
((WmConnectionFactory)tcf).setSSLKeystore(jndiProperties.getProperty("com.webMethods.jms.naming.keystore"));
((WmConnectionFactory)tcf).setSSLTruststore(jndiProperties.getProperty("com.webMethods.jms.naming.truststore"));
connection = tcf.createTopicConnection(username, password);
System.out.println("connected to Broker \"" +
((WmTopicConnection) connection).getBroker() + "\"");
System.out.println(((WmConnection) connection).getSSLEncryptionLevel());
} catch (JMSException ex) {
String broker = ((WmTopicConnectionFactory) tcf).getBrokerList();
System.out.println("ERROR: unable to connect to Broker \"" + broker + "\"");
ex.printStackTrace();
System.exit(0);
}
// publish the message(s)
try {
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSender sender = session.createSender(topic);
for (int i = 0; i < messageText.size(); i++) {
TextMessage msg = session.createTextMessage((String) messageText.get(i));
sender.send(msg);
System.out.println("published message \"" + messageText.get(i) + "\"");
}
System.out.println("published " + messageText.size() + " message(s)");
} catch (Exception ex) {
System.out.println("ERROR: unable to publish message(s)");
ex.printStackTrace();
System.exit(0);
}
// close the connection
connection.close();
System.out.println("connection closed");
} catch (Exception ex) {
System.out.println("ERROR: unexpected error");
ex.printStackTrace();
}
}
}
Please note that this originally for using Queues, I have modifiied it for Topics for this post.
It is using WmJMSNaming as JNDI-Provider and is prepared to use Authentication and Encryption.
Replace the values in “<>” with your real names.
But I think it point put how to achieve a connectivity to Broker JMS from external Java Programs.
Please check Broker Java API documenation (contains Broker JMS API documentation as well) for correct ClassNames and fruther informations.
A glance at the Broker_Messaging_Programmers_Guide might also be helpful.
Regards,
Holger
#webMethods#Universal-Messaging-Broker#Integration-Server-and-ESB