Solved

Stopping a URLConnection.connect() blocking from other Thread

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 30
How to execute a Python program and gather return output in Java 2 49
web project error add remove 1 52
junit example issue 2 27
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

733 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