Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

multi thread data collection program

Posted on 2006-11-02
1
Medium Priority
?
335 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

650 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