Nirvana Java Client: JMS BytesMessage Publisher
This example uses Nirvana JMS to publish Bytes Messages to a JMS Topic.
Usage
jmsbytespub <factoryname> <topicName> <count> <transacted> <Required Arguments> <factoryname> - JMS Factory (Must exist in target realm) <topicName> - JMS Topic to publish on <count> - Number of events to publish <transacted> - Whether the session is transacted Note: -? provides help on environment variables
Application Source Code
See also:
/**
*
* ----------------------------------------------------------------------------------
*
* PCB Systems Limited License Version 1.1
* Copyright PCB Systems Limited. All rights reserved
*
* In the event that you should download or otherwise use this software
* ( the "Software" ) you hereby acknowledge and agree that:
*
* 1. The Software is the property of PCB Systems Limited: Title, Copyright and all
* other proprietary rights, interest and benefit in and to the Software is and
* shall be owned by PCB Systems Limited;
*
* 2. You will not make copies of the Software whatsoever other than, if you should
* so wish, a single copy for archival purposes only;
*
* 3. You will not modify, reverse assemble, decompile, reverse engineer or otherwise
* translate the Software;
*
* 4. You will not redistribute, copy, forward electronically or circulate the Software
* to any person for any purpose whatsoever without the prior written consent of
* PCB Systems Limited;
*
* 5. You will not charge for, market or provide any managed service or product that
* is based upon or includes the Software or any variant of it; and
*
* 6. You will not use the Software for any purpose apart from your own personal,
* noncommercial and lawful use;
*
* You hereby agree that the software is used by you on an "as is" basis, without
* warranty of any kind. PCB Systems Limited hereby expressly disclaim all warranties
* and conditions, either expressed or implied, including but not limited to any
* implied warranties or conditions or merchantability and fitness for a particular
* purpose.
*
* You agree that you are solely responsible for determining the appropriateness of
* using the Software and assume all risks associated with it including but not
* limited to the risks of program errors, damage to or loss of of data, programs or
* equipment and unavailability or interruption of operations.
*
* PCB Systems Limited will not be liable for any direct damages or for any, special,
* incidental or indirect damages or for any economic consequential damages ( including
* lost profits or savings ), or any damage howsoever arising.
*
* ----------------------------------------------------------------------------------
*
*/
package com.pcbsys.nirvana.nJMSApps;
import com.pcbsys.foundation.utils.fEnvironment;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.NamingException;
/**
* This class demonstrates how to construct a Publisher that can send bytes messages on a topic
*/
public class BytesMessagePublisher extends Publisher {
static public String sClassVersion = "$Name: $ - $Revision: 1.1 $";
public static void main(String[] args) {
//Check to see if args were specified
if ((args.length == 1) && args[0].equals("-?")) {
UsageEnv();
}
if (args.length < 2) {
Usage();
System.exit(1);
}
//Process Environment Variables
processEnvironmentVariable("RNAME");
processEnvironmentVariable("PRINCIPAL");
processEnvironmentVariable("PASSWORD");
processEnvironmentVariable("CONTEXT_FACTORY");
processEnvironmentVariable("PROVIDER_URL");
processEnvironmentVariable("LOGLEVEL");
processEnvironmentVariable("HPROXY");
processEnvironmentVariable("HAUTH");
processEnvironmentVariable("CKEYSTORE");
processEnvironmentVariable("CKEYSTOREPASSWD");
processEnvironmentVariable("CAKEYSTORE");
processEnvironmentVariable("CAKEYSTOREPASSWD");
// Install any proxy server settings
fEnvironment.setProxyEnvironments();
// Install any ssl settings
fEnvironment.setSSLEnvironments();
//Create an instance for this class
BytesMessagePublisher publisher = new BytesMessagePublisher();
int count = 10000;
boolean transacted = false;
if (args.length > 2) {
count = new Integer(args[2]).intValue();
}
if (args.length > 3) {
transacted = new Boolean(args[2]).booleanValue();
}
//Publish to the topic specified
publisher.doIt(args[0], args[1], count, transacted);
}
/**
* This method demonstrates the Nirvana JMS API calls necessary to publish to
* a topic.
* It is called after all command line arguments have been received and
* validated
*
* @param factoryName The name of the factory to find
* @param topicName the name of the topic
* @param count number of message to publish
* @param transacted whether the session is transactional
*/
private void doIt(String factoryName, String topicName, int count, boolean transacted) {
try {
// get the initial context
Context ctx = getInitialContext();
TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup(factoryName);
// Create a Topic Connection from the Topic Connection Factory
TopicConnection tc = tcf.createTopicConnection();
// set the exceptionlistener
tc.setExceptionListener(this);
// Start the connection
tc.start();
// Create a Topic Sesson from the Topic Connection
TopicSession ts = tc.createTopicSession(transacted, 1);
// create the topic, and bind if necessary
Topic t = (Topic) getDestination(ctx, ts, topicName);
//Create a Topic Publisher from the Topic Session
TopicPublisher tp = ts.createPublisher(t);
//Prompt for a message
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1);
System.out.print("Enter a message to be published : ");
String str = br.readLine();
// create a bytes message
BytesMessage bmsg = ts.createBytesMessage();
bmsg.writeUTF(str);
//Loop for count
for (int x = 0; x < count; x++) {
try {
//Publish the message to the topic
tp.publish(bmsg);
if (transacted) {
ts.commit();
}
} catch (JMSException e) {
boolean committed = false;
while (!committed) {
try {
tp.publish(bmsg);
if (transacted) {
ts.commit();
}
committed = true;
} catch (JMSException ex) {
Thread.sleep(1000);
}
}
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
//Print a message to the console saying we are about to exit
System.out.println("Closing topic session and topic connection");
Thread.sleep(1000);
//Close the Topic Connection
tc.close();
//Close the Topic Session
ts.close();
//close the context
ctx.close();
} catch (NamingException ex) {
System.out.println("Naming Exception : Please ensure you have created the connection factory " + factoryName);
System.out.println("Naming Exception : This can be done using the Enterprise Manager JNDI panel or the jmsadmin command line application");
System.exit(0);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
}
/**
* Prints the usage message for this class
*/
private static void Usage() {
System.out.println("Usage ...\n\n");
System.out.println("jmsbytespub <factoryname> <topicName> <count> <transacted>\n");
System.out.println("<Required Arguments> \n");
System.out.println("<factoryname> - JMS Factory (Must exist in target realm)");
System.out.println("<topicName> - JMS Topic to publish on");
System.out.println("<count> - Number of events to publish");
System.out.println("<transacted> - Whether the session is transacted");
System.out.println("\n\nNote: -? provides help on environment variables \n");
}
}
// End of BytesMessagePublisher Class
EXAMPLE_SOURCE_END
