added demo on how to use the payload
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 11 Feb 2008 17:19:32 +0000 (17:19 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 11 Feb 2008 17:19:32 +0000 (17:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@620529 13f79535-47bb-0310-9956-ffa450edef68

test/org/apache/catalina/tribes/demos/MembersWithProperties.java [new file with mode: 0644]

diff --git a/test/org/apache/catalina/tribes/demos/MembersWithProperties.java b/test/org/apache/catalina/tribes/demos/MembersWithProperties.java
new file mode 100644 (file)
index 0000000..c883fae
--- /dev/null
@@ -0,0 +1,121 @@
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements.  See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License.  You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.apache.catalina.tribes.demos;\r
+\r
+import java.io.Serializable;\r
+import org.apache.catalina.tribes.ManagedChannel;\r
+import org.apache.catalina.tribes.Member;\r
+import org.apache.catalina.tribes.Channel;\r
+import java.util.Properties;\r
+import org.apache.catalina.tribes.MembershipListener;\r
+import org.apache.catalina.tribes.util.UUIDGenerator;\r
+import org.apache.catalina.tribes.util.Arrays;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.ByteArrayInputStream;\r
+import java.io.IOException;\r
+\r
+public class MembersWithProperties implements MembershipListener{\r
+    Channel channel;\r
+    static Thread main;\r
+\r
+    public MembersWithProperties(Channel channel, Properties props) throws IOException {\r
+        this.channel = channel;\r
+        channel.addMembershipListener(this);\r
+        ManagedChannel mchannel = (ManagedChannel)channel;\r
+        mchannel.getMembershipService().setPayload(getPayload(props));\r
+    }\r
+    \r
+    byte[] getPayload(Properties props) throws IOException {\r
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();\r
+        props.store(bout,"");\r
+        return bout.toByteArray();\r
+    }\r
+    \r
+    Properties getProperties(byte[] payload) throws IOException {\r
+        ByteArrayInputStream bin = new ByteArrayInputStream(payload);\r
+        Properties props = new Properties();\r
+        props.load(bin);\r
+        return props;\r
+    }\r
+\r
+    public void memberAdded(Member member) {\r
+        try {\r
+            System.out.println("Received member added:"+member);\r
+            System.out.println("Payload["+member+"] :");\r
+            getProperties(member.getPayload()).store(System.out,"");\r
+        }catch ( Exception x ) {\r
+            x.printStackTrace();\r
+        }\r
+    }\r
+  \r
+    public void memberDisappeared(Member member) {\r
+        try {\r
+            System.out.println("Received member disappeared:"+member);\r
+            System.out.println("Payload["+member+"] :");\r
+            getProperties(member.getPayload()).store(System.out,"");\r
+        }catch ( Exception x ) {\r
+            x.printStackTrace();\r
+        }\r
+    }\r
+\r
+    public static void usage() {\r
+        System.out.println("Tribes Member Properties demo.");\r
+        System.out.println("Usage:\n\t" +\r
+                           "java MemberWithProperties \n\t" +\r
+                           "Channel options:" +\r
+                           ChannelCreator.usage() + "\n\n" +\r
+                           "Example:\n\t" +\r
+                           "java MembersWithProperties -port 4004\n\t" +\r
+                           "java MembersWithProperties -bind 192.168.0.45 -port 4005\n\t" +\r
+                           "java MembersWithProperties -bind 192.168.0.45 -port 4005 -mbind 192.168.0.45 -count 100 -stats 10\n");\r
+    }\r
+\r
+    public static void main(String[] args) throws Exception {\r
+        if (args.length==0) usage();\r
+        main = Thread.currentThread();\r
+        ManagedChannel channel = (ManagedChannel) ChannelCreator.createChannel(args);\r
+        Properties props = new Properties();\r
+        props.setProperty("mydomainkey","mydomainvalue");\r
+        props.setProperty("someotherkey", Arrays.toString(UUIDGenerator.randomUUID(true)));\r
+        MembersWithProperties test = new MembersWithProperties(channel, props);\r
+        channel.start(channel.DEFAULT);\r
+        Runtime.getRuntime().addShutdownHook(new Shutdown(channel));\r
+        try {\r
+            main.sleep(Long.MAX_VALUE);\r
+        }catch(InterruptedException ix) {\r
+            main.sleep(5000);//allow everything to shutdown\r
+        }\r
+    }\r
+\r
+    public static class Shutdown extends Thread {\r
+        ManagedChannel channel = null;\r
+        public Shutdown(ManagedChannel channel) {\r
+            this.channel = channel;\r
+        }\r
+\r
+        public void run() {\r
+            System.out.println("Shutting down...");\r
+            try {\r
+                channel.stop(channel.DEFAULT);\r
+            } catch (Exception x) {\r
+                x.printStackTrace();\r
+            }\r
+            System.out.println("Channel stopped.");\r
+            main.interrupt();\r
+        }\r
+    }\r
+}
\ No newline at end of file