You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@ws.apache.org by Markus Fischer <ma...@fischer.name> on 2006/02/20 16:08:10 UTC

Exception in thread doesn't return error code to system

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

first of all please excuse me writing this question here as I think
myself it doesn't have much to do with the apache xml-rpc library
itself. However I though it might be better asking people working with
the WebServer directly, maybe the answer simple.

When an exception happens, in my case

Exception in thread "XML-RPC Weblistener" java.lang.RuntimeException:
Address already in use"

the application returns to the system but no error code is given back to
the shell. I'm running a standalone application at the unix shell prompt.

Usually when an uncaught Exception occurs, it is returned "1" to the shell.

I *think* this is different here because org.apache.xmlrpc.WebServer is
using threads and the Listener thread throws this exception and not the
main thread.

I'm not very confident with Java so I wasn't able to figure out how I
can signal the shell that an error occurred in this special case.

I need this as I'm using a cronjob to start the application but in case
of errors a notification should be sent and currently I'm using the
return value at the shell prompt.

thanks for any pointers and sorry for this off-topic question,
- - Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD+dta1nS0RcInK9ARAhH4AJ9uLox9RBIzhxe3yS8ZQokfDlnH8gCeKpdZ
LDDN3eS9OD6iIQmf2K5nhJE=
=SSEm
-----END PGP SIGNATURE-----

Re: Exception in thread doesn't return error code to system

Posted by Markus Fischer <ma...@fischer.name>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Adam Taft wrote:
> In the version of the XmlRpc library I'm using (1.2-b1), the exception
> you're getting is actually being thrown by the WebServer.run() method.
> It looks like WebServer.start() is spawning a new thread to invoke
> WebServer.run().
> 
> Therefore, you'd probably be best to override WebServer.run(). Something
> like:
> 
> class MyWebServer extends WebServer {
>   public void run() {
>     try {
>       super.run();
>     } catch (Exception e) {
>       System.exit(<n>);
>     }
>   }
> }

Thanks, that did it!

- - Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD+rvv1nS0RcInK9ARAv53AKCJhAj3XM7CR03fPNiZ8wFZ6NGqCwCeMwV7
7Rba5l3ODxVcsKyG/DxKSh4=
=JkVm
-----END PGP SIGNATURE-----

Re: Exception in thread doesn't return error code to system

Posted by Adam Taft <ad...@hydroblaster.com>.
In the version of the XmlRpc library I'm using (1.2-b1), the exception 
you're getting is actually being thrown by the WebServer.run() method. 
It looks like WebServer.start() is spawning a new thread to invoke 
WebServer.run().

Therefore, you'd probably be best to override WebServer.run(). 
Something like:

class MyWebServer extends WebServer {
   public void run() {
     try {
       super.run();
     } catch (Exception e) {
       System.exit(<n>);
     }
   }
}


Another possible idea would be to open up a "normal" socket on your port 
and catch any errors you get.  Then, you'd close it and start your 
server on the same port.

The specific error you're getting, as I'm sure you know, is because 
you've already got a socket open / server listening on the specified 
port.  Trying to open a listener on that port should give you the same 
error back, but in a more predictable way (since it would be on the same 
thread).

Your startup shell script could/should also be looking for running 
versions of your server and fail if it already sees a version running. 
This would be a pretty smart thing to do regardless of what exceptions 
you're trying to catch.


Markus Fischer wrote:
> Hi,
> 
> Adam Taft wrote:
>> Markus Fischer wrote:
>>> I'm not very confident with Java so I wasn't able to figure out how I
>>> can signal the shell that an error occurred in this special case.
>> Catch the error and then do:
>>
>> System.exit(n);
>>
>> where n is the error number you want to return to the shell.
> 
> I tried being that clever, but it seems that I was trying to catch the
> Exception in the wrong place. My code looks like this:
> 
> WebServer webserver = new WebServer (port);
> webserver.addHandler(handler, object);
> webserver.start ();
> 
> When the BindException is thrown he's in start() method, putting the
> exception block around it doesn't catch it.
> 
> I *think* if it would have been thrown there I would have gotten the
> right return value on the shell but it isn't.
> 
> thanks,
> - Markus
> 

Re: Exception in thread doesn't return error code to system

Posted by Markus Fischer <ma...@fischer.name>.
Hi,

Adam Taft wrote:
> Markus Fischer wrote:
>> I'm not very confident with Java so I wasn't able to figure out how I
>> can signal the shell that an error occurred in this special case.
> 
> Catch the error and then do:
> 
> System.exit(n);
> 
> where n is the error number you want to return to the shell.

I tried being that clever, but it seems that I was trying to catch the
Exception in the wrong place. My code looks like this:

WebServer webserver = new WebServer (port);
webserver.addHandler(handler, object);
webserver.start ();

When the BindException is thrown he's in start() method, putting the
exception block around it doesn't catch it.

I *think* if it would have been thrown there I would have gotten the
right return value on the shell but it isn't.

thanks,
- Markus

Re: Exception in thread doesn't return error code to system

Posted by Adam Taft <ad...@hydroblaster.com>.

Markus Fischer wrote:
> I'm not very confident with Java so I wasn't able to figure out how I
> can signal the shell that an error occurred in this special case.

Catch the error and then do:

System.exit(n);

where n is the error number you want to return to the shell.

See also 
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#exit(int)