package com.freedom.jms.samples; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.directory.InitialDirContext; public class JMSProducer { public static void main(java.lang.String[] args) { System.setProperty("javax.net.ssl.trustStore","/Users/mmik/Downloads/keystores/YOURACCOUNT ID/cloudmq.jks"); System.setProperty("javax.net.ssl.keyStore","/Users/mmik/Downloads/keystores/YOURACCOUNT ID/cloudmq.jks"); System.setProperty("javax.net.ssl.keyStorePassword","YOURACCOUNTPASSWORD"); System.setProperty("javax.net.ssl.trustStorePassword", "YOURACCOUNTPASSWORD"); String url = JMSValues.url; String icf = JMSValues.icf; java.util.Hashtable environment; String cf = JMSValues.msgEng; String writeQueue ; if (!JMSValues.multi) writeQueue = JMSValues.writeQueue; else writeQueue = JMSValues.topic+"1"; String msg = new String("TestMessage with JNDI"); InitialDirContext ctx = null; Connection connection; // Connect to it System.out.println("==================== JMS Sample Program with JNDI"); try { //=================================================== // Get the Initial Context Factory from name space System.out.println("==================== Get Init Context Factory"); environment = new java.util.Hashtable(); environment.put(Context.INITIAL_CONTEXT_FACTORY, icf); environment.put(Context.PROVIDER_URL, url); environment.put(Context.REFERRAL, "throw"); // Attempt to connect to the JNDI service provider, supplying // the properties previously supplied ctx = new InitialDirContext(environment); System.out.println("JNDI Initial Context found (" + ctx + ")"); //=================================================== // Get the Connection Factory ConnectionFactory factory; // Get to product specific System.out.println("==================== Get Conn Factory"); try { // Attempt to retrieve a ConnectionFactory from JNDI namespace factory = (ConnectionFactory) ctx.lookup(cf); } catch (java.lang.Exception e) { //System.out.println("Did not find " + cf + " so trying cn=" + cf); factory = (ConnectionFactory) ctx.lookup("cn=" + cf); } //=================================================== // Create the connection and start it System.out.println("==================== Create connection"); connection = factory.createConnection(); connection.start(); //=================================================== // Create a session - each thread should have there own Session session; // Required per thread boolean transacted = false; int sflags = Session.AUTO_ACKNOWLEDGE; System.out.println("==================== Create session"); session = connection.createSession(transacted, sflags); //=================================================== // Look up destination definition from name space Destination destination; // Look up destination definition from name space System.out.println("==================== Get destination from name space"); destination = (Destination) ctx.lookup("cn="+writeQueue); //=================================================== // Create a text message TextMessage message; System.out.println("==================== Create a TextMessage and add text"); String sb = new String(); for (int i=0; i < 10000; i++) { sb = sb + "x"; } message = session.createTextMessage(sb); System.out.println(sb.length()); //=================================================== // Send the message MessageProducer messageProducer; System.out.println("==================== Send the message"); messageProducer = session.createProducer(destination); for (int kk = 0; kk < 100; kk++) { long startTime = System.currentTimeMillis(); messageProducer.send(message); long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime); } System.out.println("message sent"+msg+"]"); //=================================================== // Clean up if (connection != null) { connection.close(); } System.out.println("Clean up is complete"); //=================================================== // Catch any exceptions } catch (JMSException je) { System.err.println("Caught: " + je); Exception e = je.getLinkedException(); if (e != null) { System.err.println("linked exception: " + e); } } catch (Exception e) { System.err.println("Caught: " + e); } } }