Relate Thread and resultSet
I know how to make basic threads, and I know how to access to MySQL and I've found the way (at least I think so) to make each thread execute a Linux command. But because of my basic level of Java I don't know how to make all that code work together.
What I want to do is:
1.- get from a DB the number of servers (there's a field called 'IP' to be checked) that I must ping to see if that IP is working.
2.- create a thread for each one of those IP's
3.- execute a Linux command with each thread
If anyone could guide me or tell me about a similar script already made I would thank you for it.
Greetings from southern Spain.
Why drive a shell script with a Java Program
hi
I do not want to distract you from seeking an answer to your specific question, but i just wanted to ask you the following:
(i) Why are you writing Java code to call a shell script?
(ii) If you HAVE TO write this driver from Java why does the pinging of the IP addresses have to be done by threads?
Writing Perl code or Python code or a shell script to call your other shell script is in my humble opinion what should be done.
If on the other hand this is an academic exercise, then you can ignore my post entirely. :cool:
...and i will look at your question again to give you a Java answer.
regards
beez
Technical elitism is a barrier to progress
Hi Juan
I am an advocate of Java...and i hate technical elitism.
Here is the code:
package com.psg.pinger.core;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.io.IOException;
/**
* @author kshAdministrator
*
* This class represents a remote host in terms of IP.
* It enables one to kick start it once created in order
*
*/
public class RemoteHost implements Runnable {
private static final String IPADDRESS_DELIMITER=".";
private static final int IP4_EXPECTED_COUNT=4;
private static final int IP6_EXPECTED_COUNT=6;
private static final int PING_TIMEOUT_MILLISEC=5000;
//Java InetAdress class being used to ping the addresses
private InetAddress hostInetAddr;
private byte[] addressArr=new byte[IP4_EXPECTED_COUNT];
private StringTokenizer st;
private String IPAddress;
public RemoteHost(String inputIPAddress) {
IPAddress=inputIPAddress;
assert(IPAddress!=null);
st=new StringTokenizer(IPAddress, IPADDRESS_DELIMITER);
assert( (st.countTokens()== IP4_EXPECTED_COUNT) || (st.countTokens()==IP6_EXPECTED_COUNT));
//Now populate the byteArray
for(int i=0, tmpInt=0; st.hasMoreTokens();i++) {
tmpInt=Integer.parseInt(st.nextToken());
addressArr[i]=(byte)tmpInt;
//START Logging
System.out.println("Echoing the byte array:");
System.out.println(addressArr[i]);
//END Logging
}
//Now instantiate the InetAddress
try {
/*
* NB: the following getByAddress will fail if the byte array
* is set to IP6_EXPECTED_COUNT
* Therefore we have to configure it to call the correct constructor
* based on IP4 or IP6
*/
hostInetAddr=InetAddress.getByAddress(addressArr);
} catch (UnknownHostException uhe) {
System.out.println("Exception::InetAddress instantiation failed");
uhe.printStackTrace();
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
//Now try to ping it with the equivalent method of the InetAddress instance
try {
System.out.println("About to ping:["+IPAddress+"].This _might_ block for:"+PING_TIMEOUT_MILLISEC+" milli seconds...");
if(hostInetAddr.isReachable(PING_TIMEOUT_MILLISEC) ) {
System.out.println("Success in reaching host at ["+IPAddress+"]");
} else {
System.out.println("Unreacheable host at ["+IPAddress+"]");
}
} catch (IOException ioe) {
System.out.println("Exception::destination unreachable due to exception");
ioe.printStackTrace();
}
}
public static void main(String[] args) {
//RemoteHost rh=new RemoteHost("124.22.11.22");
RemoteHost rh=new RemoteHost("192.168.1.2");
Thread[] rhhreadArr=new Thread[10];
RemoteHost rh0=new RemoteHost("191.168.1.2");
RemoteHost rh1=new RemoteHost("198.168.1.2");
RemoteHost rh2=new RemoteHost("197.168.1.2");
RemoteHost rh3=new RemoteHost("191.168.1.2");
RemoteHost rh4=new RemoteHost("190.168.1.2");
rhhreadArr[0]=new Thread(rh0);
rhhreadArr[1]=new Thread(rh1);
rhhreadArr[2]=new Thread(rh2);
rhhreadArr[3]=new Thread(rh3);
rhhreadArr[4]=new Thread(rh4);
rhhreadArr[0].start();
rhhreadArr[1].start();
rhhreadArr[2].start();
rhhreadArr[3].start();
rhhreadArr[4].start();
}
}