Author: ritchiem
Date: Tue Jul 17 02:55:56 2007
New Revision: 556868

URL: http://svn.apache.org/viewvc?view=rev&rev=556868
Log:
Addition of simple pub/sub examples.

Added:
    
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/
    
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Client.java
   (with props)
    
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/ConnectionSetup.java
   (with props)
    
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Publisher.java
   (with props)
    
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Subscriber.java
   (with props)

Added: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Client.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Client.java?view=auto&rev=556868
==============================================================================
--- 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Client.java
 (added)
+++ 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Client.java
 Tue Jul 17 02:55:56 2007
@@ -0,0 +1,72 @@
+/*
+ *  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.    
+ *
+ * 
+ */
+package org.apache.qpid.example.pubsub;
+
+import javax.jms.Connection;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Session;
+import javax.naming.NamingException;
+
+/**
+ * An abstract base class that wraps up the creation of a JMS client utilising 
JNDI
+ */
+public abstract class Client
+{
+    protected ConnectionSetup _setup;
+
+    protected Connection _connection;
+    protected Destination _destination;
+    protected Session _session;
+
+    public Client(String destination)
+    {
+        if (destination == null)
+        {
+            destination = ConnectionSetup.TOPIC_JNDI_NAME;
+        }
+
+        try
+        {
+            _setup = new ConnectionSetup();
+        }
+        catch (NamingException e)
+        {
+            //ignore
+        }
+
+        if (_setup != null)
+        {
+            try
+            {
+                _connection = _setup.getConnectionFactory().createConnection();
+                _destination = _setup.getDestination(destination);
+            }
+            catch (JMSException e)
+            {
+                System.err.println(e.getMessage());
+            }
+        }
+    }
+
+    public abstract void start();
+
+}
\ No newline at end of file

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Client.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Client.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/ConnectionSetup.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/ConnectionSetup.java?view=auto&rev=556868
==============================================================================
--- 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/ConnectionSetup.java
 (added)
+++ 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/ConnectionSetup.java
 Tue Jul 17 02:55:56 2007
@@ -0,0 +1,123 @@
+/*
+ *  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.    
+ *
+ * 
+ */
+package org.apache.qpid.example.pubsub;
+
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.util.Properties;
+
+/**
+ * This ConnectionSetup is a wrapper around JNDI it creates a number of 
entries.
+ *
+ * It is equivalent to a PropertyFile of value:
+ *
+ * connectionfactory.local=amqp://guest:[EMAIL 
PROTECTED]/test?brokerlist='localhost'
+ * connectionfactory.vm=amqp://guest:[EMAIL 
PROTECTED]/test?brokerlist='vm://:1'
+ *
+ * queue.queue=example.MyQueue
+ * topic.topic=example.hierarical.topic
+ *
+ */
+public class ConnectionSetup
+{
+    final static String INITIAL_CONTEXT_FACTORY = 
"org.apache.qpid.jndi.PropertiesFileInitialContextFactory";
+
+    final static String CONNECTION_JNDI_NAME = "local";
+    final static String CONNECTION_NAME = "amqp://guest:[EMAIL 
PROTECTED]/test?brokerlist='localhost'";
+
+    public static final String QUEUE_JNDI_NAME = "queue";
+    final static String QUEUE_NAME = "example.MyQueue";
+
+    public static final String TOPIC_JNDI_NAME = "topic";
+    final static String TOPIC_NAME = "example.hierarical.topic";
+
+    private Context _ctx;
+
+    public ConnectionSetup() throws NamingException
+    {
+
+        // Set the properties ...
+        Properties properties = new Properties();
+        properties.put(Context.INITIAL_CONTEXT_FACTORY, 
INITIAL_CONTEXT_FACTORY);
+        properties.put("connectionfactory." + CONNECTION_JNDI_NAME, 
CONNECTION_NAME);
+        properties.put("connectionfactory." + "vm", "amqp://guest:[EMAIL 
PROTECTED]/test?brokerlist='vm://:1'");
+
+        properties.put("queue." + QUEUE_JNDI_NAME, QUEUE_NAME);
+        properties.put("topic." + TOPIC_JNDI_NAME, TOPIC_NAME);
+        // Create the initial context
+        _ctx = new InitialContext(properties);
+
+    }
+
+    public ConnectionSetup(Properties properties) throws NamingException
+    {
+        _ctx = new InitialContext(properties);
+    }
+
+    public ConnectionFactory getConnectionFactory()
+    {
+
+        // Perform the lookups
+        try
+        {
+            return (ConnectionFactory) _ctx.lookup(CONNECTION_JNDI_NAME);
+        }
+        catch (NamingException e)
+        {
+            //ignore
+        }
+        return null;
+    }
+
+    public Destination getDestination(String jndiName)
+    {
+        // Perform the lookups
+        try
+        {
+            return (Destination) _ctx.lookup(jndiName);
+        }
+        catch (ClassCastException cce)
+        {
+            //ignore
+        }
+        catch (NamingException ne)
+        {
+            //ignore
+        }
+        return null;
+    }
+
+
+    public void close()
+    {
+        try
+        {
+            _ctx.close();
+        }
+        catch (NamingException e)
+        {
+            //ignore
+        }
+    }
+}

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/ConnectionSetup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/ConnectionSetup.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Publisher.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Publisher.java?view=auto&rev=556868
==============================================================================
--- 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Publisher.java
 (added)
+++ 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Publisher.java
 Tue Jul 17 02:55:56 2007
@@ -0,0 +1,81 @@
+/*
+ *  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.    
+ *
+ * 
+ */
+package org.apache.qpid.example.pubsub;
+
+import javax.jms.JMSException;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+
+/**
+ * A simple Publisher example.
+ *
+ * The class can take two arguments.
+ * java Publisher <destination> <msgCount>
+ * Where:
+ * destination is either 'topic' or 'queue'  (Default: topic)
+ * msgCount is the number of messages to send (Default : 100)
+ *
+ */
+public class Publisher extends Client
+{
+    int _msgCount;
+
+    public Publisher(String destination, int msgCount)
+    {
+        super(destination);
+        _msgCount = msgCount;
+    }
+
+    public void start()
+    {
+        try
+        {
+            _session = _connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            MessageProducer _producer = _session.createProducer(_destination);
+
+            for (int msgCount = 0; msgCount < _msgCount; msgCount++)
+            {
+                _producer.send(_session.createTextMessage("msg:" + msgCount));
+                System.out.println("Sent:" + msgCount);
+            }
+
+            System.out.println("Done.");
+            _connection.close();
+        }
+        catch (JMSException e)
+        {
+            e.printStackTrace();  //To change body of catch statement use File 
| Settings | File Templates.
+        }
+    }
+
+
+    public static void main(String[] args)
+    {
+
+        String destination = args.length > 2 ? args[1] : null;
+
+        int msgCount = args.length > 2 ? Integer.parseInt(args[2]) : 100;
+
+        new Publisher(destination, msgCount).start();
+    }
+
+}

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Publisher.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Publisher.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Subscriber.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Subscriber.java?view=auto&rev=556868
==============================================================================
--- 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Subscriber.java
 (added)
+++ 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Subscriber.java
 Tue Jul 17 02:55:56 2007
@@ -0,0 +1,98 @@
+/*
+ *  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.    
+ *
+ * 
+ */
+package org.apache.qpid.example.pubsub;
+
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.jms.Topic;
+import java.util.concurrent.CountDownLatch;
+
+
+/**
+ * Simple client that listens for the specified number of msgs on the given 
Destinaton
+ *
+ * The class can take two arguments.
+ * java Subscriber <destination> <msgCount>
+ * Where:
+ * destination is either 'topic' or 'queue'  (Default: topic)
+ * msgCount is the number of messages to send (Default : 100)
+ */
+public class Subscriber extends Client implements MessageListener
+{
+
+    CountDownLatch _count;
+
+    public Subscriber(String destination, int msgCount)
+    {
+        super(destination);
+        _count = new CountDownLatch(msgCount);
+    }
+
+
+    public void start()
+    {
+        try
+        {
+            _session = _connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
+
+            _session.createDurableSubscriber((Topic) 
_setup.getDestination(ConnectionSetup.TOPIC_JNDI_NAME),
+                                             
"exampleClient").setMessageListener(this);
+            _connection.start();
+            _count.await();
+
+            System.out.println("Done");
+
+            _connection.close();
+        }
+        catch (JMSException e)
+        {
+            e.printStackTrace();  //To change body of catch statement use File 
| Settings | File Templates.
+        }
+        catch (InterruptedException e)
+        {
+            e.printStackTrace();  //To change body of catch statement use File 
| Settings | File Templates.
+        }
+    }
+
+    public static void main(String[] args)
+    {
+        String destination = args.length > 2 ? args[1] : null;
+        int msgCount = args.length > 2 ? Integer.parseInt(args[2]) : 100;
+
+        new Subscriber(destination, msgCount).start();
+    }
+
+    public void onMessage(Message message)
+    {
+        try
+        {
+            _count.countDown();
+            System.out.println("Received msg:" + ((TextMessage) 
message).getText());
+        }
+        catch (JMSException e)
+        {
+            e.printStackTrace();  //To change body of catch statement use File 
| Settings | File Templates.
+        }
+    }
+}

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Subscriber.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/qpid/branches/M2/java/client/example/src/main/java/org/apache/qpid/example/pubsub/Subscriber.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to