You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by fh...@apache.org on 2006/09/11 16:47:47 UTC
svn commit: r442235 [2/2] - in
/tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos: ./
ChannelCreator.java CoordinationDemo.java EchoRpcTest.java
IntrospectionUtils.java LoadTest.java MapDemo.java
Added: tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/LoadTest.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/LoadTest.java?view=auto&rev=442235
==============================================================================
--- tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/LoadTest.java (added)
+++ tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/LoadTest.java Mon Sep 11 07:47:47 2006
@@ -0,0 +1,424 @@
+/*
+ * Copyright 1999,2004-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 java.util.Random;
+
+import org.apache.catalina.tribes.ByteMessage;
+import org.apache.catalina.tribes.ChannelException;
+import org.apache.catalina.tribes.ChannelListener;
+import org.apache.catalina.tribes.ManagedChannel;
+import org.apache.catalina.tribes.Member;
+import org.apache.catalina.tribes.MembershipListener;
+import org.apache.catalina.tribes.io.XByteBuffer;
+import org.apache.catalina.tribes.Channel;
+import java.io.Externalizable;
+
+
+/**
+ * <p>Title: </p>
+ *
+ * <p>Description: </p>
+ *
+ * <p>Copyright: Copyright (c) 2005</p>
+ *
+ * <p>Company: </p>
+ *
+ * @author not attributable
+ * @version 1.0
+ */
+public class LoadTest implements MembershipListener,ChannelListener, Runnable {
+ protected static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LoadTest.class);
+ public static int size = 24000;
+ public static Object mutex = new Object();
+ public boolean doRun = true;
+
+ public long bytesReceived = 0;
+ public float mBytesReceived = 0;
+ public int messagesReceived = 0;
+ public boolean send = true;
+ public boolean debug = false;
+ public int msgCount = 100;
+ ManagedChannel channel=null;
+ public int statsInterval = 10000;
+ public long pause = 0;
+ public boolean breakonChannelException = false;
+ public boolean async = false;
+ public long receiveStart = 0;
+ public int channelOptions = Channel.SEND_OPTIONS_DEFAULT;
+
+ static int messageSize = 0;
+
+ public static long messagesSent = 0;
+ public static long messageStartSendTime = 0;
+ public static long messageEndSendTime = 0;
+ public static int threadCount = 0;
+
+ public static synchronized void startTest() {
+ threadCount++;
+ if ( messageStartSendTime == 0 ) messageStartSendTime = System.currentTimeMillis();
+ }
+
+ public static synchronized void endTest() {
+ threadCount--;
+ if ( messageEndSendTime == 0 && threadCount==0 ) messageEndSendTime = System.currentTimeMillis();
+ }
+
+
+ public static synchronized long addSendStats(long count) {
+ messagesSent+=count;
+ return 0l;
+ }
+
+ private static void printSendStats(long counter, int messageSize) {
+ float cnt = (float)counter;
+ float size = (float)messageSize;
+ float time = (float)(System.currentTimeMillis()-messageStartSendTime) / 1000f;
+ log.info("****SEND STATS-"+Thread.currentThread().getName()+"*****"+
+ "\n\tMessage count:"+counter+
+ "\n\tTotal bytes :"+(long)(size*cnt)+
+ "\n\tTotal seconds:"+(time)+
+ "\n\tBytes/second :"+(size*cnt/time)+
+ "\n\tMBytes/second:"+(size*cnt/time/1024f/1024f));
+ }
+
+
+
+ public LoadTest(ManagedChannel channel,
+ boolean send,
+ int msgCount,
+ boolean debug,
+ long pause,
+ int stats,
+ boolean breakOnEx) {
+ this.channel = channel;
+ this.send = send;
+ this.msgCount = msgCount;
+ this.debug = debug;
+ this.pause = pause;
+ this.statsInterval = stats;
+ this.breakonChannelException = breakOnEx;
+ }
+
+
+
+ public void run() {
+
+ long counter = 0;
+ long total = 0;
+ LoadMessage msg = new LoadMessage();
+ int messageSize = LoadTest.messageSize;
+
+ try {
+ startTest();
+ while (total < msgCount) {
+ if (channel.getMembers().length == 0 || (!send)) {
+ synchronized (mutex) {
+ try {
+ mutex.wait();
+ } catch (InterruptedException x) {
+ log.info("Thread interrupted from wait");
+ }
+ }
+ } else {
+ try {
+ //msg.setMsgNr((int)++total);
+ counter++;
+ if (debug) {
+ printArray(msg.getMessage());
+ }
+ channel.send(channel.getMembers(), msg, channelOptions);
+ if ( pause > 0 ) {
+ if ( debug) System.out.println("Pausing sender for "+pause+" ms.");
+ Thread.sleep(pause);
+ }
+ } catch (ChannelException x) {
+ if ( debug ) log.error("Unable to send message:"+x.getMessage(),x);
+ log.error("Unable to send message:"+x.getMessage());
+ ChannelException.FaultyMember[] faulty = x.getFaultyMembers();
+ for (int i=0; i<faulty.length; i++ ) log.error("Faulty: "+faulty[i]);
+ --counter;
+ if ( this.breakonChannelException ) throw x;
+ }
+ }
+ if ( (counter % statsInterval) == 0 && (counter > 0)) {
+ //add to the global counter
+ counter = addSendStats(counter);
+ //print from the global counter
+ //printSendStats(LoadTest.messagesSent, LoadTest.messageSize, LoadTest.messageSendTime);
+ printSendStats(LoadTest.messagesSent, LoadTest.messageSize);
+
+ }
+
+ }
+ }catch ( Exception x ) {
+ log.error("Captured error while sending:"+x.getMessage());
+ if ( debug ) log.error("",x);
+ printSendStats(LoadTest.messagesSent, LoadTest.messageSize);
+ }
+ endTest();
+ }
+
+
+
+ /**
+ * memberAdded
+ *
+ * @param member Member
+ * @todo Implement this org.apache.catalina.tribes.MembershipListener
+ * method
+ */
+ public void memberAdded(Member member) {
+ log.info("Member added:"+member);
+ synchronized (mutex) {
+ mutex.notifyAll();
+ }
+ }
+
+ /**
+ * memberDisappeared
+ *
+ * @param member Member
+ * @todo Implement this org.apache.catalina.tribes.MembershipListener
+ * method
+ */
+ public void memberDisappeared(Member member) {
+ log.info("Member disappeared:"+member);
+ }
+
+ public boolean accept(Serializable msg, Member mbr){
+ return (msg instanceof LoadMessage) || (msg instanceof ByteMessage);
+ }
+
+ public void messageReceived(Serializable msg, Member mbr){
+ if ( receiveStart == 0 ) receiveStart = System.currentTimeMillis();
+ if ( debug ) {
+ if ( msg instanceof LoadMessage ) {
+ printArray(((LoadMessage)msg).getMessage());
+ }
+ }
+
+ if ( msg instanceof ByteMessage && !(msg instanceof LoadMessage)) {
+ LoadMessage tmp = new LoadMessage();
+ tmp.setMessage(((ByteMessage)msg).getMessage());
+ msg = tmp;
+ tmp = null;
+ }
+
+
+ bytesReceived+=((LoadMessage)msg).getMessage().length;
+ mBytesReceived+=((float)((LoadMessage)msg).getMessage().length)/1024f/1024f;
+ messagesReceived++;
+ if ( (messagesReceived%statsInterval)==0 || (messagesReceived==msgCount)) {
+ float bytes = (float)(((LoadMessage)msg).getMessage().length*messagesReceived);
+ float seconds = ((float)(System.currentTimeMillis()-receiveStart)) / 1000f;
+ log.info("****RECEIVE STATS-"+Thread.currentThread().getName()+"*****"+
+ "\n\tMessage count :"+(long)messagesReceived+
+ "\n\tTotal bytes :"+(long)bytes+
+ "\n\tTotal mbytes :"+(long)mBytesReceived+
+ "\n\tTime since 1st:"+seconds+" seconds"+
+ "\n\tBytes/second :"+(bytes/seconds)+
+ "\n\tMBytes/second :"+(mBytesReceived/seconds)+"\n");
+
+ }
+ }
+
+
+ public static void printArray(byte[] data) {
+ System.out.print("{");
+ for (int i=0; i<data.length; i++ ) {
+ System.out.print(data[i]);
+ System.out.print(",");
+ }
+ System.out.println("} size:"+data.length);
+ }
+
+
+
+ //public static class LoadMessage implements Serializable {
+ public static class LoadMessage extends ByteMessage implements Serializable {
+
+ public static byte[] outdata = new byte[size];
+ public static Random r = new Random(System.currentTimeMillis());
+ public static int getMessageSize (LoadMessage msg) {
+ int messageSize = msg.getMessage().length;
+ if ( ((Object)msg) instanceof ByteMessage ) return messageSize;
+ try {
+ messageSize = XByteBuffer.serialize(new LoadMessage()).length;
+ log.info("Average message size:" + messageSize + " bytes");
+ } catch (Exception x) {
+ log.error("Unable to calculate test message size.", x);
+ }
+ return messageSize;
+ }
+ static {
+ r.nextBytes(outdata);
+ }
+
+ protected byte[] message = getMessage();
+
+ public LoadMessage() {
+ }
+
+ public byte[] getMessage() {
+ if ( message == null ) {
+ message = outdata;
+ }
+ return message;
+ }
+
+ public void setMessage(byte[] data) {
+ this.message = data;
+ }
+ }
+
+ public static void usage() {
+ System.out.println("Tribes Load tester.");
+ System.out.println("The load tester can be used in sender or received mode or both");
+ System.out.println("Usage:\n\t"+
+ "java LoadTest [options]\n\t"+
+ "Options:\n\t\t"+
+ "[-mode receive|send|both] \n\t\t"+
+ "[-startoptions startflags (default is Channel.DEFAULT) ] \n\t\t"+
+ "[-debug] \n\t\t"+
+ "[-count messagecount] \n\t\t"+
+ "[-stats statinterval] \n\t\t"+
+ "[-pause nrofsecondstopausebetweensends] \n\t\t"+
+ "[-threads numberofsenderthreads] \n\t\t"+
+ "[-size messagesize] \n\t\t"+
+ "[-sendoptions channeloptions] \n\t\t"+
+ "[-break (halts execution on exception)]\n"+
+ "[-shutdown (issues a channel.stop() command after send is completed)]\n"+
+ "\tChannel options:"+
+ ChannelCreator.usage()+"\n\n"+
+ "Example:\n\t"+
+ "java LoadTest -port 4004\n\t"+
+ "java LoadTest -bind 192.168.0.45 -port 4005\n\t"+
+ "java LoadTest -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 {
+ boolean send = true;
+ boolean debug = false;
+ long pause = 0;
+ int count = 1000000;
+ int stats = 10000;
+ boolean breakOnEx = false;
+ int threads = 1;
+ boolean shutdown = false;
+ int startoptions = Channel.DEFAULT;
+ int channelOptions = Channel.SEND_OPTIONS_DEFAULT;
+ if ( args.length == 0 ) {
+ args = new String[] {"-help"};
+ }
+ for (int i = 0; i < args.length; i++) {
+ if ("-threads".equals(args[i])) {
+ threads = Integer.parseInt(args[++i]);
+ } else if ("-count".equals(args[i])) {
+ count = Integer.parseInt(args[++i]);
+ System.out.println("Sending "+count+" messages.");
+ } else if ("-pause".equals(args[i])) {
+ pause = Long.parseLong(args[++i])*1000;
+ } else if ("-break".equals(args[i])) {
+ breakOnEx = true;
+ } else if ("-shutdown".equals(args[i])) {
+ shutdown = true;
+ } else if ("-stats".equals(args[i])) {
+ stats = Integer.parseInt(args[++i]);
+ System.out.println("Stats every "+stats+" message");
+ } else if ("-sendoptions".equals(args[i])) {
+ channelOptions = Integer.parseInt(args[++i]);
+ System.out.println("Setting send options to "+channelOptions);
+ } else if ("-startoptions".equals(args[i])) {
+ startoptions = Integer.parseInt(args[++i]);
+ System.out.println("Setting start options to "+startoptions);
+ } else if ("-size".equals(args[i])) {
+ size = Integer.parseInt(args[++i])-4;
+ System.out.println("Message size will be:"+(size+4)+" bytes");
+ } else if ("-mode".equals(args[i])) {
+ if ( "receive".equals(args[++i]) ) send = false;
+ } else if ("-debug".equals(args[i])) {
+ debug = true;
+ } else if ("-help".equals(args[i]))
+ {
+ usage();
+ System.exit(1);
+ }
+ }
+
+ ManagedChannel channel = (ManagedChannel)ChannelCreator.createChannel(args);
+
+ LoadTest test = new LoadTest(channel,send,count,debug,pause,stats,breakOnEx);
+ test.channelOptions = channelOptions;
+ LoadMessage msg = new LoadMessage();
+
+ messageSize = LoadMessage.getMessageSize(msg);
+ channel.addChannelListener(test);
+ channel.addMembershipListener(test);
+ channel.start(startoptions);
+ Runtime.getRuntime().addShutdownHook(new Shutdown(channel));
+ while ( threads > 1 ) {
+ Thread t = new Thread(test);
+ t.setDaemon(true);
+ t.start();
+ threads--;
+ test = new LoadTest(channel,send,count,debug,pause,stats,breakOnEx);
+ test.channelOptions = channelOptions;
+ }
+ test.run();
+ if ( shutdown && send ) channel.stop(channel.DEFAULT);
+ System.out.println("System test complete, sleeping to let threads finish.");
+ Thread.sleep(60*1000*60);
+ }
+
+ public static class Shutdown extends Thread {
+ ManagedChannel channel = null;
+ public Shutdown(ManagedChannel channel) {
+ this.channel = channel;
+ }
+
+ public void run() {
+ System.out.println("Shutting down...");
+ SystemExit exit = new SystemExit(5000);
+ exit.setDaemon(true);
+ exit.start();
+ try {
+ channel.stop(channel.DEFAULT);
+
+ }catch ( Exception x ) {
+ x.printStackTrace();
+ }
+ System.out.println("Channel stopped.");
+ }
+ }
+ public static class SystemExit extends Thread {
+ private long delay;
+ public SystemExit(long delay) {
+ this.delay = delay;
+ }
+ public void run () {
+ try {
+ Thread.sleep(delay);
+ }catch ( Exception x ) {
+ x.printStackTrace();
+ }
+ System.exit(0);
+
+ }
+ }
+
+}
\ No newline at end of file
Added: tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/MapDemo.java
URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/MapDemo.java?view=auto&rev=442235
==============================================================================
--- tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/MapDemo.java (added)
+++ tomcat/tc6.0.x/trunk/test/org/apache/catalina/tribes/demos/MapDemo.java Mon Sep 11 07:47:47 2006
@@ -0,0 +1,415 @@
+package org.apache.catalina.tribes.demos;
+
+import java.io.Serializable;
+import java.util.Map;
+
+import java.awt.ComponentOrientation;
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.JTextField;
+import javax.swing.table.AbstractTableModel;
+import javax.swing.table.TableModel;
+
+import org.apache.catalina.tribes.Channel;
+import org.apache.catalina.tribes.ChannelListener;
+import org.apache.catalina.tribes.ManagedChannel;
+import org.apache.catalina.tribes.Member;
+import org.apache.catalina.tribes.MembershipListener;
+import org.apache.catalina.tribes.tipis.AbstractReplicatedMap;
+import org.apache.catalina.tribes.tipis.LazyReplicatedMap;
+import javax.swing.table.DefaultTableCellRenderer;
+import java.awt.Color;
+import java.awt.Component;
+import javax.swing.table.TableColumn;
+import org.apache.catalina.tribes.util.UUIDGenerator;
+import org.apache.catalina.tribes.util.Arrays;
+
+/**
+ * <p>Title: </p>
+ *
+ * <p>Description: </p>
+ *
+ * <p>Copyright: Copyright (c) 2005</p>
+ *
+ * <p>Company: </p>
+ *
+ * @author not attributable
+ * @version 1.0
+ */
+public class MapDemo implements ChannelListener, MembershipListener{
+
+ protected LazyReplicatedMap map;
+ protected SimpleTableDemo table;
+
+ public MapDemo(Channel channel, String mapName ) {
+ map = new LazyReplicatedMap(null,channel,5000, mapName,null);
+ table = SimpleTableDemo.createAndShowGUI(map,channel.getLocalMember(false).getName());
+ channel.addChannelListener(this);
+ channel.addMembershipListener(this);
+// for ( int i=0; i<1000; i++ ) {
+// map.put("MyKey-"+i,"My String Value-"+i);
+// }
+ this.messageReceived(null,null);
+ }
+
+ public boolean accept(Serializable msg, Member source) {
+ table.dataModel.getValueAt(-1,-1);
+ return false;
+ }
+
+ public void messageReceived(Serializable msg, Member source) {
+
+ }
+
+ public void memberAdded(Member member) {
+ }
+ public void memberDisappeared(Member member) {
+ table.dataModel.getValueAt(-1,-1);
+ }
+
+ public static void usage() {
+ System.out.println("Tribes MapDemo.");
+ System.out.println("Usage:\n\t" +
+ "java MapDemo [channel options] mapName\n\t" +
+ "\tChannel options:" +
+ ChannelCreator.usage());
+ }
+
+ public static void main(String[] args) throws Exception {
+ long start = System.currentTimeMillis();
+ ManagedChannel channel = (ManagedChannel) ChannelCreator.createChannel(args);
+ String mapName = "MapDemo";
+ if ( args.length > 0 && (!args[args.length-1].startsWith("-"))) {
+ mapName = args[args.length-1];
+ }
+ channel.start(channel.DEFAULT);
+ Runtime.getRuntime().addShutdownHook(new Shutdown(channel));
+ MapDemo demo = new MapDemo(channel,mapName);
+
+ System.out.println("System test complete, time to start="+(System.currentTimeMillis()-start)+" ms. Sleeping to let threads finish.");
+ Thread.sleep(60 * 1000 * 60);
+ }
+
+ public static class Shutdown
+ extends Thread {
+ ManagedChannel channel = null;
+ public Shutdown(ManagedChannel channel) {
+ this.channel = channel;
+ }
+
+ public void run() {
+ System.out.println("Shutting down...");
+ SystemExit exit = new SystemExit(5000);
+ exit.setDaemon(true);
+ exit.start();
+ try {
+ channel.stop(channel.DEFAULT);
+
+ } catch (Exception x) {
+ x.printStackTrace();
+ }
+ System.out.println("Channel stopped.");
+ }
+ }
+
+ public static class SystemExit
+ extends Thread {
+ private long delay;
+ public SystemExit(long delay) {
+ this.delay = delay;
+ }
+
+ public void run() {
+ try {
+ Thread.sleep(delay);
+ } catch (Exception x) {
+ x.printStackTrace();
+ }
+ System.exit(0);
+
+ }
+ }
+
+ public static class SimpleTableDemo
+ extends JPanel implements ActionListener{
+ private static int WIDTH = 550;
+
+ private LazyReplicatedMap map;
+ private boolean DEBUG = false;
+ AbstractTableModel dataModel = new AbstractTableModel() {
+
+
+ String[] columnNames = {
+ "Key",
+ "Value",
+ "Backup Node",
+ "isPrimary",
+ "isProxy",
+ "isBackup"};
+
+ public int getColumnCount() { return columnNames.length; }
+
+ public int getRowCount() {return map.sizeFull() +1; }
+
+ public StringBuffer getMemberNames(Member[] members){
+ StringBuffer buf = new StringBuffer();
+ if ( members!=null ) {
+ for (int i=0;i<members.length; i++ ) {
+ buf.append(members[i].getName());
+ buf.append("; ");
+ }
+ }
+ return buf;
+ }
+
+ public Object getValueAt(int row, int col) {
+ if ( row==-1 ) {
+ update();
+ return "";
+ }
+ if ( row == 0 ) return columnNames[col];
+ Object[] entries = map.entrySetFull().toArray();
+ Map.Entry e = (Map.Entry)entries [row-1];
+ LazyReplicatedMap.MapEntry entry = (LazyReplicatedMap.MapEntry)e.getValue();
+ switch (col) {
+ case 0: return entry.getKey();
+ case 1: return entry.getValue();
+ case 2: return getMemberNames(entry.getBackupNodes());
+ case 3: return new Boolean(entry.isPrimary());
+ case 4: return new Boolean(entry.isProxy());
+ case 5: return new Boolean(entry.isBackup());
+ default: return "";
+ }
+
+ }
+
+ public void update() {
+ fireTableDataChanged();
+ }
+ };
+
+ JTextField txtAddKey = new JTextField(20);
+ JTextField txtAddValue = new JTextField(20);
+ JTextField txtRemoveKey = new JTextField(20);
+ JTextField txtChangeKey = new JTextField(20);
+ JTextField txtChangeValue = new JTextField(20);
+
+ JTable table = null;
+ public SimpleTableDemo(LazyReplicatedMap map) {
+ super();
+ this.map = map;
+
+ this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
+
+ //final JTable table = new JTable(data, columnNames);
+ table = new JTable(dataModel);
+
+ table.setPreferredScrollableViewportSize(new Dimension(WIDTH, 150));
+ for ( int i=0; i<table.getColumnCount(); i++ ) {
+ TableColumn tm = table.getColumnModel().getColumn(i);
+ tm.setCellRenderer(new ColorRenderer());
+ }
+
+
+ if (DEBUG) {
+ table.addMouseListener(new MouseAdapter() {
+ public void mouseClicked(MouseEvent e) {
+ printDebugData(table);
+ }
+ });
+ }
+
+ //setLayout(new GridLayout(5, 0));
+ setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
+
+ //Create the scroll pane and add the table to it.
+ JScrollPane scrollPane = new JScrollPane(table);
+
+ //Add the scroll pane to this panel.
+ add(scrollPane);
+
+ //create a add value button
+ JPanel addpanel = new JPanel();
+ addpanel.setPreferredSize(new Dimension(WIDTH,30));
+ addpanel.add(createButton("Add","add"));
+ addpanel.add(txtAddKey);
+ addpanel.add(txtAddValue);
+ addpanel.setMaximumSize(new Dimension(WIDTH,30));
+ add(addpanel);
+
+ //create a remove value button
+ JPanel removepanel = new JPanel( );
+ removepanel.setPreferredSize(new Dimension(WIDTH,30));
+ removepanel.add(createButton("Remove","remove"));
+ removepanel.add(txtRemoveKey);
+ removepanel.setMaximumSize(new Dimension(WIDTH,30));
+ add(removepanel);
+
+ //create a change value button
+ JPanel changepanel = new JPanel( );
+ changepanel.add(createButton("Change","change"));
+ changepanel.add(txtChangeKey);
+ changepanel.add(txtChangeValue);
+ changepanel.setPreferredSize(new Dimension(WIDTH,30));
+ changepanel.setMaximumSize(new Dimension(WIDTH,30));
+ add(changepanel);
+
+
+ //create sync button
+ JPanel syncpanel = new JPanel( );
+ syncpanel.add(createButton("Synchronize","sync"));
+ syncpanel.add(createButton("Replicate","replicate"));
+ syncpanel.add(createButton("Random","random"));
+ syncpanel.setPreferredSize(new Dimension(WIDTH,30));
+ syncpanel.setMaximumSize(new Dimension(WIDTH,30));
+ add(syncpanel);
+
+
+ }
+
+ public JButton createButton(String text, String command) {
+ JButton button = new JButton(text);
+ button.setActionCommand(command);
+ button.addActionListener(this);
+ return button;
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ System.out.println(e.getActionCommand());
+ if ( "add".equals(e.getActionCommand()) ) {
+ System.out.println("Add key:"+txtAddKey.getText()+" value:"+txtAddValue.getText());
+ map.put(txtAddKey.getText(),new StringBuffer(txtAddValue.getText()));
+ }
+ if ( "change".equals(e.getActionCommand()) ) {
+ System.out.println("Change key:"+txtChangeKey.getText()+" value:"+txtChangeValue.getText());
+ StringBuffer buf = (StringBuffer)map.get(txtChangeKey.getText());
+ if ( buf!=null ) {
+ buf.delete(0,buf.length());
+ buf.append(txtChangeValue.getText());
+ map.replicate(txtChangeKey.getText(),true);
+ } else {
+ buf = new StringBuffer();
+ buf.append(txtChangeValue.getText());
+ map.put(txtChangeKey.getText(),buf);
+ }
+ }
+ if ( "remove".equals(e.getActionCommand()) ) {
+ System.out.println("Remove key:"+txtRemoveKey.getText());
+ map.remove(txtRemoveKey.getText());
+ }
+ if ( "sync".equals(e.getActionCommand()) ) {
+ System.out.println("Syncing from another node.");
+ map.transferState();
+ }
+ if ( "random".equals(e.getActionCommand()) ) {
+ Thread t = new Thread() {
+ public void run() {
+ for (int i = 0; i < 100; i++) {
+ String key = Arrays.toString(UUIDGenerator.randomUUID(false));
+ map.put(key, key);
+ dataModel.fireTableDataChanged();
+ table.paint(table.getGraphics());
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException x) {
+ Thread.currentThread().interrupted();
+ }
+ }
+ }
+ };
+ t.start();
+ }
+
+ if ( "replicate".equals(e.getActionCommand()) ) {
+ System.out.println("Replicating out to the other nodes.");
+ map.replicate(true);
+ }
+ dataModel.getValueAt(-1,-1);
+ }
+
+ private void printDebugData(JTable table) {
+ int numRows = table.getRowCount();
+ int numCols = table.getColumnCount();
+ javax.swing.table.TableModel model = table.getModel();
+
+ System.out.println("Value of data: ");
+ for (int i = 0; i < numRows; i++) {
+ System.out.print(" row " + i + ":");
+ for (int j = 0; j < numCols; j++) {
+ System.out.print(" " + model.getValueAt(i, j));
+ }
+ System.out.println();
+ }
+ System.out.println("--------------------------");
+ }
+
+ /**
+ * Create the GUI and show it. For thread safety,
+ * this method should be invoked from the
+ * event-dispatching thread.
+ */
+ public static SimpleTableDemo createAndShowGUI(LazyReplicatedMap map, String title) {
+ //Make sure we have nice window decorations.
+ JFrame.setDefaultLookAndFeelDecorated(true);
+
+ //Create and set up the window.
+ JFrame frame = new JFrame("SimpleTableDemo - "+title);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ //Create and set up the content pane.
+ SimpleTableDemo newContentPane = new SimpleTableDemo(map);
+ newContentPane.setOpaque(true); //content panes must be opaque
+ frame.setContentPane(newContentPane);
+
+ //Display the window.
+ frame.setSize(450,250);
+ newContentPane.setSize(450,300);
+ frame.pack();
+ frame.setVisible(true);
+ return newContentPane;
+ }
+ }
+
+ static class ColorRenderer extends DefaultTableCellRenderer {
+
+ public ColorRenderer() {
+ super();
+ }
+
+ public Component getTableCellRendererComponent
+ (JTable table, Object value, boolean isSelected,
+ boolean hasFocus, int row, int column) {
+ Component cell = super.getTableCellRendererComponent
+ (table, value, isSelected, hasFocus, row, column);
+ cell.setBackground(Color.WHITE);
+ if ( row > 0 ) {
+ Color color = null;
+ boolean primary = ( (Boolean) table.getValueAt(row, 3)).booleanValue();
+ boolean proxy = ( (Boolean) table.getValueAt(row, 4)).booleanValue();
+ boolean backup = ( (Boolean) table.getValueAt(row, 5)).booleanValue();
+ if (primary) color = Color.GREEN;
+ else if (proxy) color = Color.RED;
+ else if (backup) color = Color.BLUE;
+ if ( color != null ) cell.setBackground(color);
+ }
+// System.out.println("Row:"+row+" Column:"+column+" Color:"+cell.getBackground());
+// cell.setBackground(bkgndColor);
+// cell.setForeground(fgndColor);
+
+ return cell;
+ }
+
+
+ }
+
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org