1 /*------------------------------------------------------------------------------
  2  Name:      MultiThreadPublisher.java
  3  Project:   xmlBlaster.org
  4  Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5  ------------------------------------------------------------------------------*/
  6 
  7 package javaclients;
  8 
  9 import java.io.BufferedReader;
 10 import java.io.FileReader;
 11 import java.io.IOException;
 12 import java.util.ArrayList;
 13 import java.util.StringTokenizer;
 14 
 15 import org.xmlBlaster.util.Global;
 16 
 17 /**
 18  * MultiThreadSequencer
 19  * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
 20  */
 21 public class MultiThreadSequencer extends Thread {
 22 
 23    public static int COUNT = 0;
 24    private boolean isPublish;
 25    private long initialSleep;
 26    private Global global;
 27    
 28    public MultiThreadSequencer(boolean isPublish, long initialSleep, String[] args) {
 29       super();
 30       this.global = new Global();
 31       this.global.init(args);
 32       this.initialSleep = initialSleep;
 33       this.isPublish = isPublish;
 34    }
 35 
 36    public void process() {
 37       try {
 38          Thread.sleep(this.initialSleep);
 39          start(); // here a new thread is started
 40       }
 41       catch (Exception ex) {
 42          ex.printStackTrace();
 43       }
 44    }
 45    
 46    public void run() {
 47       if (this.isPublish)
 48          new HelloWorldPublish(this.global);
 49       else 
 50          new HelloWorldSubscribe(this.global);
 51       
 52       synchronized(MultiThreadSequencer.class) {
 53          COUNT++;
 54       }
 55    }
 56    
 57    public static String[] parseInitialLine(String line) {
 58       String[] ret = new String[3];
 59       int pos = line.indexOf(' ');
 60       if (pos > 0) {
 61          ret[0] = line.substring(0, pos);
 62          line = line.substring(pos+1);
 63          pos = line.indexOf(' ');
 64          if (pos > 0) {
 65             ret[1] = line.substring(0, pos);
 66             ret[2] = line.substring(pos+1);
 67          }
 68       }
 69       return ret;
 70    }
 71    
 72    public static String[] getLineAsArgs(String line) {
 73       StringTokenizer tokenizer = new StringTokenizer(line, " ");
 74       int nmax = tokenizer.countTokens();
 75       String[] ret = new String[nmax];
 76       int i = 0;
 77       while (tokenizer.hasMoreTokens()) {
 78          ret[i] = tokenizer.nextToken();
 79          i++;
 80       }
 81       return ret;
 82    }
 83    
 84    public static MultiThreadSequencer[] createPublishers(String filename) throws IOException {
 85       BufferedReader reader = new BufferedReader(new FileReader(filename));
 86       String line = null;
 87       ArrayList list = new ArrayList();
 88       while ( (line = reader.readLine()) != null) {
 89          line = line.trim();
 90          if (line.length() == 0)
 91             continue;
 92          String[] tmpLines = parseInitialLine(line);
 93          boolean isPublish = true;
 94          if ("SUB".equalsIgnoreCase(tmpLines[0]))
 95             isPublish = false;
 96          long initialSleep = Long.parseLong(tmpLines[1]);
 97          MultiThreadSequencer publisher = new MultiThreadSequencer(isPublish, initialSleep, getLineAsArgs(tmpLines[2]));
 98          list.add(publisher);
 99       }
100       return (MultiThreadSequencer[])list.toArray(new MultiThreadSequencer[list.size()]);
101    }
102 
103    
104    public static void main(String[] args) {
105       if (args.length != 1) {
106          System.err.println("usage: java javaclients.MultiThreadSequencer filename");
107          System.err.println("where the format of the file is 'PUB/SUB initialDelayInMillis args[]");
108          String[] tmpArgs = new String[] { "-help" };
109          System.err.println("where the parameters passed as args are:");
110          HelloWorldPublish.main(tmpArgs);
111          System.err.println("or where the parameters passed as args are:");
112          HelloWorldSubscribe.main(tmpArgs);
113          System.exit(-1);
114       }
115       try {
116          MultiThreadSequencer[] pubs = MultiThreadSequencer.createPublishers(args[0]);
117          for (int i=0; i < pubs.length; i++) {
118             pubs[i].process();
119          }
120          while (MultiThreadSequencer.COUNT < pubs.length) {
121             Thread.sleep(500L);
122          }
123          System.err.println("Publishing is finished: CTRL-C to terminate");
124       }
125       catch (Exception ex) {
126          ex.printStackTrace();
127       }
128       
129       
130    }
131    
132    
133 }


syntax highlighted by Code2HTML, v. 0.9.1