Author: rhs
Date: Thu Aug 22 12:28:19 2013
New Revision: 1516433

URL: http://svn.apache.org/r1516433
Log:
PROTON-397: added example contributed by Marc Berkowitz

Added:
    qpid/proton/trunk/examples/messenger/java/   (with props)
    qpid/proton/trunk/examples/messenger/java/README.txt
    qpid/proton/trunk/examples/messenger/java/pom.xml
    qpid/proton/trunk/examples/messenger/java/recv   (with props)
    qpid/proton/trunk/examples/messenger/java/send   (with props)
    qpid/proton/trunk/examples/messenger/java/src/
    qpid/proton/trunk/examples/messenger/java/src/main/
    qpid/proton/trunk/examples/messenger/java/src/main/java/
    qpid/proton/trunk/examples/messenger/java/src/main/java/org/
    qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/
    qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/
    
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/
    
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/
    
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Recv.java
    
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Send.java
Modified:
    qpid/proton/trunk/   (props changed)
    qpid/proton/trunk/pom.xml

Propchange: qpid/proton/trunk/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Aug 22 12:28:19 2013
@@ -0,0 +1,2 @@
+build
+target

Propchange: qpid/proton/trunk/examples/messenger/java/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Aug 22 12:28:19 2013
@@ -0,0 +1 @@
+target

Added: qpid/proton/trunk/examples/messenger/java/README.txt
URL: 
http://svn.apache.org/viewvc/qpid/proton/trunk/examples/messenger/java/README.txt?rev=1516433&view=auto
==============================================================================
--- qpid/proton/trunk/examples/messenger/java/README.txt (added)
+++ qpid/proton/trunk/examples/messenger/java/README.txt Thu Aug 22 12:28:19 
2013
@@ -0,0 +1,22 @@
+This directory contains java examples that use the messenger API.
+Based on the python examples in  ../py
+
+  Send.java - a simple example of using the messenger API to send messages
+  Recv.java - a simple example of using the messenger API to receive messages
+
+Note that depending on the address passed into these scripts, you can
+use them in either a peer to peer or a brokered scenario.
+
+For brokered usage:
+  java Recv.class amqp://<broker>/<queue>
+  java Send.class -a amqp://<broker>/<queue> msg_1 ... msg_n
+
+For peer to peer usage:
+  # execute on <host> to receive messages from all local network interfaces
+  java Recv.class amqp://~0.0.0.0
+  java Send.class -a amqp://<host> msg_1 ... msg_n
+
+Or, use the shell scripts "recv" and "send" to run the java programs:
+recv [-v] [-n MAXMESSAGES] [-a ADDRESS] ... [-a ADDRESS]
+send [-a ADDRESS] [-s SUBJECT] MESSAGE ... MESSAGE
+

Added: qpid/proton/trunk/examples/messenger/java/pom.xml
URL: 
http://svn.apache.org/viewvc/qpid/proton/trunk/examples/messenger/java/pom.xml?rev=1516433&view=auto
==============================================================================
--- qpid/proton/trunk/examples/messenger/java/pom.xml (added)
+++ qpid/proton/trunk/examples/messenger/java/pom.xml Thu Aug 22 12:28:19 2013
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <parent>
+    <groupId>org.apache.qpid</groupId>
+    <artifactId>proton-project</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <relativePath>../../..</relativePath>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>proton-j-messenger-example</artifactId>
+  <name>proton-j-messenger-example</name>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.qpid</groupId>
+      <artifactId>proton-j-impl</artifactId>
+      <version>${project.parent.version}</version>
+    </dependency>
+  </dependencies>
+
+  <scm>
+    <url>http://svn.apache.org/viewvc/qpid/proton/</url>
+  </scm>
+</project>

Added: qpid/proton/trunk/examples/messenger/java/recv
URL: 
http://svn.apache.org/viewvc/qpid/proton/trunk/examples/messenger/java/recv?rev=1516433&view=auto
==============================================================================
--- qpid/proton/trunk/examples/messenger/java/recv (added)
+++ qpid/proton/trunk/examples/messenger/java/recv Thu Aug 22 12:28:19 2013
@@ -0,0 +1,12 @@
+#! /bin/bash
+# Usage: recv [-v] [-n MAXMESSAGES] [-a ADDRESS]*"
+# Subscribes to the given amqp addresses (by default, to 
amqp://localhost/test),
+# and prints messages received, upt to MAXMESSAGES. 
+# Prints message headers and body; -v means print all message properties.
+
+HERE=$(cd $(dirname $0); pwd)
+TOP=$(cd $(dirname $0); cd  ../../..; pwd)
+LIBS=$HERE/target/classes:$TOP/proton-j/proton-api/target/classes:$TOP/proton-j/proton/target/classes
+JFLAGS="-Djava.util.logging.config.file=$HERE/recv.trace.props -cp $LIBS"
+java -cp $LIBS org.apache.qpid.proton.example.Recv "$@"
+

Propchange: qpid/proton/trunk/examples/messenger/java/recv
------------------------------------------------------------------------------
    svn:executable = *

Added: qpid/proton/trunk/examples/messenger/java/send
URL: 
http://svn.apache.org/viewvc/qpid/proton/trunk/examples/messenger/java/send?rev=1516433&view=auto
==============================================================================
--- qpid/proton/trunk/examples/messenger/java/send (added)
+++ qpid/proton/trunk/examples/messenger/java/send Thu Aug 22 12:28:19 2013
@@ -0,0 +1,10 @@
+#! /bin/bash
+# Usage: send [-a ADDRESS] [-s SUBJECT] MESSAGE ... MESSAGE
+# sends each arg as a text-message to the given adress (by default, to 
amqp://localhost/test)
+
+HERE=$(cd $(dirname $0); pwd)
+TOP=$(cd $(dirname $0); cd  ../../..; pwd)
+LIBS=$HERE/target/classes:$TOP/proton-j/proton-api/target/classes:$TOP/proton-j/proton/target/classes
+JFLAGS="-Djava.util.logging.config.file=$HERE/send.trace.props -cp $LIBS"
+java -cp $LIBS org.apache.qpid.proton.example.Send "$@"
+

Propchange: qpid/proton/trunk/examples/messenger/java/send
------------------------------------------------------------------------------
    svn:executable = *

Added: 
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Recv.java
URL: 
http://svn.apache.org/viewvc/qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Recv.java?rev=1516433&view=auto
==============================================================================
--- 
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Recv.java
 (added)
+++ 
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Recv.java
 Thu Aug 22 12:28:19 2013
@@ -0,0 +1,131 @@
+/*
+ *
+ * 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.proton.example;
+
+import org.apache.qpid.proton.message.Message;
+import org.apache.qpid.proton.messenger.Messenger;
+import org.apache.qpid.proton.messenger.impl.MessengerImpl;
+import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Example/test of the java Messenger/Message API.
+ * Based closely qpid src/proton/examples/messenger/py/recv.py
+ * @author [email protected]
+ * @since 8/4/2013
+ */
+public class Recv {
+    private static Logger tracer = Logger.getLogger("proton.example");
+    private boolean verbose = false;
+    private int maxct = 0;
+    private List<String> addrs = new ArrayList<String>();
+
+    private static void usage() {
+        System.err.println("Usage: recv [-v] [-n MAXCT] [-a ADDRESS]*");
+        System.exit(2);
+    }
+
+    private Recv(String args[]) {
+        int i = 0;
+        while (i < args.length) {
+            String arg = args[i++];
+            if (arg.startsWith("-")) {
+                if ("-v".equals(arg)) {
+                    verbose = true;
+                } else if ("-a".equals(arg)) {
+                    addrs.add(args[i++]);
+                } else if ("-n".equals(arg)) {
+                    maxct = Integer.valueOf(args[i++]);
+                } else {
+                    System.err.println("unknown option " + arg);
+                    usage();
+                }
+            } else {
+                usage();
+            }
+        }
+        if (addrs.size() == 0) {
+            addrs.add("amqp://~0.0.0.0");
+        }
+    }
+
+    private static String safe(Object o) {
+        return String.valueOf(o);
+    }
+
+    private void print(int i, Message msg) {
+        StringBuilder b = new StringBuilder("message: ");
+        b.append(i).append("\n");
+        b.append("Address: ").append(msg.getAddress()).append("\n");
+        b.append("Subject: ").append(msg.getSubject()).append("\n");
+        if (verbose) {
+            b.append("Props:     ").append(msg.getProperties()).append("\n");
+            b.append("App Props: 
").append(msg.getApplicationProperties()).append("\n");
+            b.append("Msg Anno:  
").append(msg.getMessageAnnotations()).append("\n");
+            b.append("Del Anno:  
").append(msg.getDeliveryAnnotations()).append("\n");
+        } else {
+            ApplicationProperties p = msg.getApplicationProperties();
+            String s = (p == null) ? "null" : safe(p.getValue());
+            b.append("Headers: ").append(s).append("\n");
+        }
+        b.append(msg.getBody()).append("\n");
+        b.append("END").append("\n");
+        System.out.println(b.toString());
+    }
+
+    private void run() {
+        try {
+            Messenger mng = new MessengerImpl();
+            mng.start();
+            for (String a : addrs) {
+                mng.subscribe(a);
+            }
+            int ct = 0;
+            boolean done = false;
+            while (!done) {
+                mng.recv();
+                while (mng.incoming() > 0) {
+                    Message msg = mng.get();
+                    ++ct;
+                    print(ct, msg);
+                    if (maxct > 0 && ct >= maxct) {
+                        done = true;
+                        break;
+                    }
+                }
+            }
+            mng.stop();
+        } catch (Exception e) {
+            tracer.log(Level.SEVERE, "proton error", e);
+        }
+    }
+
+    public static void main(String args[]) {
+        Recv o = new Recv(args);
+        o.run();
+    }
+}
+

Added: 
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Send.java
URL: 
http://svn.apache.org/viewvc/qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Send.java?rev=1516433&view=auto
==============================================================================
--- 
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Send.java
 (added)
+++ 
qpid/proton/trunk/examples/messenger/java/src/main/java/org/apache/qpid/proton/example/Send.java
 Thu Aug 22 12:28:19 2013
@@ -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.proton.example;
+
+import org.apache.qpid.proton.amqp.messaging.AmqpValue;
+import org.apache.qpid.proton.message.Message;
+import org.apache.qpid.proton.message.impl.MessageImpl;
+import org.apache.qpid.proton.messenger.Messenger;
+import org.apache.qpid.proton.messenger.impl.MessengerImpl;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Example/test of the java Messenger/Message API.
+ * Based closely qpid src/proton/examples/messenger/py/send.py
+ * @author [email protected]
+ * @since 8/4/2013
+ */
+public class Send {
+
+    private static Logger tracer = Logger.getLogger("proton.example");
+    private String address = "amqp://0.0.0.0/test";
+    private String subject;
+    private String[] bodies;
+
+    private static void usage() {
+        System.err.println("Usage: send [-a ADDRESS] [-s SUBJECT] MSG+");
+        System.exit(2);
+    }
+
+    private Send(String args[]) {
+        int i = 0;
+        while (i < args.length) {
+            String arg = args[i++];
+            if (arg.startsWith("-")) {
+                if ("-a".equals(arg)) {
+                    address = args[i++];
+                } else if ("-s".equals(arg)) {
+                    subject = args[i++];
+                } else {
+                    System.err.println("unknown option " + arg);
+                    usage();
+                }
+            } else {
+                --i;
+                break;
+            }
+        }
+        bodies = Arrays.copyOfRange(args, i, args.length);
+    }
+
+    private void run() {
+        try {
+            Messenger mng = new MessengerImpl();
+            mng.start();
+            Message msg = new MessageImpl();
+            msg.setAddress(address);
+            if (subject != null) msg.setSubject(subject);
+            for (String body : bodies) {
+                msg.setBody(new AmqpValue(body));
+                mng.put(msg);
+            }
+            mng.send();
+            mng.stop();
+        } catch (Exception e) {
+            tracer.log(Level.SEVERE, "proton error", e);
+        }
+    }
+
+    public static void main(String args[]) {
+        Send o = new Send(args);
+        o.run();
+    }
+}
+
+
+

Modified: qpid/proton/trunk/pom.xml
URL: 
http://svn.apache.org/viewvc/qpid/proton/trunk/pom.xml?rev=1516433&r1=1516432&r2=1516433&view=diff
==============================================================================
--- qpid/proton/trunk/pom.xml (original)
+++ qpid/proton/trunk/pom.xml Thu Aug 22 12:28:19 2013
@@ -87,6 +87,7 @@
       <modules>
         <module>proton-j</module>
         <module>tests</module>
+        <module>examples/messenger/java</module>
       </modules>
     </profile>
     <profile>
@@ -96,6 +97,7 @@
         <!--proton-j-impl is required so we can use its EncoderImpl and 
DecoderImpl to generate test data -->
         <module>proton-j/proton</module>
         <module>tests</module>
+        <module>examples/messenger/java</module>
       </modules>
     </profile>
   </profiles>



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to