dcsimg
Please can some help me with the THREADING issue.-Urgent Please.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ajay_luthria
Posted On:   Friday, March 15, 2002 02:05 AM

Hello All I may have posted this same question multiple times as I havent received any response as yet.I apologize for this I have been assigned the following task 1.Connect to a Servlet. 2.Wait for a response for 30 seconds.If no response recieved within 30 seconds,return 'Connection timed out' else display response Attached is the Java code for this I am no sure regarding the timer routine as a TIMER() class already exits in java.util.* please some one guide me ajayluthria@hotmail.cm public class HttpHandler { private static String sURL="localhost"; static String sMessage="Hello Server..Client sen   More>>

Hello All

I may have posted this same question multiple times as I havent received any response as yet.I apologize for this

I have been assigned the following task

1.Connect to a Servlet.

2.Wait for a response for 30 seconds.If no response recieved within 30 seconds,return 'Connection timed out' else display response

Attached is the Java code for this

I am no sure regarding the timer routine as a TIMER() class already exits in java.util.*
please some one guide me

ajayluthria@hotmail.cm

			
public class HttpHandler
{

private static String sURL="localhost";
static String sMessage="Hello Server..Client sending Data";
static HttpURLConnection hpCon=null;
static DataInputStream dis =null;

public static void main(String[] args)
{
sendData(sMessage);
}

public static void sendData(String sMess)
{
String response=null;
try{
URL url=null;
String uri = "http://" + sURL + ":8080/servlet/threads.Recieve_Http_Data1";
url = new URL(uri);
hpCon=null;

hpCon = (HttpURLConnection)url.openConnection();
hpCon.setDoOutput(true);
hpCon.setDoInput(true);

DataOutputStream dos = new DataOutputStream(hpCon.getOutputStream());
dos.writeUTF(sMess);

//new Timer(); // This is predefined class in util

dis = new DataInputStream(hpCon.getInputStream());

try {
response = dis.readUTF();
System.out.println("SERVER RESPONDING : " + response);
}finally
{ dos.close();
dis.close();
}


}catch(IOException e)
{System.out.println("Error in Client : " + e);}

System.out.println(new Date());


class Timer extends Thread
{
Timer()
{
start(); // start the Thread.
}

public void run()
{
System.out.println("Here I am");
try {
for(int i=0; i < 30 ; i++)
{

Thread.sleep(1000);
}

// If inputStream is Null
if(dis !=null)
{
try {
dis.close();
}catch(Exception e)
{ System.out.println("Exception caught here : " + e);}
}

}catch(InterruptedException e){
System.out.println("Exception Caught" + e);}


}

}

} // End of Method sendData

} // End of Class HttpHandler

   <<Less

Re: Please can some help me with the THREADING issue.-Urgent Please.

Posted By:   Christopher_Schultz  
Posted On:   Friday, March 15, 2002 05:50 AM

I think you want something like this:





public class TimerThread
extends Thread
{
Object _monitor;
long _timeout;

TimerThread(Object monitor, long timeout);
{
_monitor = monitor;
_timeout = timeout;
}

public void run()
{
try
{
Thread.sleep(timeout);
_monitor.notify();
}
catch (InterruptedException ie)
{
// Ignore
}
}
}

public DataThread
extends Thread
{
Object _monitor;
boolean _cancelled = false;
boolean _complete = false;

DataThread(Object monitor)
{
_monitor = monitor;
}

public void cancelSend()
{
_cancelled = true;
}

public boolean isComplete()
{
return(_complete);
}

public void run()
{
try
{
// Contact servlet

if(_cancelled)
return; // Bail out

// Read data, etc.

if(_cancelled)
return; // Bail out
}
finally
{
// We're done if we didn't get cancelled
if(!_cancelled)
{
_complete = true;
_monitor.notify();
}
}
}
}

public class Driver
{
public static void main(String[] args)
{
new Driver().go();
}

public synchronized void go()
{
TimerThread timer = new TimerThread(this, 30*1000);
DataThread dataThread = new DataThread(this);

timer.start();
dataThread.start();

try
{
this.wait();

if(dataThread.isComplete())
{
System.err.println("Success!");
}
else
{
dataThread.cancelSend();
System.err.println("Timeout reached.");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}



Hope that helps. I wrote this all off the top of my head. It may or may not compile. It may or may not work. It may or may not do what you want it to do.



Typically, if nobody is answering your question, it is because it is a tough question or nobody can figure out a good answer for it. The more times you ask the same question, the less attention it will get.



I would suggest that, instead of asking someone to solve your problem, you ask for resources on, say, Threading, and how to go about limiting the time an operation can take.



I'm almost positive that there are resources on this exact question out there.



Good luck,

-chris
About | Sitemap | Contact