?
Solved

Stopping a URLConnection.connect() blocking from other Thread

Posted on 1998-10-26
4
Medium Priority
?
968 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 480 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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