You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Emmanuel Lecharny <el...@apache.org> on 2009/05/28 13:04:44 UTC

Re: How to catch java.nio.channels.UnresolvedAddressException ?

On Thu, May 28, 2009 at 12:47 PM, Alexander Christian <al...@root1.de> wrote:

>
> Hi guys,
>
> I have a small problem.
>
> I have the following part of code:
>
> ----
> ConnectFuture future = null;
> try {
>        future = connector.connect(new InetSocketAddress(host, port));
> } catch (Exception e){
>        throw new EstablishConnectionFailed("Can't connect due to unresolved
> socket address. host="+host+" port="+port+" error: "+e.getMessage());
> }
> ----
>
> In case I used an IP adress and not an hostname (to prevent "unresolved xyz
> exceptions"), AND the current machine has some network problems (can be
> "simulated" by disabling the network-device in windows xp), I get the
> following exception:
>
> ----
> Exception in thread "main" java.nio.channels.UnresolvedAddressException
>        at sun.nio.ch.Net.checkAddress(Unknown Source)
>        at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
>        at
>
> org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
>        at
>
> org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:46)
>        at
>
> org.apache.mina.core.polling.AbstractPollingIoConnector.connect0(AbstractPollingIoConnector.java:324)
>        at
>
> org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:256)
>        at
>
> org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:173)
>        at de.root1.simon.Simon.lookup(Simon.java:xxx)
>        at de.root1.simon.Simon.lookup(Simon.java:yyy)
>        at SampleClient.main(SampleClient.java:zzz)
> ----
>
> As you can see, I already build an "universal" try/catch block to catch
> this exception. But it doesn't matter what kind of exception I'm trying to
> catch (tried: java.nio.channels.UnresolvedAddressException,
> java.lang.RuntimeException and java.lang.Exception), I'm finally not able
> to catch the exception and "transform" it to my own exception
> (EstablishConnectionFailed).
>
> I'm using MINA 2.0.0M4 and JRE/JDK6U11 on Windows XP SP3.
> Any ideas on how to get the exception catched properly?


The connection is asynchronous, and you get a future as a reply to your
request. If you want to wait for the result, you must do something like :

try {
  future.awaitUninterruptibly
} catch ( Exception e ) {
  // You should get some exception here.
}


-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

Re: How to catch java.nio.channels.UnresolvedAddressException?

Posted by Alexander Christian <al...@root1.de>.
Hi again,

finally I found the problem. Here's a complete reproducer-sample:

----
import java.net.InetSocketAddress;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;


public class MinaExceptionTest {
	
	public static void main(String[] args) {
		
		IoConnector connector = new NioSocketConnector();
		connector.setHandler(new IoHandler(){

			@Override
			public void exceptionCaught(IoSession session, Throwable cause)
					throws Exception {
				System.out.println("exceptionCaught: session="+session+"
cause="+cause);
			}

			@Override
			public void messageReceived(IoSession session, Object message)
					throws Exception {
				System.out.println("messageReceived: session="+session+"
message="+message);				
			}

			@Override
			public void messageSent(IoSession session, Object message)
					throws Exception {
				System.out.println("messageSent: session="+session+"
message="+message);
			}

			@Override
			public void sessionClosed(IoSession session) throws Exception {
				System.out.println("sessionClosed: session="+session);
			}

			@Override
			public void sessionCreated(IoSession session) throws Exception {
				System.out.println("sessionCreated: session="+session);
			}

			@Override
			public void sessionIdle(IoSession session, IdleStatus status)
					throws Exception {
				System.out.println("sessionIdle: session="+session+
"status="+status);								
			}

			@Override
			public void sessionOpened(IoSession session) throws Exception {
				System.out.println("sessionOpened: session="+session);				
			}
			
		});
		
		System.out.println("Connecting ...");
		ConnectFuture future = connector.connect(new
InetSocketAddress("my.notexisting.domain.com", 80));
		System.out.println("connect() returned");
		future.awaitUninterruptibly();
		System.out.println("connected! Now trying to get session ...");
		future.getSession();
		System.out.println("Got session");
		System.exit(0);
	}

}
----

Try running this sample with disabled network-devices in Windows. You will
get the following output:

----
Connecting ...
connect() returned
connected! Now trying to get session ...
Exception in thread "main" java.nio.channels.UnresolvedAddressException
	at sun.nio.ch.Net.checkAddress(Unknown Source)
	at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
	at
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
	at
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:46)
	at
org.apache.mina.core.polling.AbstractPollingIoConnector.connect0(AbstractPollingIoConnector.java:324)
	at
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:256)
	at
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:173)
	at MinaExceptionTest.main(MinaExceptionTest.java:60)
----

The problem now is: the await... line returns also in case of errors. And
that was my problem. I thought this would give exceptions or things like
that. So I continued with getting the session. And THIS brought up the
exception with the really weird stack trace that seems to point to the
wrong line. 

Maybe getSession() should internally check the status (connected or not)
and return an exception or so. It seems that this is right now implemented,
but as you can see, the wrong stacktrace is printed ...

For now I will use "future.isConnected()" right before getting the session.
This will help identifiyng the issue better. Other possibility: Use a
try/catch block around the getSession line that checks for "Exception", or
to be more precise, check for java.nio.channels.UnresolvedAddressException.

But checking the status (connected or not) would be better.

br,

Alex



On Thu, 28 May 2009 17:18:54 +0200, Emmanuel Lecharny
<el...@apache.org>
wrote:
>>
>> > I
>> > see no way for java to not get through the catch( Exception e )...
>>
>> Me too. That's really a strange behaviour.
> 
> 
> Even if the  UnresolvedAddressException is a RuntimeException, it should
be
> caught by the catch( Exception e). There is something weird...
> 
> 
>>
>> > Can you
>> > add a System.out.println() in the catch part of your code ?
>>
>> I will try. But as the stack-trace says that this IS comming from the
>> connect-method I'm not sure if an sysout is telling something different.
> 
> 
> Just keep me updated.

Re: How to catch java.nio.channels.UnresolvedAddressException ?

Posted by Emmanuel Lecharny <el...@apache.org>.
>
> > I
> > see no way for java to not get through the catch( Exception e )...
>
> Me too. That's really a strange behaviour.


Even if the  UnresolvedAddressException is a RuntimeException, it should be
caught by the catch( Exception e). There is something weird...


>
> > Can you
> > add a System.out.println() in the catch part of your code ?
>
> I will try. But as the stack-trace says that this IS comming from the
> connect-method I'm not sure if an sysout is telling something different.


Just keep me updated.

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

Re: How to catch java.nio.channels.UnresolvedAddressException?

Posted by Alexander Christian <al...@root1.de>.
On Thu, 28 May 2009 16:30:24 +0200, Emmanuel Lecharny
<el...@apache.org>
wrote:
> 
> 
> Well, are you sure that the log you get is not generated somwhere else ? 

Yes, I'm sure. 

> I
> see no way for java to not get through the catch( Exception e )... 

Me too. That's really a strange behaviour.

> Can you
> add a System.out.println() in the catch part of your code ?

I will try. But as the stack-trace says that this IS comming from the
connect-method I'm not sure if an sysout is telling something different.


br, 

Alex




> 
> 
>>
>> Maybe you forgot that this only happens in case of a network problem, as
>> i
>> described in my last mail.
> 
> No, the exception wasn't necessarily  produced by the connect() part. (at
> least, this is what I though, as I don't have access to the current code
at
> the office).
>
>
>
> On Thu, May 28, 2009 at 4:12 PM, Alexander Christian <al...@root1.de>
wrote:
> 
>>
>> Sorry, this does not work.
>> Here's my changed sample code:
>>
>> ----
>> 468: ConnectFuture future = null;
>> 469: try {
>> 470:        future = connector.connect(new InetSocketAddress(host,
>> port));
>> 471:        future.awaitUninterruptibly();
>> 472: } catch (Exception e){
>> 473:        throw new EstablishConnectionFailed("Can't connect due to
>> unresolved socket address. host="+host+" port="+port+" error:
>> "+e.getMessage());
>> 474: }
>> ----
>>
>> And the exception:
>> ----
>> Exception in thread "main" java.nio.channels.UnresolvedAddressException
>>        at sun.nio.ch.Net.checkAddress(Unknown Source)
>>        at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
>>        at
>>
>>
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
>>        at
>>
>>
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:46)
>>        at
>>
>>
org.apache.mina.core.polling.AbstractPollingIoConnector.connect0(AbstractPollingIoConnector.java:324)
>>        at
>>
>>
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:256)
>>        at
>>
>>
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:173)
>>         at de.root1.simon.Simon.lookup(Simon.java:470) <---- This is the
>> line in
>> which the exception occurs
>>        at de.root1.simon.Simon.lookup(Simon.java:322)
>>        at SampleClient.main(SampleClient.java:86)
>> ----
>>
>>
>> As you can see: The exception is is thrown at line 470, which is the
line
>> with the connect() call. And the exception is not caught. So it's not up
>> to
>> future.awaitUninterruptibly() to throw the exception.


Re: How to catch java.nio.channels.UnresolvedAddressException ?

Posted by Emmanuel Lecharny <el...@apache.org>.
On Thu, May 28, 2009 at 4:12 PM, Alexander Christian <al...@root1.de> wrote:

>
> Sorry, this does not work.
> Here's my changed sample code:
>
> ----
> 468: ConnectFuture future = null;
> 469: try {
> 470:        future = connector.connect(new InetSocketAddress(host, port));
> 471:        future.awaitUninterruptibly();
> 472: } catch (Exception e){
> 473:        throw new EstablishConnectionFailed("Can't connect due to
> unresolved socket address. host="+host+" port="+port+" error:
> "+e.getMessage());
> 474: }
> ----
>
> And the exception:
> ----
> Exception in thread "main" java.nio.channels.UnresolvedAddressException
>        at sun.nio.ch.Net.checkAddress(Unknown Source)
>        at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
>        at
>
> org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
>        at
>
> org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:46)
>        at
>
> org.apache.mina.core.polling.AbstractPollingIoConnector.connect0(AbstractPollingIoConnector.java:324)
>        at
>
> org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:256)
>        at
>
> org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:173)
>         at de.root1.simon.Simon.lookup(Simon.java:470) <---- This is the
> line in
> which the exception occurs
>        at de.root1.simon.Simon.lookup(Simon.java:322)
>        at SampleClient.main(SampleClient.java:86)
> ----
>
>
> As you can see: The exception is is thrown at line 470, which is the line
> with the connect() call. And the exception is not caught. So it's not up to
> future.awaitUninterruptibly() to throw the exception.


Well, are you sure that the log you get is not generated somwhere else ? I
see no way for java to not get through the catch( Exception e )... Can you
add a System.out.println() in the catch part of your code ?


>
> Maybe you forgot that this only happens in case of a network problem, as i
> described in my last mail.

No, the exception wasn't necessarily  produced by the connect() part. (at
least, this is what I though, as I don't have access to the current code at
the office).


-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

Re: How to catch java.nio.channels.UnresolvedAddressException?

Posted by Alexander Christian <al...@root1.de>.
Sorry, this does not work.
Here's my changed sample code:

----
468: ConnectFuture future = null;
469: try {
470:        future = connector.connect(new InetSocketAddress(host, port));
471:        future.awaitUninterruptibly();
472: } catch (Exception e){
473:        throw new EstablishConnectionFailed("Can't connect due to
unresolved socket address. host="+host+" port="+port+" error:
"+e.getMessage());
474: }
----

And the exception:
----
Exception in thread "main" java.nio.channels.UnresolvedAddressException
	at sun.nio.ch.Net.checkAddress(Unknown Source)
	at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
	at
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
	at
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:46)
	at
org.apache.mina.core.polling.AbstractPollingIoConnector.connect0(AbstractPollingIoConnector.java:324)
	at
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:256)
	at
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:173)
	at de.root1.simon.Simon.lookup(Simon.java:470) <---- This is the line in
which the exception occurs
	at de.root1.simon.Simon.lookup(Simon.java:322)
	at SampleClient.main(SampleClient.java:86)
----


As you can see: The exception is is thrown at line 470, which is the line
with the connect() call. And the exception is not caught. So it's not up to
future.awaitUninterruptibly() to throw the exception. 
Maybe you forgot that this only happens in case of a network problem, as i
described in my last mail.

br,
Alex



On Thu, 28 May 2009 13:04:44 +0200, Emmanuel Lecharny
<el...@apache.org>
wrote:
> 
> 
> The connection is asynchronous, and you get a future as a reply to your
> request. If you want to wait for the result, you must do something like :
> 
> try {
>   future.awaitUninterruptibly
> } catch ( Exception e ) {
>   // You should get some exception here.
> }
>
>
> On Thu, May 28, 2009 at 12:47 PM, Alexander Christian <al...@root1.de>
> wrote:
> 
>>
>> Hi guys,
>>
>> I have a small problem.
>>
>> I have the following part of code:
>>
>> ----
>> ConnectFuture future = null;
>> try {
>>        future = connector.connect(new InetSocketAddress(host, port));
>> } catch (Exception e){
>>        throw new EstablishConnectionFailed("Can't connect due to
>>        unresolved
>> socket address. host="+host+" port="+port+" error: "+e.getMessage());
>> }
>> ----
>>
>> In case I used an IP adress and not an hostname (to prevent "unresolved
>> xyz
>> exceptions"), AND the current machine has some network problems (can be
>> "simulated" by disabling the network-device in windows xp), I get the
>> following exception:
>>
>> ----
>> Exception in thread "main" java.nio.channels.UnresolvedAddressException
>>        at sun.nio.ch.Net.checkAddress(Unknown Source)
>>        at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
>>        at
>>
>>
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
>>        at
>>
>>
org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:46)
>>        at
>>
>>
org.apache.mina.core.polling.AbstractPollingIoConnector.connect0(AbstractPollingIoConnector.java:324)
>>        at
>>
>>
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:256)
>>        at
>>
>>
org.apache.mina.core.service.AbstractIoConnector.connect(AbstractIoConnector.java:173)
>>        at de.root1.simon.Simon.lookup(Simon.java:xxx)
>>        at de.root1.simon.Simon.lookup(Simon.java:yyy)
>>        at SampleClient.main(SampleClient.java:zzz)
>> ----
>>
>> As you can see, I already build an "universal" try/catch block to catch
>> this exception. But it doesn't matter what kind of exception I'm trying
>> to
>> catch (tried: java.nio.channels.UnresolvedAddressException,
>> java.lang.RuntimeException and java.lang.Exception), I'm finally not
able
>> to catch the exception and "transform" it to my own exception
>> (EstablishConnectionFailed).
>>
>> I'm using MINA 2.0.0M4 and JRE/JDK6U11 on Windows XP SP3.
>> Any ideas on how to get the exception catched properly?