Solved

Stopping a URLConnection.connect() blocking from other Thread

Posted on 1998-10-26
4
958 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 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

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

688 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