tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fha...@apache.org
Subject svn commit: r620529 - /tomcat/trunk/test/org/apache/catalina/tribes/demos/MembersWithProperties.java
Date Mon, 11 Feb 2008 17:19:52 GMT
Author: fhanik
Date: Mon Feb 11 09:19:32 2008
New Revision: 620529

URL: http://svn.apache.org/viewvc?rev=620529&view=rev
Log:
added demo on how to use the payload

Added:
    tomcat/trunk/test/org/apache/catalina/tribes/demos/MembersWithProperties.java

Added: tomcat/trunk/test/org/apache/catalina/tribes/demos/MembersWithProperties.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/tribes/demos/MembersWithProperties.java?rev=620529&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/tribes/demos/MembersWithProperties.java (added)
+++ tomcat/trunk/test/org/apache/catalina/tribes/demos/MembersWithProperties.java Mon Feb
11 09:19:32 2008
@@ -0,0 +1,121 @@
+/*
+ * 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.catalina.tribes.demos;
+
+import java.io.Serializable;
+import org.apache.catalina.tribes.ManagedChannel;
+import org.apache.catalina.tribes.Member;
+import org.apache.catalina.tribes.Channel;
+import java.util.Properties;
+import org.apache.catalina.tribes.MembershipListener;
+import org.apache.catalina.tribes.util.UUIDGenerator;
+import org.apache.catalina.tribes.util.Arrays;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+public class MembersWithProperties implements MembershipListener{
+    Channel channel;
+    static Thread main;
+
+    public MembersWithProperties(Channel channel, Properties props) throws IOException {
+        this.channel = channel;
+        channel.addMembershipListener(this);
+        ManagedChannel mchannel = (ManagedChannel)channel;
+        mchannel.getMembershipService().setPayload(getPayload(props));
+    }
+    
+    byte[] getPayload(Properties props) throws IOException {
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();
+        props.store(bout,"");
+        return bout.toByteArray();
+    }
+    
+    Properties getProperties(byte[] payload) throws IOException {
+        ByteArrayInputStream bin = new ByteArrayInputStream(payload);
+        Properties props = new Properties();
+        props.load(bin);
+        return props;
+    }
+
+    public void memberAdded(Member member) {
+        try {
+            System.out.println("Received member added:"+member);
+            System.out.println("Payload["+member+"] :");
+            getProperties(member.getPayload()).store(System.out,"");
+        }catch ( Exception x ) {
+            x.printStackTrace();
+        }
+    }
+  
+    public void memberDisappeared(Member member) {
+        try {
+            System.out.println("Received member disappeared:"+member);
+            System.out.println("Payload["+member+"] :");
+            getProperties(member.getPayload()).store(System.out,"");
+        }catch ( Exception x ) {
+            x.printStackTrace();
+        }
+    }
+
+    public static void usage() {
+        System.out.println("Tribes Member Properties demo.");
+        System.out.println("Usage:\n\t" +
+                           "java MemberWithProperties \n\t" +
+                           "Channel options:" +
+                           ChannelCreator.usage() + "\n\n" +
+                           "Example:\n\t" +
+                           "java MembersWithProperties -port 4004\n\t" +
+                           "java MembersWithProperties -bind 192.168.0.45 -port 4005\n\t"
+
+                           "java MembersWithProperties -bind 192.168.0.45 -port 4005 -mbind
192.168.0.45 -count 100 -stats 10\n");
+    }
+
+    public static void main(String[] args) throws Exception {
+        if (args.length==0) usage();
+        main = Thread.currentThread();
+        ManagedChannel channel = (ManagedChannel) ChannelCreator.createChannel(args);
+        Properties props = new Properties();
+        props.setProperty("mydomainkey","mydomainvalue");
+        props.setProperty("someotherkey", Arrays.toString(UUIDGenerator.randomUUID(true)));
+        MembersWithProperties test = new MembersWithProperties(channel, props);
+        channel.start(channel.DEFAULT);
+        Runtime.getRuntime().addShutdownHook(new Shutdown(channel));
+        try {
+            main.sleep(Long.MAX_VALUE);
+        }catch(InterruptedException ix) {
+            main.sleep(5000);//allow everything to shutdown
+        }
+    }
+
+    public static class Shutdown extends Thread {
+        ManagedChannel channel = null;
+        public Shutdown(ManagedChannel channel) {
+            this.channel = channel;
+        }
+
+        public void run() {
+            System.out.println("Shutting down...");
+            try {
+                channel.stop(channel.DEFAULT);
+            } catch (Exception x) {
+                x.printStackTrace();
+            }
+            System.out.println("Channel stopped.");
+            main.interrupt();
+        }
+    }
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message