diff options
| author | Rajith Muditha Attapattu <rajith@apache.org> | 2008-01-16 22:02:04 +0000 |
|---|---|---|
| committer | Rajith Muditha Attapattu <rajith@apache.org> | 2008-01-16 22:02:04 +0000 |
| commit | 648f1a6384730ccf3183d3507663c849d98f19f0 (patch) | |
| tree | 17ca6939b1de8ae2c93b13cdd2f993d32ef59c19 /java/client/example/src | |
| parent | ad0910c4f2c7ef5bee8305d5cad76ecf8cf926fb (diff) | |
| download | qpid-python-648f1a6384730ccf3183d3507663c849d98f19f0.tar.gz | |
Modified the direct exchange examples to interoperate with the c++ and python examples
by allowing the ability to specify a routing key that is different from it's queue name.
Also deleted the sample.properties and replaced them by property files for each example.
I also removed the dependency on the BaseExamples class. Waiting for Arnaud to review it.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@612584 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/client/example/src')
4 files changed, 104 insertions, 107 deletions
diff --git a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java index 4ceeff4ba0..f30136242d 100644 --- a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java +++ b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java @@ -20,45 +20,45 @@ */ package org.apache.qpid.example.jmsexample.direct; -import org.apache.qpid.example.jmsexample.common.BaseExample; +import java.util.Properties; -import javax.jms.*; +import javax.jms.BytesMessage; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.ExceptionListener; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; /** * The example creates a MessageConsumer on the specified * Queue which is used to synchronously consume messages. */ -public class Consumer extends BaseExample +public class Consumer { /** * Used in log output. */ private static final String CLASS = "Consumer"; - /* The queue name */ - private String _queueName; - /** * Create a Consumer client. - * - * @param args Command line arguments. */ - public Consumer(String[] args) + public Consumer() { - super(CLASS, args); - _queueName = _argProcessor.getStringArgument("-queueName"); } /** * Run the message consumer example. - * - * @param args Command line arguments. */ public static void main(String[] args) { - _options.put("-queueName", "Queue name"); - _defaults.put("-queueName", "direct_message_queue"); - Consumer syncConsumer = new Consumer(args); + Consumer syncConsumer = new Consumer(); syncConsumer.runTest(); } @@ -69,8 +69,20 @@ public class Consumer extends BaseExample { try { - // Declare the connection - Connection connection = getConnection(); + // Load JNDI properties + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("direct.properties")); + + //Create the initial context + Context ctx = new InitialContext(properties); + + // look up destination + Destination destination = (Destination)ctx.lookup("directQueue"); + + // Lookup the connection factory + ConnectionFactory conFac = (ConnectionFactory)ctx.lookup("local"); + // create the connection + Connection connection = conFac.createConnection(); // As this application is using a MessageConsumer we need to set an ExceptionListener on the connection // so that errors raised within the JMS client library can be reported to the application @@ -93,9 +105,6 @@ public class Consumer extends BaseExample System.out.println(CLASS + ": Creating a non-transacted, auto-acknowledged session"); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - // lookup the queue - Queue destination = (Queue) getInitialContext().lookup(_queueName); - // Create a MessageConsumer System.out.println(CLASS + ": Creating a MessageConsumer"); MessageConsumer messageConsumer = session.createConsumer(destination); @@ -117,13 +126,13 @@ public class Consumer extends BaseExample } else { - byte[] body = new byte[(int) ((BytesMessage) message).getBodyLength()]; + byte[] body = new byte[(int) ((BytesMessage) message).getBodyLength()]; ((BytesMessage) message).readBytes(body); text = new String(body); } if (text.equals("That's all, folks!")) { - System.out.println(CLASS + ": Received final message for " + _queueName); + System.out.println(CLASS + ": Received final message " + text); end = true; } else @@ -138,7 +147,7 @@ public class Consumer extends BaseExample // Close the JNDI reference System.out.println(CLASS + ": Closing JNDI context"); - getInitialContext().close(); + ctx.close(); } catch (Exception exp) { diff --git a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java index 29873a7529..691bea3a33 100644 --- a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java +++ b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java @@ -20,23 +20,32 @@ */ package org.apache.qpid.example.jmsexample.direct; -import org.apache.qpid.example.jmsexample.common.BaseExample; - -import javax.jms.*; +import java.util.Properties; + +import javax.jms.BytesMessage; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.ExceptionListener; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; /** * The example creates a MessageConsumer on the specified * Queue and uses a MessageListener with this MessageConsumer * in order to enable asynchronous delivery. */ -public class Listener extends BaseExample implements MessageListener +public class Listener implements MessageListener { /* Used in log output. */ private static final String CLASS = "Listener"; - /* The queue name */ - private String _queueName; - /** * An object to synchronize on. */ @@ -54,25 +63,17 @@ public class Listener extends BaseExample implements MessageListener /** * Create an Listener client. - * - * @param args Command line arguments. */ - public Listener(String[] args) + public Listener() { - super(CLASS, args); - _queueName = _argProcessor.getStringArgument("-queueName"); } /** * Run the message consumer example. - * - * @param args Command line arguments. */ public static void main(String[] args) { - _options.put("-queueName", "Queue name"); - _defaults.put("-queueName", "message_queue"); - Listener listener = new Listener(args); + Listener listener = new Listener(); listener.runTest(); } @@ -83,8 +84,20 @@ public class Listener extends BaseExample implements MessageListener { try { - // Declare the connection - Connection connection = getConnection(); + // Load JNDI properties + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("direct.properties")); + + //Create the initial context + Context ctx = new InitialContext(properties); + + // look up destination + Destination destination = (Destination)ctx.lookup("directQueue"); + + // Lookup the connection factory + ConnectionFactory conFac = (ConnectionFactory)ctx.lookup("local"); + // create the connection + Connection connection = conFac.createConnection(); // As this application is using a MessageConsumer we need to set an ExceptionListener on the connection // so that errors raised within the JMS client library can be reported to the application @@ -108,9 +121,6 @@ public class Listener extends BaseExample implements MessageListener Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - // lookup the queue - Queue destination = session.createQueue(_queueName); - // Create a MessageConsumer System.out.println(CLASS + ": Creating a MessageConsumer"); @@ -144,7 +154,7 @@ public class Listener extends BaseExample implements MessageListener // Close the JNDI reference System.out.println(CLASS + ": Closing JNDI context"); - getInitialContext().close(); + ctx.close(); } catch (Exception exp) { @@ -177,7 +187,7 @@ public class Listener extends BaseExample implements MessageListener } if (text.equals("That's all, folks!")) { - System.out.println(CLASS + ": Received final message for " + _queueName); + System.out.println(CLASS + ": Received final message " + text); synchronized (_lock) { _finished = true; diff --git a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java index 8a91fce59e..d42a0d8788 100644 --- a/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java +++ b/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java @@ -20,40 +20,42 @@ */ package org.apache.qpid.example.jmsexample.direct; -import org.apache.qpid.example.jmsexample.common.BaseExample; +import java.util.Properties; -import javax.jms.*; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.Message; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; /** * Message producer example, sends message to a queue. */ -public class Producer extends BaseExample +public class Producer { /* Used in log output. */ private static final String CLASS = "Producer"; - /* The queue name */ - private String _queueName; + private int numMessages = 10; + private short deliveryMode = 0; /** * Create a Producer client. - * @param args Command line arguments. */ - public Producer (String[] args) + public Producer () { - super(CLASS, args); - _queueName = _argProcessor.getStringArgument("-queueName"); } /** * Run the message producer example. - * @param args Command line arguments. */ public static void main(String[] args) { - _options.put("-queueName", "Queue name"); - _defaults.put("-queueName", "direct_message_queue"); - Producer producer = new Producer(args); + Producer producer = new Producer(); producer.runTest(); } @@ -61,8 +63,21 @@ public class Producer extends BaseExample { try { - // Declare the connection - Connection connection = getConnection(); + + // Load JNDI properties + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("direct.properties")); + + //Create the initial context + Context ctx = new InitialContext(properties); + + // look up destination + Destination destination = (Destination)ctx.lookup("directQueue"); + + // Lookup the connection factory + ConnectionFactory conFac = (ConnectionFactory)ctx.lookup("local"); + // create the connection + Connection connection = conFac.createConnection(); // Create a session on the connection // This session is a default choice of non-transacted and uses the auto acknowledge feature of a session. @@ -70,7 +85,7 @@ public class Producer extends BaseExample Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // lookup the queue - Queue destination = (Queue) getInitialContext().lookup(_queueName); + //Queue destination = session.createQueue(_queueName); // Create a Message producer System.out.println(CLASS + ": Creating a Message Producer"); @@ -81,18 +96,18 @@ public class Producer extends BaseExample System.out.println(CLASS + ": Creating a TestMessage to send to the destination"); // Loop to publish the requested number of messages. - for (int i = 1; i < getNumberMessages() + 1; i++) + for (int i = 1; i < numMessages + 1; i++) { // NOTE: We have NOT HAD TO START THE CONNECTION TO BEGIN SENDING messages, // this is different to the consumer end as a CONSUMERS CONNECTIONS MUST BE STARTED BEFORE RECEIVING. message = session.createTextMessage("Message " + i); System.out.println(CLASS + ": Sending message: " + i); - messageProducer.send(message, getDeliveryMode(), Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + messageProducer.send(message, deliveryMode, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); } // And send a final message to indicate termination. - message = session.createTextMessage("That's all, folks!"); - messageProducer.send(message, getDeliveryMode(), Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); + message = session.createTextMessage("That's all, folks!"); + messageProducer.send(message, deliveryMode, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); // Close the connection to the broker System.out.println(CLASS + ": Closing connection"); @@ -100,11 +115,12 @@ public class Producer extends BaseExample // Close the JNDI reference System.out.println(CLASS + ": Closing JNDI context"); - getInitialContext().close(); + ctx.close(); } catch (Exception exp) { System.err.println(CLASS + ": Caught an Exception: " + exp); + exp.printStackTrace(); } } } diff --git a/java/client/example/src/main/java/sample.properties b/java/client/example/src/main/java/sample.properties deleted file mode 100644 index 9c7a6bfe42..0000000000 --- a/java/client/example/src/main/java/sample.properties +++ /dev/null @@ -1,38 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - - -java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory - -# A 0.10 connection factory -connectionfactory.ConnectionFactory = qpid:password=pass;username=name@tcp:localhost:5672 - -# register some queues in JNDI using the form -# queue.[jndiName] = [physicalName] -#queue.MyQueue = example.MyQueue -#queue.syncQueue = syncQueue -#queue.testQueue = testQueue - -# register some topics in JNDI using the form -# topic.[jndiName] = [physicalName] -#topic.ibmStocks = stocks.nyse.ibm -#topic.testTopic = testTopic - -# Register an AMQP destination in JNDI -# NOTE: Qpid currently only supports direct,topics and headers -# destination.[jniName] = [BindingURL] -destination.direct_message_queue = direct://amq.direct/routing_key/message_queue
\ No newline at end of file |
