received_ = 0;
}
static public void main(String[] args) {
try {
CommandLine cmdline = new CommandLine(args);
if (cmdline.exists("help")) {
// the help option has been specified, print the usage
// information
usage();
} else if (cmdline.exists("topic")) {
// see if an ack mode has been specified. If it hasn't
// then assume CLIENT_ACKNOWLEDGE mode.
int ackMode = Session.CLIENT_ACKNOWLEDGE;
if (cmdline.exists("ackmode")) {
String amode = cmdline.value("ackmode");
if (amode.equals("auto")) {
ackMode = Session.AUTO_ACKNOWLEDGE;
} else if (amode.equals("dups")) {
ackMode = Session.DUPS_OK_ACKNOWLEDGE;
} else if (!amode.equals("client")) {
// ignore all ack modes, to test no acking
ackMode = -1;
}
}
// enable debugging - undocumented option
if (cmdline.exists("debug")) {
LoggerIfc logger = LoggerFactory.getLogger();
logger.setLogLevel(LogEventType.Debug);
}
String topic_name = cmdline.value("topic");
if (topic_name != null) {
// connect to the JNDI server and get a reference to
// root context
Hashtable props = new Hashtable();
String host = "localhost";
String port = null;
String jndiname = "JndiServer";
String mode = "rmi";
if (cmdline.exists("mode")) {
mode = cmdline.value("mode");
}
String modeType =
RmiJndiInitialContextFactory.class.getName();
if (cmdline.exists("jndiport")) {
port = cmdline.value("jndiport");
}
if (cmdline.exists("jndihost")) {
host = cmdline.value("jndihost");
}
// override the default server name if specified on the
// command line.
if (cmdline.exists("jndiname")) {
jndiname = cmdline.value("jndiname");
}
if (mode.equals("ipc") || mode.equals("tcp")) {
if (port == null) {
port = "3035";
}
props.put(Context.PROVIDER_URL,
"tcp://" + host + ":" + port + "/");
modeType =
"org.exolab.jms.jndi.mipc.IpcJndiInitialContextFactory";
} else if (mode.equals("http")) {
System.out.println("Using HTTP");
/* CONNECTING BY WAY OF PROXY
Client needs to set these system properties if
it requires to go through a proxy for HTTP.
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port);
*/
/* CONNECTING FOR SSL TRANSACTIONS.
Client needs to set these system properties
if it requires to go through a proxy for
HTTPS.
System.setProperty("https.proxyHost", "host");
System.setProperty("https.proxyPort", "port");
Client needs to pass in "-mode http -secure"
to use https,
Client needs -Djavax.net.ssl.trustStore=cacerts
passed in. JSSE is required to make this work.
*/
String type = "http://";
if (cmdline.exists("secure")) {
if (port == null) {
port = "8443";
}
type = "https://";
modeType =
"org.exolab.jms.jndi.http.SslHttpJndiInitialContextFactory";
} else {
if (port == null) {
port = "8080";
}
modeType =
"org.exolab.jms.jndi.http.HttpJndiInitialContextFactory";
}
props.put(Context.PROVIDER_URL,
type + host + ":" + port + "/" +
"openjms/servlet/OpenJMSJndi");
String url = cmdline.value("url");
if (url == null) {
if (cmdline.exists("secure")) {
url = type + "localhost:8443";
} else {
url = type + "localhost:8080";
}
}
// Client URL to allow server to send messages
// to registered consumers/receivers
System.getProperties().setProperty
(JndiConstants.HTTP_CLIENT_URL_PROPERTY, url);
} else {
if (port == null) {
port = "1099";
}
props.put(Context.PROVIDER_URL,
"rmi://" + host + ":" + port + "/" + jndiname);
}
System.err.println("Using provider url " + props.get(Context.PROVIDER_URL));
props.put(Context.INITIAL_CONTEXT_FACTORY, modeType);
Context context = new InitialContext(props);
// lookup the connection factory from the context
TopicConnectionFactory factory = (TopicConnectionFactory)
context.lookup("JmsTopicConnectionFactory");
// if we can't find the factory then throw an exception
if (factory == null) {
throw new RuntimeException(
"Failed to locate connection factory");
}
LoggerFactory.getLogger().logDebug("Have the connection factory " + factory);
TopicConnection connection =
factory.createTopicConnection();
TopicSession session =
connection.createTopicSession(false, ackMode);
Topic topic = null;
if (cmdline.exists("persistent")) {
topic = (Topic)context.lookup(topic_name);
} else {
topic = session.createTopic(topic_name);
}
if (topic == null) {
System.err.println("Failed to get administered object"
+ " exiting.....");
System.exit(-1);
}
String selector = null;
if (cmdline.exists("selector")) {
selector = "JMSPriority=1";
}
// determine if the summary flag has been specified. By
// default the message details are printed out. If the
// summary flag is specified then the count of the messages
// received is displayed.
boolean summary = false;
if (cmdline.exists("summary")) {
summary = true;
}
// if the 'name' option has been specified then assume a
// durable subscriber otherwise transient
TopicSubscriber subscriber = null;
String name = cmdline.value("name");
if (name != null) {
subscriber = session.createDurableSubscriber
(topic, name, selector, false);
} else {
subscriber = session.createSubscriber(topic, selector, false);
}
int secs = 60;
boolean counted = false;
int count = 1;
if (cmdline.exists("count")) {
counted = true;
try {
String value = cmdline.value("count");
count = Integer.parseInt(value);
} catch (Exception exception) {
System.err.println("Illegal count value");
System.exit(-1);
}
}
// the timeout is used to terminate the consumer if not
// messags are received within the specified timeframe.
// The default value is 5 minutes.
secs = 5 * 60;
if (cmdline.exists("timeout")) {
secs = Integer.parseInt(cmdline.value("timeout"));
}
SimpleConsumer consumer = (counted)
? new SimpleConsumer(name, connection, subscriber, ackMode, count, secs,summary)
: new SimpleConsumer(name, connection, subscriber, ackMode, secs, summary);
subscriber.setMessageListener(consumer);
connection.setExceptionListener(consumer);
// start the connection unless the noStart option is specified
if (!cmdline.exists("noStart")) {
connection.start();
}
} else {
System.err.println("Cannot subscribe to messages under "