bits and pieces

March 13, 2011

Grails Tip: Configuring Embedded Tomcat Instance Used in Development / Test Environments

Filed under: Uncategorized — Tags: — roshandawrani @ 3:49 pm

To support quick development and testing cycles, Grails fires up an instance of embedded Tomcat at various times – say, when you do “run-app” or run the functional tests, etc.

Tomcat plugin of Grails is responsible for launching this embedded instance with the default configuration. There may come times, however, when you want to go beyond this default configuration and make specific changes – say, try different variations of the protocol it uses, play with its thread-pool settings, etc – standard Tomcat configuration that you can easily change in server.xml when you have a standalone server running.

So, how do you do it?

While the Tomcat plugin allows for some basic configuration changes through the BuildConfig script – like jvmArgs property (if you want to pass some JVM arguments), it doesn’t use / expose any server.xml, in which you can make your configuration changes. Instead what it does is that in the preStart() phase, it fires an event called ConfigureTomcat and passes it a org.apache.catalina.startup.Tomcat configuration instance that you can utilize.

You can now trap this event in your Grails application’s “scripts/_Events.groovy” script as shown below, and play with the configuration of the embedded Tomcat instance that Grails is going to fire-up.

eventConfigureTomcat = {tomcat ->
    def connector = new Connector("org.apache.coyote.http11.Http11NioProtocol")
    connector.port = System.getProperty("server.port", "8080").toInteger()
    connector.redirectPort = "8443"
    connector.protocol = "HTTP/1.1"
    connector.connectionTimeout = "20000"

    tomcat.connector = connector
    tomcat.service.addConnector connector
}
About these ads

3 Comments »

  1. [...] Dawrani tells how to adjust the configuration of Grails’ embedded Tomcat instance using the Tomcat plugin’s ConfigureTomcat [...]

    Pingback by GroovyMag - the magazine for Groovy and Grails developers about Groovy programming and Grails development — March 15, 2011 @ 2:35 am

  2. Thanks for the hint – can I use this to access the manager application? How would I do that?

    Comment by AZDave — June 13, 2011 @ 4:32 am

  3. Hi ,

    We are using Tomcatserver.groovy file to start the tomcat . however , i want to start the tomcat server on debug mode so could you pelase help me with this .

    Comment by sakshiambrish — November 7, 2013 @ 1:32 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: