Solved

Stopping a URLConnection.connect() blocking from other Thread

Posted on 1998-10-26
4
925 Views
Last Modified: 2011-10-03
Hi;

I have two threads:

Thread one is doing this:

1) URLConnection myCon = new URLConnection(url);
2) myCon.connect();
3) .. now it blocks for a long time

At this moment Thread two wanna cancel the connect!
Is there a way to close the connection (like you can
close a socket connection with the close() method) ?
I don't wanna use the Thread.stop() or Thread.interrupt()
method, because it doesn't make the situation better!

Any ideas ?
Greetings Oliver

0
Comment
Question by:mos
  • 2
4 Comments
 
LVL 2

Expert Comment

by:shchuka
ID: 1226401
I don't think there's another way but calling Thread.stop() or Thread.interrupt().  The blocking occurs withing standard java classes, and thus this is like trying to get inside a library function in C.  You have to work with thread methods.

-Aleks.
0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 160 total points
ID: 1226402
Actualy, your problem is not related to threads at all, but rather, to the blocking nature of most  TCP calls.

As of JDK 1.1, you have several options to control this blocking. Quoting from the JDK docs:

"Socket Options in Java

javadoc's of socket classes for JDK1.1 are here!

[snip]

In java

The C way of setting options lacks the type-safety of object-oriented programming. The option one wishes to set/get is identified by an int, and the value to set/get into is an opaque void* . It is all too easy to pass the wrong option identifier, the wrong type object in the void* parameter, or the wrong for that parameter. Worse still, the code for these errors will typically compile, and the error will only be manifested at runtime.

Java now provides a type-safe way to set options. Each socket class has a get/set method for each option it supports, taking and returning the appropriate type. The options supported, for which socket classes and their meaning in brief:


·  TCP_NODELAY

·  Disable Nagle's algorithm.
·  Valid for (client) Sockets.

·  SO_LINGER

·  Specify a linger-on-close timeout.
·  Valid for (client) Sockets.

·  SO_TIMEOUT

·  Specify a timeout on blocking socket operations. (Don't block forever!)
·  Valid for all sockets: Socket, ServerSocket, DatagramSocket.

·  IP_MULTICAST_IF

·  Specify the outgoing interface for multicast packets (on multihomed hosts).
·  Valid for MulticastSockets.

Additionally, JDK1.1 provides new constructors to Socket, ServerSocket and DatagramSocket that allow the specification of a local address/port to which the socket should bind, and similarly these classes can divulge their local addresses.

Full javadoc's for the socket classes in JDK1.1 are provided here, including complete details on how to get/set  all options. If there's other options you'd like to use from java [tell us!]

[snip]

The implementation details...

.that you don't need to know, unless you subclass SocketImpl/DatagramSocketImpl. Every *Socket object has an underlying SocketImpl/DatagramSocketImpl that interfaces to native code. The Impl classes implement two methods to support options:

        void setOption(int optID, Object val) throws SocketException;
        Object getOption(int optID) throws SocketException;
that look much like C. These methods act as glue to the native methods, and ensure type safety before native methods are invoked. "


So, basicly, you need to create t your own SocketImpl and either define a timeout or make it non blocking.  Generaly speeking, you would use no delay on DB calls (such as NSLOOKUP) and time out on non DB calls.

Of course, this implies that you will have to either set up a polling loop to check if any data has arrived or asociate your socket to an event that can be triggered when data arrives. (c.f.: Class java.net.Socket  in the JDK docs.)

Hope this helps.
0
 

Author Comment

by:mos
ID: 1226403
Thanks jpk for this answer!

Does this mean I can't use the URLConnection class ?
From what know does the URLConnection class contain a socket so that the
functionality of sockets should be there!
Using a Socket for a http request means that I have to implement all the http
stuff again (e.g. redirect) that is already implented in the URLConnection class.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1226404
Not dirrectly no.

But you can derive a class from URLConnection and have it implement  a SocketImpl (SocketImpl implements SocketOptions which is not documented).

By making this derived class, you maintain the functionality of the URLConnection but gain access (inderectly) to the URLConnection's base socket. In this way, you can access the SocketOptions methods which will allow you to set up non blocking calls.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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