Solved

multi thread data collection program

Posted on 2006-11-02
1
328 Views
Last Modified: 2008-02-01
I have a program that I run to collect data. It works by creating a new thread for each computer in a text file. It then uses the Ps tool PSEXEC to run a process (written by someone else) on a remote computer. The data created by the process is save on my local machine. The java program watches the local machine destination drive to see when the data is there. That way it kowns when it is all done.

My problem is that i can't get the java program to work with more than 8 machines. I need it to eventually work for hundreds of machines. I've tried increasing the heap size but that doesn't work. I'm not sure where the limitation is.


I'll attach the program at the bottom even though it is large and requires a lot of explanation

/*******************************************************************************************/
import java.applet.      *;
import java.awt.*;
import java.io.*;
import java.net.InetAddress;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.*;

class DataCollector extends Thread
      {
            static public boolean ready;

            public static void main (String[] args) {

            ready = false;





                  class SimpleThread extends Thread {

                           String computer;
                           String path;
                           String Error="";
                           long start_time;
                           int timeout = 900000; //15 minutes

                           public SimpleThread(String aComp, String aPath)
                            {
                                    computer = aComp;
                                    path = aPath;
                                    start_time= System.currentTimeMillis();
                                    System.out.println(computer + "<- started");
                            }
                            public void run()
                            {
                                    boolean notdone = true;
                                    //************ IS ONLINE ? *******************//
                                    int timeoutping = 2000;
                                    try{
                                    InetAddress host = InetAddress.getByName(computer);
                                    if (!(host.isReachable(timeoutping))){
                                          notdone = false;
                                          Error = "Communication Error";
                                    }
                                    }catch (Exception e){notdone=false; Error= "Host not found by Name";}
                                    //************ Clear Old PSExec Versions here ***********//
                                    Process p1;
                                    try{
                                          p1  = Runtime.getRuntime().exec("sc \\\\" + computer + " stop psexecsvc");
                                          p1.waitFor();
                                    }catch(Exception e){}
                                    try{
                                          p1  = Runtime.getRuntime().exec("del \\\\" + computer + "\\admin$\\psexesvc.exe");
                                          p1.waitFor();
                                    }catch(Exception e){}
                                    try{
                                          p1  = Runtime.getRuntime().exec("del \\\\" + computer + "\\admin$\\system32\\psexesvc.exe");
                                          p1.waitFor();
                                    }catch(Exception e){}
                                    try{
                                          p1  = Runtime.getRuntime().exec("sc \\\\" + computer + " delete psexesvc");
                                          p1.waitFor();
                                    }catch(Exception e){}
                                    //************ Clear IBMDSA ***********//
                                    Process p3;
                                    int exitVal=0;
                                    try{
                                          do{
                                                try
                                                {
                                                      Runtime rt = Runtime.getRuntime();
                                                      Process proc = rt.exec("pskill -t  \\\\" + computer + " -u username -p password ibmdsa2");
                                                      InputStream stderr = proc.getErrorStream();
                                                      InputStreamReader isr = new InputStreamReader(stderr);
                                                      BufferedReader br = new BufferedReader(isr);
                                                      String line = null;
                                                      while ( (line = br.readLine()) != null){} //use output before hang
                                                      exitVal = proc.waitFor();
                                                } catch (Throwable t)
                                                  {
                                                      t.printStackTrace();
                                                  }



                                          }while(exitVal == 0);
                                    }catch(Exception e){ Error="Possilbe problem; could not kill process: " + e; System.out.println(e);}

                                    //*************Remote Execute the program to each computer
                                    try{
                                          String line;
                                          Process p2  = Runtime.getRuntime().exec("psexec \\\\" + computer + " -u ent\\username -p password -c -v ibmdsa2.exe -c -d \"" + path + computer + "\"");
                                        } catch (Exception e){Error="Could not initiate process"; notdone = false; System.out.println("exception: " + e);}



                                    //******************* WAIT FOR PROCESS TO END ****************************//
                                    //* Asume it is done when .gz file is done being written too             *//
                                    //************************************************************************//
                                    while(notdone)
                                    {
                                          File dir = new File(path + computer);
                                          String[] children = dir.list();
                                          if (children == null) {
                                                // Either dir does not exist or is not a directory
                                          } else {
                                                for (int i=0; i<children.length; i++) {
                                                      // Get filename of file or directory
                                                      String filename = children[i];
                                                }
                                          }


                                          // The list of files can also be retrieved as File objects
                                          File[] files = dir.listFiles();

                                          // This filter only returns directories
                                          FileFilter fileFilter = new FileFilter() {
                                                public boolean accept(File file) {
                                                      return file.getName().substring(file.getName().length() -3).equals(".gz");
                                                }
                                          };
                                          files = dir.listFiles(fileFilter);



                                          boolean isTimeout = (System.currentTimeMillis() > (start_time + timeout));

                                          if(!(files.length == 0))
                                                if(files[0].canRead()){
                                                      notdone = false;
                                                }

                                          if (isTimeout){
                                                notdone = false;
                                                Error = "Time out";
                                          }
                                    try{
                                    Thread.sleep ( 2000 );
                                    }catch(Exception e)
                                    {System.out.println(e);}
                                    }
                                    //************ END IBMDSA2 PROCESS ***********//
                                    try{
                                          Process p4;
                                          p4  = Runtime.getRuntime().exec("pskill -t  \\\\" + computer + " -u username -p password ibmdsa2");
                                          p4.waitFor();
                                    }catch(Exception e){ Error="Possilbe problem; could not kill process: " + e;}

                                    while (!(ready)){}
                                    if (!(Error.equals("")))
                                          System.out.println(computer + ": " + Error);

                                    System.out.println(computer + " done");

                              }

                        }





            System.out.println("Initiating Program: Dispatching Listener Threads");

            String error="";
            String path = "\\\\ulcaccamo\\d$\\dsa_results\\";
            List<String> computers = new ArrayList<String>();

            int index=0;

            try{
                  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("complist.txt")));
                  String line = "";
                  while((line = br.readLine()) != null) {
                  computers.add(line.trim());
            }
            }catch(Exception e){}

            while(index < computers.size() && computers.get(index) != "")
            {
                  boolean success = (new File(path + computers.get(index))).mkdirs();
                  if (!success) {
                  //write failure to log file
                  }

                  //DELETE GZ Files to ensure that there is no conflicts
                  File files = new File(path + computers.get(index));
                  String[] children = files.list();
                  for (int i=0; i<children.length; i++) {
            // Get filename of file or directory
                  if (children[i].substring(children[i].length() -3).equals(".gz"))
                        success = (new File(path + computers.get(index)+ "\\" + children[i])).delete();
                  }


                  //Listen for computer to finish its process
                  Thread b = new SimpleThread(computers.get(index), path);
                  //b.setPriority(Thread.MIN_PRIORITY);
                  b.start();
                  index+=1;
            }
            Calendar cal = new GregorianCalendar();
            int hour12 = cal.get(Calendar.HOUR);            // 0..11
            int min = 16 + cal.get(Calendar.MINUTE);             // 0..59
            if (min > 59) { min = min - 60; hour12 +=1;}
            if(hour12 > 12) hour12 = hour12 - 12;

            System.out.println("Dispatched Listeners...");

            System.out.println("Expected End Time: "+ hour12 + ":" + min);
            System.out.println("");
            System.out.println("Erroneous Computer List");
            System.out.println("------------------------");
            ready = true;
      }





}
0
Comment
Question by:bbcac
1 Comment
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 17864862
    Hi!

Take a look at this discussion : http://forum.java.sun.com/thread.jspa?threadID=645335&messageID=4346478
I suggest you also use threadpool : http://www.java2s.com/Code/Java/Threads/Thread-Pool.htm
for Thread reuse.
What error do you get when increasing the number of computernames in complist.txt to more than 8 computers ?

Regards,
  Tomas Helgi  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 49
Cipher Configuration on Apache HTTPD 4 53
Spring Framework HTTPSession management 1 24
expectj telnet failing 5 26
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now