You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Randy Toor <rt...@hotmail.com> on 2012/12/11 00:09:36 UTC

NPE when starting up Embedded tomcat7

Hello all,

I have an existing server which uses the Embedded class which I am tasked with upgrading to tomcat7 (deprecated, but we'll be keeping it for the time being).  There were some small changes to the code to fix API breaks but the startup stuff is the same.  When I start up the container I am now seeing this -

INFO: Starting tomcat server
org.apache.catalina.startup.Embedded||Dec 7, 2012 12:09:49 PM org.apache.catalina.startup.Embedded startInternal
INFO: Catalina naming disabled
Dec 7, 2012 12:09:49 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.32
Dec 7, 2012 12:09:49 PM org.apache.catalina.util.LifecycleBase start
INFO: The start() method was called on component [Realm[NullRealm]] after start() had already been called. The second call will be ignored.
org.apache.catalina.util.LifecycleBase||Dec 7, 2012 12:09:49 PM org.apache.catalina.util.LifecycleBase start
Dec 7, 2012 12:09:49 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]]
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:743)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 7 more
Caused by: java.lang.NullPointerException: while trying to invoke the method org.apache.catalina.Service.findConnectors() of an object loaded from local variable 's'
    at org.apache.catalina.core.ApplicationContext.populateSessionTrackingModes(ApplicationContext.java:1191)
    at org.apache.catalina.core.ApplicationContext.<init>(ApplicationContext.java:125)
    at org.apache.catalina.core.StandardContext.getServletContext(StandardContext.java:2323)
    at org.apache.catalina.core.StandardContext.postWorkDirectory(StandardContext.java:6062)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5102)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more
org.apache.catalina.core.ContainerBase||Dec 7, 2012 12:09:49 PM org.apache.catalina.core.ContainerBase startInternal


I looked into that particular line in ApplicationContext and I see this code is new in tomcat7 -

        Service s = ((Engine) context.getParent().getParent()).getService();
        Connector[] connectors = s.findConnectors();


Looking at the API docs for Engine, it indicates that a Service does not necessarily need to be associated with an Engine (from the docs - "Set the Service with which we are associated (if any).
").  Am I missing a step now to prevent this?  Or should there be a null check here?

Thank you!

 		 	   		  

Re: Stopping/pausing a Connector

Posted by Konstantin Kolinko <kn...@gmail.com>.
2014/1/22 Randy Toor <rt...@hotmail.com>:

The rules: do not top-post here
http://tomcat.apache.org/lists.html#tomcat-users
-> "6."

> It is 7.0.32.
>
> To be honest, I'm not sure which connector it is.  The code extends org.apache.catalina.connector.Connector, sets up HTTP settings, and adds it to the EmbeddedTomcat container.

What kind of connector is used is visible as thread names (if you do a
thread dump, or if you connect with jconsole), or in the logs (if you
enable logging).

> Using netstat I'm noticing that with either a pause() or a stop() the port is still being listened on.  It's only when I shut down completely that it stops.  I would prefer it to stop listening, at least that way I can the OS can return me an error or drop the connection, but instead it seems to accept requests and hold on to them until resume() or start() is called before processing.  Is this expected?

http://tomcat.apache.org/tomcat-7.0-doc/config/http.html
See "bindOnInit".

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


RE: Stopping/pausing a Connector

Posted by Randy Toor <rt...@hotmail.com>.
It is 7.0.32.

To be honest, I'm not sure which connector it is.  The code extends org.apache.catalina.connector.Connector, sets up HTTP settings, and adds it to the EmbeddedTomcat container.

Using netstat I'm noticing that with either a pause() or a stop() the port is still being listened on.  It's only when I shut down completely that it stops.  I would prefer it to stop listening, at least that way I can the OS can return me an error or drop the connection, but instead it seems to accept requests and hold on to them until resume() or start() is called before processing.  Is this expected?

Thanks!

> Date: Sun, 19 Jan 2014 19:38:40 +0400
> Subject: Re: Stopping/pausing a Connector
> From: knst.kolinko@gmail.com
> To: users@tomcat.apache.org
> 
> 2014/1/17 Randy Toor <rt...@hotmail.com>:
> > Hi,
> >
> > I'm running embedded tomcat (unfortunately still using the deprecated classes and not the newer Tomcat class) and I'm trying to configure my connector at runtime to reject requests.
> >
> > If I use connector.pause(), any requests just hang until I call connector.resume().
> >
> > If I use connector.stop(), the first request gets a 503 service unavailable, but any subsequent requests just hang for some reason until I start it again.
> >
> > Is the latter behaviour expected?  And what would be the best way for me to get the 503 service unavailable (or some error) every time?
> >
> > Tomcat7, by the way.
> >
> 
> 1. Exactly which one of ~50 different 7.0.x  versions?
> 2. What connector (BIO, NIO, APR)?
> 
> 3.  On a stop() I would expect Tomcat to close the sockets.  Clients
> that have already connected will have their connection aborted.
> 
> Clients that have not connected - the behavior depends on your OS
> network settings. The OS may reply that a port is unreachable, or may
> just drop the incoming connection.
> 
> In either case, when nothing listens on that port, you have nothing
> that could serve a "503" response.
> 
> Best regards,
> Konstantin Kolinko
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
 		 	   		  

Re: Stopping/pausing a Connector

Posted by Konstantin Kolinko <kn...@gmail.com>.
2014/1/17 Randy Toor <rt...@hotmail.com>:
> Hi,
>
> I'm running embedded tomcat (unfortunately still using the deprecated classes and not the newer Tomcat class) and I'm trying to configure my connector at runtime to reject requests.
>
> If I use connector.pause(), any requests just hang until I call connector.resume().
>
> If I use connector.stop(), the first request gets a 503 service unavailable, but any subsequent requests just hang for some reason until I start it again.
>
> Is the latter behaviour expected?  And what would be the best way for me to get the 503 service unavailable (or some error) every time?
>
> Tomcat7, by the way.
>

1. Exactly which one of ~50 different 7.0.x  versions?
2. What connector (BIO, NIO, APR)?

3.  On a stop() I would expect Tomcat to close the sockets.  Clients
that have already connected will have their connection aborted.

Clients that have not connected - the behavior depends on your OS
network settings. The OS may reply that a port is unreachable, or may
just drop the incoming connection.

In either case, when nothing listens on that port, you have nothing
that could serve a "503" response.

Best regards,
Konstantin Kolinko

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


Stopping/pausing a Connector

Posted by Randy Toor <rt...@hotmail.com>.
Hi,

I'm running embedded tomcat (unfortunately still using the deprecated classes and not the newer Tomcat class) and I'm trying to configure my connector at runtime to reject requests.

If I use connector.pause(), any requests just hang until I call connector.resume().

If I use connector.stop(), the first request gets a 503 service unavailable, but any subsequent requests just hang for some reason until I start it again.

Is the latter behaviour expected?  And what would be the best way for me to get the 503 service unavailable (or some error) every time?

Tomcat7, by the way.
 		 	   		  

RE: NPE when starting up Embedded tomcat7

Posted by Randy Toor <rt...@hotmail.com>.
Hi Chris,

The switch to the Tomcat class will be made soon enough, but for now it's really a time/risk thing at this stage of the project.  The Embedded class is ingrained pretty well and it isn't trivial to switch to use Tomcat.

Here is the code to set up the Embedded instance -


        embedded = new Embedded();

        embedded.setUseNaming(false);
        embedded.setCatalinaHome(catalinaHome);
        embedded.setCatalinaBase(catalinaBase);

        engine = embedded.createEngine();
        engine.setName(STANDARD_ENGINE_NAME);
        // This name MUST match the name attributes of one of the Host elements
        // nested immediately inside.
        engine.setDefaultHost(defaultHostName);
        engine.setParentClassLoader(classLoader);

        host = embedded.createHost(defaultHostName, appBase);
        // if not set: catalina/work/OSGiEngine/tomcatHost/_;
        // ow: catalina/<workDir>/_
        ((StandardHost) host).setWorkDir(workDir);

        host.setRealm(engine.getRealm());

        // http-access log
        ((StandardHost) host).addValve(createLoggingValve());

        engine.addChild(host);

        File rootContext = new File(System.getProperty(SYS_PROPERTY_CATALINA_HOME) + SLASH + ROOT);
        String docBase = "";
        if (rootContext.exists()) {
            docBase = rootContext.getAbsolutePath();
        } else {
            docBase = ROOT;
        }
        tomcatDefaultCtx = embedded.createContext("", docBase);
        tomcatDefaultCtx.setSessionCookiePath("/");
        tomcatDefaultCtx.addWelcomeFile(INDEX_JSP);
        host.addChild(tomcatDefaultCtx);

        embedded.addEngine(engine);


Actually, I did just try adding the line -

engine.setService(embedded);

Which does move me past the NPE (and on to a different error), but I'm not sure that this is what I want to do.  This code worked fine with tomcat6 and as I mentioned, the docs indicate a service need not be associated with an Engine.  Is it a requirement now?



> Date: Tue, 11 Dec 2012 10:30:58 -0500
> From: chris@christopherschultz.net
> To: users@tomcat.apache.org
> Subject: Re: NPE when starting up Embedded tomcat7
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Randy,
> 
> On 12/10/12 6:09 PM, Randy Toor wrote:
> > I have an existing server which uses the Embedded class which I am 
> > tasked with upgrading to tomcat7 (deprecated, but we'll be keeping
> > it for the time being).
> 
> What part is deprecated? The Embedded class itself? Why not just
> switch-over to using the Tomcat class? It's much easier to use and now
> would be the time to make the switch.
> 
> > There were some small changes to the code to fix API breaks but
> > the startup stuff is the same.  When I start up the container I am
> > now seeing this -
> > 
> > INFO: Starting tomcat server 
> > org.apache.catalina.startup.Embedded||Dec 7, 2012 12:09:49 PM
> > org.apache.catalina.startup.Embedded startInternal INFO: Catalina
> > naming disabled Dec 7, 2012 12:09:49 PM
> > org.apache.catalina.core.StandardEngine startInternal INFO:
> > Starting Servlet Engine: Apache Tomcat/7.0.32 Dec 7, 2012 12:09:49
> > PM org.apache.catalina.util.LifecycleBase start INFO: The start()
> > method was called on component [Realm[NullRealm]] after start() had
> > already been called. The second call will be ignored. 
> > org.apache.catalina.util.LifecycleBase||Dec 7, 2012 12:09:49 PM
> > org.apache.catalina.util.LifecycleBase start Dec 7, 2012 12:09:49
> > PM org.apache.catalina.core.ContainerBase startInternal SEVERE: A
> > child container failed during start 
> > java.util.concurrent.ExecutionException:
> > org.apache.catalina.LifecycleException: Failed to start component
> > [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]]
> >
> > 
> at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
> > at java.util.concurrent.FutureTask.get(FutureTask.java:83) at
> > org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
> >
> > 
> at
> org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
> > at
> > org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
> >
> > 
> at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
> > at
> > org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
> >
> > 
> at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
> > at java.util.concurrent.FutureTask.run(FutureTask.java:138) at
> > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> >
> > 
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> > at java.lang.Thread.run(Thread.java:743) Caused by:
> > org.apache.catalina.LifecycleException: Failed to start component
> > [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]]
> >
> > 
> at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
> > ... 7 more Caused by: java.lang.NullPointerException: while trying
> > to invoke the method org.apache.catalina.Service.findConnectors()
> > of an object loaded from local variable 's' at
> > org.apache.catalina.core.ApplicationContext.populateSessionTrackingModes(ApplicationContext.java:1191)
> >
> > 
> at
> org.apache.catalina.core.ApplicationContext.<init>(ApplicationContext.java:125)
> > at
> > org.apache.catalina.core.StandardContext.getServletContext(StandardContext.java:2323)
> >
> > 
> at
> org.apache.catalina.core.StandardContext.postWorkDirectory(StandardContext.java:6062)
> > at
> > org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5102)
> >
> > 
> at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
> > ... 7 more org.apache.catalina.core.ContainerBase||Dec 7, 2012
> > 12:09:49 PM org.apache.catalina.core.ContainerBase startInternal
> > 
> > 
> > I looked into that particular line in ApplicationContext and I see
> > this code is new in tomcat7 -
> > 
> > Service s = ((Engine)
> > context.getParent().getParent()).getService(); Connector[]
> > connectors = s.findConnectors();
> > 
> > 
> > Looking at the API docs for Engine, it indicates that a Service
> > does not necessarily need to be associated with an Engine (from the
> > docs - "Set the Service with which we are associated (if any)". Am
> > I missing a step now to prevent this? Or should there be a null
> > check here?
> 
> Can your post your own code that uses the Embedded class? It will
> probably be easier to spot a place where you've missed something
> important (or have it out of order) than to just look at the stack
> trace above and try to debug Tomcat in order to determine the original
> mistake.
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with undefined - http://www.enigmail.net/
> 
> iEYEAREIAAYFAlDHUbIACgkQ9CaO5/Lv0PD+kQCfQ0i2evO0hn7g0E66MlpHpy4Q
> jfsAnjOqNnKIHZRlK/zuDsr4VssZQVZR
> =josl
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
 		 	   		  

Re: NPE when starting up Embedded tomcat7

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Randy,

On 12/10/12 6:09 PM, Randy Toor wrote:
> I have an existing server which uses the Embedded class which I am 
> tasked with upgrading to tomcat7 (deprecated, but we'll be keeping
> it for the time being).

What part is deprecated? The Embedded class itself? Why not just
switch-over to using the Tomcat class? It's much easier to use and now
would be the time to make the switch.

> There were some small changes to the code to fix API breaks but
> the startup stuff is the same.  When I start up the container I am
> now seeing this -
> 
> INFO: Starting tomcat server 
> org.apache.catalina.startup.Embedded||Dec 7, 2012 12:09:49 PM
> org.apache.catalina.startup.Embedded startInternal INFO: Catalina
> naming disabled Dec 7, 2012 12:09:49 PM
> org.apache.catalina.core.StandardEngine startInternal INFO:
> Starting Servlet Engine: Apache Tomcat/7.0.32 Dec 7, 2012 12:09:49
> PM org.apache.catalina.util.LifecycleBase start INFO: The start()
> method was called on component [Realm[NullRealm]] after start() had
> already been called. The second call will be ignored. 
> org.apache.catalina.util.LifecycleBase||Dec 7, 2012 12:09:49 PM
> org.apache.catalina.util.LifecycleBase start Dec 7, 2012 12:09:49
> PM org.apache.catalina.core.ContainerBase startInternal SEVERE: A
> child container failed during start 
> java.util.concurrent.ExecutionException:
> org.apache.catalina.LifecycleException: Failed to start component
> [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]]
>
> 
at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
> at java.util.concurrent.FutureTask.get(FutureTask.java:83) at
> org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
>
> 
at
org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
> at
> org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
>
> 
at
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
> at
> org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
>
> 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
> at java.util.concurrent.FutureTask.run(FutureTask.java:138) at
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
>
> 
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> at java.lang.Thread.run(Thread.java:743) Caused by:
> org.apache.catalina.LifecycleException: Failed to start component
> [StandardEngine[OSGiEngine].StandardHost[tomcathost].StandardContext[]]
>
> 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
> ... 7 more Caused by: java.lang.NullPointerException: while trying
> to invoke the method org.apache.catalina.Service.findConnectors()
> of an object loaded from local variable 's' at
> org.apache.catalina.core.ApplicationContext.populateSessionTrackingModes(ApplicationContext.java:1191)
>
> 
at
org.apache.catalina.core.ApplicationContext.<init>(ApplicationContext.java:125)
> at
> org.apache.catalina.core.StandardContext.getServletContext(StandardContext.java:2323)
>
> 
at
org.apache.catalina.core.StandardContext.postWorkDirectory(StandardContext.java:6062)
> at
> org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5102)
>
> 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
> ... 7 more org.apache.catalina.core.ContainerBase||Dec 7, 2012
> 12:09:49 PM org.apache.catalina.core.ContainerBase startInternal
> 
> 
> I looked into that particular line in ApplicationContext and I see
> this code is new in tomcat7 -
> 
> Service s = ((Engine)
> context.getParent().getParent()).getService(); Connector[]
> connectors = s.findConnectors();
> 
> 
> Looking at the API docs for Engine, it indicates that a Service
> does not necessarily need to be associated with an Engine (from the
> docs - "Set the Service with which we are associated (if any)". Am
> I missing a step now to prevent this? Or should there be a null
> check here?

Can your post your own code that uses the Embedded class? It will
probably be easier to spot a place where you've missed something
important (or have it out of order) than to just look at the stack
trace above and try to debug Tomcat in order to determine the original
mistake.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iEYEAREIAAYFAlDHUbIACgkQ9CaO5/Lv0PD+kQCfQ0i2evO0hn7g0E66MlpHpy4Q
jfsAnjOqNnKIHZRlK/zuDsr4VssZQVZR
=josl
-----END PGP SIGNATURE-----

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