Solved

Stopping a URLConnection.connect() blocking from other Thread

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HSSFWorkbook cannot be resolved error 10 68
Basic Java Case or If-Else statement... 3 50
java stored proc example 9 29
arguments to jar 5 26
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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