You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Micka <mi...@gmail.com> on 2011/05/20 10:09:44 UTC

Tomcat7 + socketserver and contextListener

Hi,
*
Informations :*

System :
Ubuntu 10.04 Server

Tomcat :
Using CATALINA_BASE:   /usr/local/tomcat7
Using CATALINA_HOME:   /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME:        /usr/lib/jvm/java-6-sun
Using CLASSPATH:       /usr/local/tomcat7/bin/
bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.12
Server built:   Apr 1 2011 06:13:02
Server number:  7.0.12.0
OS Name:        Linux
OS Version:     2.6.32-24-server
Architecture:   amd64
JVM Version:    1.6.0_24-b07
JVM Vendor:     Sun Microsystems Inc.

Tomcat script :

# Tomcat auto-start
#
# description: Auto-starts tomcat 7
# processname: tomcat7
# pidfile: /var/run/tomcat.pid

export JAVA_HOME=/usr/lib/jvm/java-6-sun

case $1 in
start)
        sh /usr/local/tomcat7/bin/startup.sh
        ;;
stop)
        sh /usr/local/tomcat7/bin/shutdown.sh
        ;;
restart)
        sh /usr/local/tomcat7/bin/shutdown.sh
        sh /usr/local/tomcat7/bin/startup.sh
        ;;
esac
exit 0


*Problem 1:*

I use Tomcat7 to host a socket server.

My application use ServletContextListener to start as well as to stop (
contextInitialized and contextDestroyed ).


Each time that my socket server receive a connection with :

Socket java.net.ServerSocket.accept()

I put the socket in a thread, and the thread manage the dialog with the
client ( device, not a browser :p )


To manage a pool of thread, I use the class :
ExecutorService


In the thread there is a loop with a    int java.io.Reader.read(char[] cbuf)


My problem is when i want to stop tomcat with :

 sudo /etc/init.d/tomcat stop

I received this event on my application by implementing the class
ServletContextListener and using the function contextDestroyed :

When contextDestroyed is called, I close all the socket and thread. But it
takes time, and because of that, I always have :

SEVERE: The web application [/SERVER] registered the JDBC driver
[org.postgresql.Driver] but failed to unregister it when the web applicatio$
May 19, 2011 4:30:21 PM org.apache.catalina.loader.WebappClassLoader
clearReferencesThreads
SEVERE: The web application [/SERVER] appears to have started a thread named
[pool-3-thread-1] but has failed to stop it. This is very likel$
May 19, 2011 4:30:21 PM org.apache.catalina.loader.WebappClassLoader
clearReferencesThreads
SEVERE: The web application [/SERVER] appears to have started a thread named
[pool-3-thread-2] but has failed to stop it. This is very likel$
May 19, 2011 4:30:21 PM org.apache.catalina.loader.WebappClassLoader
clearReferencesThreads
SEVERE: The web application [/SERVER] appears to have started a thread named
[pool-2-thread-2] but has failed to stop it. This is very likel$
May 19, 2011 4:30:21 PM org.apache.catalina.loader.WebappClassLoader
clearReferencesThreads
SEVERE: The web application [/SERVER] appears to have started a thread named
[pool-3-thread-3] but has failed to stop it. This is very likel$
May 19, 2011 4:30:21 PM org.apache.catalina.loader.WebappClassLoader
clearReferencesThreads


*Question 1 :* Is there a way to change the timeout of the stop function ?

*Question 2 :* I also heard that sometime contextDestroyed and
contextInitialized is called at unexpected time. Is it related to the use of
memory ? By example, if there is a leaked memory, is it possible that tomcat
decide to close all the application and restart it ?

To summarize these questions : " When contextDestroyed is called ? "


*Problem 2 :*

After  sudo /etc/init.d/tomcat stop , I try again to start the server, but i
got :

May 19, 2011 4:28:22 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal
performance in production environments was not found on the java.library.pa$
May 19, 2011 4:28:22 PM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
May 19, 2011 4:28:22 PM org.apache.coyote.AbstractProtocolHandler init
SEVERE: Failed to initialize end point associated with ProtocolHandler
["http-bio-8080"]
java.net.BindException: Address already in use <null>:8080
        at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)
        at
org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:482)
        at
org.apache.coyote.AbstractProtocolHandler.init(AbstractProtocolHandler.java:354)
        at
org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:119)
        at
org.apache.catalina.connector.Connector.initInternal(Connector.java:910)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at
org.apache.catalina.core.StandardService.initInternal(StandardService.java:559)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at
org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:781)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:572)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:262)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:430)
Caused by: java.net.BindException: Address already in use
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
        at java.net.ServerSocket.bind(ServerSocket.java:328)
        at java.net.ServerSocket.<init>(ServerSocket.java:194)
        at java.net.ServerSocket.<init>(ServerSocket.java:150)
        at
org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:48)
        at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:365)


I don't understand, because sockstat -p 8080 give an empty answer :
USER     PROCESS              PID      PROTO  SOURCE ADDRESS
FOREIGN ADDRESS           STATE

And if try to stop again tomcat i got :

java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at java.net.Socket.<init>(Socket.java:375)
        at java.net.Socket.<init>(Socket.java:189)
        at
org.apache.catalina.startup.Catalina.stopServer(Catalina.java:456)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:352)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)


*Question 3 : *is that normal ?



Thx for the help !

Re: Tomcat7 + socketserver and contextListener

Posted by André Warnier <aw...@ice-sa.com>.
André Warnier wrote:
> Micka wrote:
>> If it was that simple, I would not ask your help ^^.
>>
>> An example :
>>
>> Tomcat was stopped before, and the commands :
>>  sockstat -p 8080
>>
>> give nothing.
>>
> Where does this sockstat command come from ?
> I do not seem to have that on either Windows or Linux systems.
> 
> 
> And all I can tell you about this :
> 
> SEVERE: Failed to initialize end point associated with ProtocolHandler
> ["http-bio-8080"]
> java.net.BindException: Address already in use <null>:8080
>         at 
> org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)
> 
> Is that, undoubtedly, some process already "owns" a listening socket on 
> port 8080.
> So if it is not Tomcat, it must be something else.
> And if the above "sockstat" command does not show it, then there is 
> something wrong with the sockstat command, or with the way you are using 
> it.
> 
Addendum :
I found the sockstat command in Google, and noticed this :

Note that TCP sockets in the  AF_INET or  AF_INET6 domains that are not in one of the 
LISTEN, SYN_SENT, or  ESTABLISHED states may not be shown by sockstat; use netstat(1) to 
examine them instead.

So maybe the socket is not shown, because it is in another state than the above.
Given that your Tomcat seems to have trouble stopping, this may be a possibility.

Try "netstat -pan --tcp | grep 8080" maybe ?

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat7 + socketserver and contextListener

Posted by André Warnier <aw...@ice-sa.com>.
Micka wrote:
> If it was that simple, I would not ask your help ^^.
> 
> An example :
> 
> Tomcat was stopped before, and the commands :
>  sockstat -p 8080
> 
> give nothing.
> 
Where does this sockstat command come from ?
I do not seem to have that on either Windows or Linux systems.


And all I can tell you about this :

SEVERE: Failed to initialize end point associated with ProtocolHandler
["http-bio-8080"]
java.net.BindException: Address already in use <null>:8080
         at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)

Is that, undoubtedly, some process already "owns" a listening socket on port 8080.
So if it is not Tomcat, it must be something else.
And if the above "sockstat" command does not show it, then there is something wrong with 
the sockstat command, or with the way you are using it.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat7 + socketserver and contextListener

Posted by Micka <mi...@gmail.com>.
If it was that simple, I would not ask your help ^^.

An example :

Tomcat was stopped before, and the commands :
 sockstat -p 8080

give nothing.


And when I start tomcat, I got :


SEVERE: Failed to initialize end point associated with ProtocolHandler
["http-bio-8080"]
java.net.BindException: Address already in use <null>:8080
        at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)
        at
org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:482)
        at
org.apache.coyote.AbstractProtocolHandler.init(AbstractProtocolHandler.java:354)
        at
org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:119)
        at
org.apache.catalina.connector.Connector.initInternal(Connector.java:910)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at
org.apache.catalina.core.StandardService.initInternal(StandardService.java:559)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at
org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:781)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:572)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:262)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:430)
Caused by: java.net.BindException: Address already in use
        at java.net.PlainSocketImpl.socketBind(Native Method)
        at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
        at java.net.ServerSocket.bind(ServerSocket.java:328)
        at java.net.ServerSocket.<init>(ServerSocket.java:194)
        at java.net.ServerSocket.<init>(ServerSocket.java:150)
        at
org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:48)
        at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:365)
        ... 17 more
May 20, 2011 3:04:59 PM org.apache.catalina.core.StandardService
initInternal
SEVERE: Failed to initialize connector [Connector[HTTP/1.1-8080]]
org.apache.catalina.LifecycleException: Protocol handler initialization
failed
        at
org.apache.catalina.connector.Connector.initInternal(Connector.java:912)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at
org.apache.catalina.core.StandardService.initInternal(StandardService.java:559)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at
org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:781)
        at
org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:101)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:572)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)


I really don't understand this problem ...




On Fri, May 20, 2011 at 11:59 AM, André Warnier <aw...@ice-sa.com> wrote:

> Micka wrote:
> ...
>
>
>> *Problem 2 :*
>>
>>
>> After  sudo /etc/init.d/tomcat stop , I try again to start the server, but
>> i
>> got :
>>
>> May 19, 2011 4:28:22 PM org.apache.catalina.core.AprLifecycleListener init
>> INFO: The APR based Apache Tomcat Native library which allows optimal
>> performance in production environments was not found on the
>> java.library.pa$
>> May 19, 2011 4:28:22 PM org.apache.coyote.AbstractProtocolHandler init
>> INFO: Initializing ProtocolHandler ["http-bio-8080"]
>> May 19, 2011 4:28:22 PM org.apache.coyote.AbstractProtocolHandler init
>> SEVERE: Failed to initialize end point associated with ProtocolHandler
>> ["http-bio-8080"]
>> java.net.BindException: Address already in use <null>:8080
>>        at
>> org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)
>>
>>
> It means that another instance of Tomcat is already (or still) running.
>
>
>  I don't understand, because sockstat -p 8080 give an empty answer :
>> USER     PROCESS              PID      PROTO  SOURCE ADDRESS
>> FOREIGN ADDRESS           STATE
>>
>>
> Probably just because between the first error, and your "sockstat" command,
> Tomcat really stopped.
>
>
>  And if try to stop again tomcat i got :
>>
>> java.net.ConnectException: Connection refused
>>        at java.net.PlainSocketImpl.socketConnect(Native Method)
>>        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
>>        at
>>
>
> And there again, just a timing issue : Tomcat is not running, si you can't
> stop it.
> (The connection is refused because the "stop" process tries to connect to a
> running Tomcat's port 8005, but since there is no Tomcat running, nothing is
> listening on that port and the connection is refused).
>
> In other words, the important part is your problem #1.
> When you have solved that one, problem #2 will probably go away by itself.
> But for problem #1, I cannot help you.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Tomcat7 + socketserver and contextListener

Posted by André Warnier <aw...@ice-sa.com>.
Micka wrote:
...

> 
> *Problem 2 :*
> 
> After  sudo /etc/init.d/tomcat stop , I try again to start the server, but i
> got :
> 
> May 19, 2011 4:28:22 PM org.apache.catalina.core.AprLifecycleListener init
> INFO: The APR based Apache Tomcat Native library which allows optimal
> performance in production environments was not found on the java.library.pa$
> May 19, 2011 4:28:22 PM org.apache.coyote.AbstractProtocolHandler init
> INFO: Initializing ProtocolHandler ["http-bio-8080"]
> May 19, 2011 4:28:22 PM org.apache.coyote.AbstractProtocolHandler init
> SEVERE: Failed to initialize end point associated with ProtocolHandler
> ["http-bio-8080"]
> java.net.BindException: Address already in use <null>:8080
>         at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)
> 

It means that another instance of Tomcat is already (or still) running.

> I don't understand, because sockstat -p 8080 give an empty answer :
> USER     PROCESS              PID      PROTO  SOURCE ADDRESS
> FOREIGN ADDRESS           STATE
> 

Probably just because between the first error, and your "sockstat" command, Tomcat really 
stopped.

> And if try to stop again tomcat i got :
> 
> java.net.ConnectException: Connection refused
>         at java.net.PlainSocketImpl.socketConnect(Native Method)
>         at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
>         at

And there again, just a timing issue : Tomcat is not running, si you can't stop it.
(The connection is refused because the "stop" process tries to connect to a running 
Tomcat's port 8005, but since there is no Tomcat running, nothing is listening on that 
port and the connection is refused).

In other words, the important part is your problem #1.
When you have solved that one, problem #2 will probably go away by itself.
But for problem #1, I cannot help you.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat7 + socketserver and contextListener

Posted by André Warnier <aw...@ice-sa.com>.
Micka,

Unless specifically asked otherwise, just send your messages to the list, do not copy 
other people.
We are all subscribed to the list and get all messages that way. If in addition you send 
it directly, we get it twice, which is annyoing.

Thanks.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org