Author: rajith
Date: Wed Jan 16 14:02:04 2008
New Revision: 612584

URL: http://svn.apache.org/viewvc?rev=612584&view=rev
Log:
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.


Removed:
    
incubator/qpid/trunk/qpid/java/client/example/src/main/java/sample.properties
Modified:
    
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java
    
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java
    
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java

Modified: 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java?rev=612584&r1=612583&r2=612584&view=diff
==============================================================================
--- 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java
 (original)
+++ 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Consumer.java
 Wed Jan 16 14:02:04 2008
@@ -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 @@
     {
         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 @@
             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 @@
                 }
                 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 @@
 
             // Close the JNDI reference
             System.out.println(CLASS + ": Closing JNDI context");
-            getInitialContext().close();
+            ctx.close();
         }
         catch (Exception exp)
         {

Modified: 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java?rev=612584&r1=612583&r2=612584&view=diff
==============================================================================
--- 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java
 (original)
+++ 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Listener.java
 Wed Jan 16 14:02:04 2008
@@ -20,23 +20,32 @@
  */
 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.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 @@
 
     /**
      * 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 @@
     {
         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 @@
 
             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 @@
 
             // Close the JNDI reference
             System.out.println(CLASS + ": Closing JNDI context");
-            getInitialContext().close();
+            ctx.close();
         }
         catch (Exception exp)
         {
@@ -177,7 +187,7 @@
             }
             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;

Modified: 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java?rev=612584&r1=612583&r2=612584&view=diff
==============================================================================
--- 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java
 (original)
+++ 
incubator/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/jmsexample/direct/Producer.java
 Wed Jan 16 14:02:04 2008
@@ -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 @@
     {
         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 @@
             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 @@
             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 @@
 
             // 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();
         }
     }
 }


Reply via email to