Results 1 to 15 of 15
Thread: Relate Thread and resultSet
- 01-17-2009, 03:15 PM #1
Member
- Join Date
- Jan 2009
- Location
- Spain (European Union)
- Posts
- 7
- Rep Power
- 0
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.
- 01-21-2009, 04:30 PM #2
In your main thread, read the list of IP's.
Create a thread for each IP, using a separate instance of whatever class you use to do the check. Creating separate instances avoids conflicts between the threads.
In each thread,
Create an InetAddress for each IP.
Use isReachable() to see if the IP is available. isReachable uses the same underlying methods as the ping command, and you don't have to shell out to use it. I assume you are on 1.5 or higher.
- 01-26-2009, 05:02 PM #3
Member
- Join Date
- Jan 2009
- Location
- Spain (European Union)
- Posts
- 7
- Rep Power
- 0
Hello,
thank you very much for the information. I found it useful.
For you not to have to "imagine" how my application works I'll copy here the code as I have it right now. As you'll see it's very simple as my knowledge on Java is low.
First I try and connect to a MySQL database:
TheDB.java
But my problem is, how do I make it work with this new class?Java Code:import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.*; public class TheDB { private Connection con; private Statement st; private String query = "SELECT ip FROM equipments WHERE monitorize='1'"; private String count = "SELECT COUNT(ip) FROM equipments WHERE monitorize='1'"; /* I NEED TO KNOW HOW MANY EQUIPMENTS I SHOULD MONITORIZE AND THE IP FOR EACH ONE BECAUSE LATER I HAVE TO PING THEM (or use isReachable() as you recommended me.) */ //Constructor public theDB(){ // load driver try{ Class contr = Class.forName("com.mysql.jdbc.Driver"); System.out.println(contr.toString()); }catch (ClassNotFoundException cnfe){ System.out.println("com.mysql.jdbc.Driver"); } //Conecting to DB try{ Connection micon = DriverManager.getConnection("jdbc:mysql://localhost/Comerce","theUser","thePassword"); //Create Statement object Statement st = micon.createStatement(); this.con = micon; this.st = st; }catch(SQLException sqle){ System.out.println("Error: couldn't connect"); } } public ResultSet selecting (String query) throws SQLException{ return this.st.executeQuery(query); } public ResultSet theSize (String count) throws SQLException{ return this.st.executeQuery(count); } //Getting the ResultSet public ResultSet getResultSet() throws SQLException{ return this.st.getResultSet(); } //Getting the connector public Connection getCon(){ return this.con; } }
TheCommand.java
I would appreciate any help or guiding light anyone could give me.Java Code:import java.io.*; public class TheCommand { public static void main(String[] args) { try { String[] command = {"sh","-c","/api/utils/fping_checking <<ip>>"}; // CHANGE <<ip>> FOR THE IP WE GOT IN TheDB.java. But, HOW??!! final Process process = Runtime.getRuntime().exec(command); new Thread() { public void run() { try{ InputStream is = process.getInputStream(); byte[] buffer = new byte[1024]; for(int count = 0; (count = is.read(buffer)) >= 0;) { System.out.write(buffer, 0, count); } } catch(Exception e) { e.printStackTrace(); } } }.start(); new Thread() { public void run() { try{ InputStream is = process.getErrorStream(); byte[] buffer = new byte[1024]; for(int count = 0; (count = is.read(buffer)) >= 0;) { System.err.write(buffer, 0, count); } } catch(Exception e) { e.printStackTrace(); } } }.start(); int returnCode = process.waitFor(); System.out.println("Return code = " + returnCode); } catch (Exception e) { e.printStackTrace(); } } }
Thank you in advance.
- 02-03-2009, 10:59 AM #4
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
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
- 02-03-2009, 10:53 PM #5
Member
- Join Date
- Jan 2009
- Location
- Spain (European Union)
- Posts
- 7
- Rep Power
- 0
Hello,
the answer to all that is simple: my boss asked me if that was possible with Java. Because he didn't want to use Perl in this case. He does not know Java at all and I only know a bit, that I've learnt by myself. I just wanted to give him the solution instead of saying "I can't do it. Waste your own time writing the script in Perl..."
The thread thing was because he wanted the application to do something if any of the pings failed. Though I thought that the isReachable() method could be a good solution for that.
The idea was: get the list of IP's from a MySQL database, then use the list of IP's to ping them one by one. If any ping failed then there should be a different procedure to be done.
I hope you understood my explanation. Thank you for your interest.
- 02-04-2009, 03:42 PM #6
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
In that case Steve is absolutely correct with regard to his solution.
I wrote the code myself based on his guidelines and it works fine.
- 02-04-2009, 10:39 PM #7
Member
- Join Date
- Jan 2009
- Location
- Spain (European Union)
- Posts
- 7
- Rep Power
- 0
Could you copy and paste the code of all files here or attach them? I would appreciate that.
- 02-05-2009, 08:09 AM #8
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
i am sure you would "appreciate that" but it took me the better part of half a day to code, and i charge 550 Euros per day for my services. Sorry dude, but in this world nothing is for free.
- 02-05-2009, 10:05 AM #9
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Sorry if the previous posting was harsh...i am now going to fitup a Swing front end to the code i wrote, so that you can choose from a list of IP addresses, then press PingHost...and in 9 available windows you will see the result of the ping.
If i have time i will then plug-in your database code so that the IP address list gets populated from it.
Then i will make it so that you can configure the ping timeout.
If i go mental i will make the output windows dynamic, so as add IP addresses to ping, so you will get a new output window.
Finally, i will make it so that you can run shell script commands.
I love coding man....i mean yesterday, i just thought to myself, lets have a look and see if Steve's solution will work, and i learned a heck of a lot...especially about unsigned numbers in Java.
- 02-05-2009, 11:25 PM #10
Member
- Join Date
- Jan 2009
- Location
- Spain (European Union)
- Posts
- 7
- Rep Power
- 0
I thought it was something simpler than that. But your code seems to be very detailed, so I understand you said what you said before.
I'm not upset. It's your work and you're totally free to do what you want with it.
I just asked for the code of what I thought it was a "not very sofisticated" application. But maybe it was more complicated than I thought, or maybe it could be done easier, but the right way might be as detailed as you did.
You seem to be a proffesional programmer and you say it took you hours to get the application work. Well, in that case it could take me days to do it by myself, and weeks more before that to learn what I need to know about Java so that I could make app's like that.
Believe me when I say I thought it was easier and the problem was my low knowledge on Java.
Best wishes,
Juan
- 02-06-2009, 06:43 AM #11
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
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();
}
}
- 02-06-2009, 07:18 AM #12
Member
- Join Date
- Jan 2009
- Location
- Spain (European Union)
- Posts
- 7
- Rep Power
- 0
When I saw: package com.psg.pinger.core; I thought you did it with NetBeans and I use Eclipse. But fortunatelly it worked perfectly for me.
Now I can say it could have taken me weeks, even months to write such a code. I want to congratulate you on your skills.
Obviously I do thank you for the code. You really thought about almost all details and exceptions I might imagine.
Well, now I have to do the easy part of the application: relate the resultSet to your class RemoteHost so that I get the IP's from a MYSQL database.
I hope I can do it :-)
Again thank you very much for your time. I'll be in doubt with you.
Greetings from Spain,
Juan
- 02-07-2009, 10:16 AM #13
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Thanks for your generous remarks.
To be honest i have not taken care of all cases...however it is a good start.
Good luck with connecting your code to the code provided.
adios muchacho ;=)
kambiz
- 02-13-2009, 08:56 PM #14
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Juan
You should know that the code is not 100% reliable.
I tested it on XP and if there is no internet connection (i am guessing it is the availability of DNS) then EVERY IP address comes back as having been reached.
This has something to do with the mapping of Java to an underlying implementation. Unfortunately there is no way to know if the implementation i guess the DNS is available or not at the moment.
- 02-15-2009, 09:12 PM #15
Member
- Join Date
- Feb 2009
- Posts
- 10
- Rep Power
- 0
I think you can check the return message of shell you call through Java.
View, validate and edit X9.37, X9.100-180 and UCD ICL files.
http://www.digertech.com
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
passing a value from parent thread to child thread
By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks