Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have to send a log message to a servlet and not care about it

Status
Not open for further replies.

zooxmusic

Programmer
Nov 24, 2004
96
US
Hi all, this is probably a basic question but I need an answer quick and I don't know it. I have to send an xml doc to a servlet in a seperate thread for logging purposes and I don't care about it after I send it. As it is now I am sending it on an HttpUrlConnection and i have to ask for the InputStream before it actually executes the call. And then it sits and waits for the input stream to get back. I need a way to send the xml doc to the servlet and then I don't want to wait. Just move on and if it didn't get there, its not a big thing.

Brian


Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Use a class that extends Thread, takes an outputstream or httpurlconnection or url, and the xml doc/string as a constructor parameter and fire it off : eg :

Code:
class SenderThread extends Thread {
	String szUrl;
	String szData;
	
	public SenderThread(String szUrl, String szData) {
		this.szUrl = szUrl;
		this.szData = szData;
	}
	
	public void run() {
	   try {
		URL url = new URL(szUrl);
		HttpURLConnection huc = URL.openConnection();
		... send your data ...
	   } catch (IOException ioe) {
	   	...
	   }
	}
}

// and in your client code
new SenderThread("[URL unfurl="true"]http://www.bla.com/aServlet",[/URL] "<my><data>Hello</data></my>").start();

--------------------------------------------------
Free Database Connection Pooling Software
 
really, I had something similar and it didn't seem to execute until I called httpUrlConnection.getInputStream()? I will try it. thanks

Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Hmmm, that doesn't sound quite right - if you call Thread.start() it executes a brand new thread - no waiting around - the code is detached from the parent process.

Are you sure you called the start() method, and not run() or called join() ?

--------------------------------------------------
Free Database Connection Pooling Software
 
no, no... the HttpUrlConnection doesn't actually call the "other" servlet until I explicitly call httpUrlConnection.getInputStream(). And then that calls is syncronous so it sits there and waits. The thread stuff really was extraneous info that I didn't even need to mention. I have that part of it working fine.

Brian


Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Hmmmm. How does that work? I can't use UDP over http can I? What classes would be involved?

Brian


Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
I think, Brian, that if you posted snippets of your code we might spot why your intial attempt failed. Sedj's suggestion seemed quite sensible to me, so maybe you've got a slight problem in how you're handling your threading.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Ok,


private void call()
{
long startTime = 0;
URL url = null;
HttpURLConnection httpConnection = null;
OutputStreamWriter osw = null;

try
{

url = new URL(address);
httpConnection = (HttpURLConnection)url.openConnection();

httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(false);
osw = new OutputStreamWriter(httpConnection.getOutputStream());
osw.write("xmldata=" + URLEncoder.encode(xmlString, "latin1"));
osw.flush();
osw.close();

//If I don't put this in, it never gets called
InputStream in = httpConnection.getInputStream();

}
catch(Exception e)
{
....handle exception
}

Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
I did mention that I have the tread under control.

>>The thread stuff really was extraneous info that I didn't even need to mention


I understand and have the thread working. It's not my problem.

Am I not articulating something correctly?

Brian

Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Well possibly, because from what can gather so far, your situation is this :

You have an application.
Within that application, you perform some process.
Part of that means sending some data to a servlet - which you want to just hand off without blocking your parent thread's execution.

You say you have the threading correct, so I don't understand your problem - so what if you have to call getInputStream() ? Who cares ? That code is within a separate thread so its not blocking your parent process.

HTTP protocol is a TWO-WAY protocol. Client sends data. Server reads data. Server sends response. Client reads response. Transaction complete. Because most HTTP servers expect to send some data back to the client, they probably block the socket IO on the network level (see native C/C++ recv() and send() calls to understand why) - which would maybe explain why you must call getInputStream().

But I fail to understand why this is an issue to you.

--------------------------------------------------
Free Database Connection Pooling Software
 
Oh, I see. It is an issue because I suppose I did "care" that I had to call getInputStream(). I see your point and understand. I will now let it go. Thank you for setting me straight.

Brian


Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Cool [smile]
Don't forget to close the streams and urlconnection aswell.

--------------------------------------------------
Free Database Connection Pooling Software
 
Absolutely. Preaching to the choir baby ;)

Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top