Solved

multi thread data collection program

Posted on 2006-11-02
1
329 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 82
topping3 challenge 14 79
GUI builder for Eclipse? 8 35
Error trying to install RTMT Win7 5 59
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

813 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

13 Experts available now in Live!

Get 1:1 Help Now