Solved

Blocking Stream required

Posted on 2002-04-19
13
169 Views
Last Modified: 2010-03-31
I have opened an ObjectInput/OutputStreams between a java client and a servlet.

I can send objects between them.  Sometimes the servlet takes a while to respond to the request and the client polls until EOFException is not thrown on ObjectInputStream.readObject()

Is there anyway of using socket blocking so I don't have to poll?  i.e. the readObject will not return until an object is found

Thanks.
0
Comment
Question by:tims
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 34 total points
ID: 6954519
Have you tried Socket.setSoTimeout? With this option set to a non-zero value, a read() call on the InputStream associated with the Socket will block for the specified amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout. So if you want the read to block indefinitely, use setSoTimeout(0).

Best regards,
Jim Cakalic
0
 
LVL 92

Expert Comment

by:objects
ID: 6955164
> Is there anyway of using socket blocking so I don't have
> to poll?  i.e. the readObject will not return
> until an object is found

That's exactly how readObject() works??
Can you post your code?
0
 

Author Comment

by:tims
ID: 6959627
The code goes something like this....

URLConnection con = servlet.openConnection();

//Prepare for both input and output
con.setDoInput( true );
con.setDoOutput( true );
       
//Turn off caching
con.setUseCaches( false );

ObjectInputStream in = new ObjectInputStream( con.getInputStream() );

....

try
{
Object obj = in.readObject();
}
catch( EOFException e )
{
//Nowt on the stream.
}


Jim - I can't find a way to retrieve the socket for this stream so I can set the timeout.

Objects - I thought the stream should block if there is nothing to retrieve but it throws an EOFException.

I'm using jdk1.3.1.  Off topic - I want to move to 1.4 so I don't have to send something from the servlet to open the return stream.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6959635
you cannot access Socket behind URLConnection (at least for java 1.0 - java 1.3)
0
 
LVL 92

Expert Comment

by:objects
ID: 6961190
What's your servlet doing, it sounds like it is closing the stream without sending an object. This would cause your EOF.
If the stream is still open then it wouldn't be throwing an EOF.
0
 

Author Comment

by:tims
ID: 6965821
We're attempting to replace a few rmi calls with http requests to a servlet.

I know that there are a few implementations out there for rmi tunneling over http, but we would like to implement it so that it holds the streams open and the servlet holds information about the client.  

The current version opens a connection for each request or bunch of requests and it is far too slow, especially since we have to send a key across everytime to retrieve a session object.

Hopefully it is posible to have the client send objects to an open stream whenever it wishes, the servlet will do the business logic and send back an object with the response(s).  The streams should be held open indefinitly.

Please add any comments on this.

Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 6967093
HTTP is a request/response protocol.
HTTP does not support holding a connection open for full duplex comms.
There are various hacks around to attempt to achieve this, but they are not guaranteed to be reliable, and probably won't work thru most proxies.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6967908
> HTTP does not support holding a connection open for full duplex comms.

in fact HTTP 1.1 supports persistent connections.
0
 
LVL 92

Expert Comment

by:objects
ID: 6967930
yes I should stop generalising :) but how many servers/proxies support 1.1?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6967944
quck search on google does not return any usefull information :), but I suppose that all major players support HTTP 1.1  - am I wrong ?
0
 
LVL 92

Expert Comment

by:objects
ID: 6968032
not sure.
0
 

Author Comment

by:tims
ID: 6979847
After a few tries.  We have decided to postpone this bit of the project because I'm spending far too much time n it.  We are staying with the queue of requests, sending the queue (via http to a servlet) only when a request cannot wait.  The servlet replys with an array of update objects and then closes the streams.

The EJB remote interfaces are cached in the servlet class but it is still quite heavy compaired to just rmi - but it works.

Thanks for your help.  I've asked for the points to be split between the three of you.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6980366
Points have been split, per your Community Support question.

Points for objects -> http://www.experts-exchange.com/jsp/qShow.jsp?qid=20295372
Points for heyhey_ -> http://www.experts-exchange.com/jsp/qShow.jsp?qid=20295375

:) Moondancer - EE Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 80
Apps blocked by Java 9 55
object oriented programming comparison 5 52
java jdbc batch example 8 17
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

932 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now