?
Solved

multi thread data collection program

Posted on 2006-11-02
1
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 1000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 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…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 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