Solved

multi thread data collection program

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

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
Comment Utility
    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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 81
IT Company 5 67
stringclean challenge 26 54
micro services spring boot application error 3 16
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

763 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

15 Experts available now in Live!

Get 1:1 Help Now