`
MafiaDada
  • 浏览: 25076 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Run a Java web application within grails

阅读更多
Ever needed to run an existing java web application side by side with your grails project? I found myself in exactly that position this week, and discovered that it (like most things in grails) was rather simple to accomplish. Considering I work in a predominately Java environment, I often need to run a java web application side by side with my grails work. To date, I would simply run grails in the embedded tomcat engine (or Jetty in grails <1.2) bundled with grails, and use a local install of tomcat to power my java web apps.

I wanted to see if I could streamline the environment, and have my locally installed tomcat run my grails application. It is possible to use the excellent tomcat plugin to export a WAR of your project to an external tomcat instance via tomcat's manager application. The problem with this configuration is that you lose the "hot-deploy" features of grails which was a deal breaker for me.

After quite a bit of head scratching, googling, and yelling at my computer, I finally got pointed in the right direction from Graeme Rocher via the grails-user mailing list. If you are not aware, grails uses tomcat's embedded version under the hood as of 1.2. I was aware that grails emits numerous lifecycle events that you can hook into, but was unaware that the tomcat plugin uses this system to announce a "TomcatConfigured" event that exposes the underlying tomcat API. During startup of your grails project, the tomcat plugin will configure the embedded engine, and then announce the "TomcatConfigured" event, and pass along the newly configured tomcat. This can be used to manage and configure every aspect of the tomcat engine.

My current implementation supports only one specific web application. In the future, I plan on updating this code to support multiple applications which will be configurable via Config.groovy, which should be rather trivial.

Begin by adding your project specific configuration to Config.groovy:

grails.myproject.contextRoot = "/myproject"
grails.myproject.build.path = "/myproject/WEB-INF/classes"
grails.myproject.web.root = "/myproject/"


Next we need to add the event handler to our Grails application. This is done by simply creating a new file named _Events.groovy and placing it in the /scripts directory of your project.

import org.apache.catalina.*
import org.apache.catalina.connector.*
import org.apache.catalina.loader.WebappLoader
import org.codehaus.groovy.grails.commons.ConfigurationHolder

eventConfigureTomcat = {tomcat ->
println "### Starting load of custom application"
def contextRoot = ConfigurationHolder.config.grails.myproject.contextRoot
def buildroot= ConfigurationHolder.config.grails.myproject.build.path
def webroot  = ConfigurationHolder.config.grails.myproject.web.root

File appDir = new File(webroot);
context = tomcat.addWebapp(contextRoot, appDir.getAbsolutePath());
context.reloadable = true

WebappLoader loader = new WebappLoader(tomcat.class.classLoader)

loader.addRepository(new File(buildroot).toURI().toURL().toString());
context.loader = loader
loader.container = context

println "### Ending load of custom application"
}


You can now start you application with the grails run-app command, and your java web application will be available under the context you set in Config.groovy.

As a side note, if you need to support having an apache web server sit in fron of your application, you can add the following to turn on AJP connections for the embedded tomcat container.

// enable AJP to allow apache to front tomcat
def ajpConnector = new Connector("org.apache.jk.server.JkCoyoteHandler")
ajpConnector.port = 8009
ajpConnector.setProperty("redirectPort", "8443")
ajpConnector.setProperty("protocol", "AJP/1.3")
ajpConnector.setProperty("enableLookups", "false")

tomcat.service.addConnector ajpConnector


Add this part right after the "context.reloadable = true" line.

注: grails内部自带的tomcat没有server.xml文件。

原文地址:
http://krollrich.blogspot.com/2009/12/run-java-web-application-within-grails.html

分享到:
评论

相关推荐

    Grails 1.1 Web Application Development

    Grails 1.1 Web Application Development.pdf

    Java程序员使用Grails的十大优势

    Web开发方面没有王者,PHP市场很大,但RoR,Python,Java,及至Asp.net都占据着一方市场.那么作为Java程序员,当你不想使用SSH做网站的时候,尝试一下Grails吧,这里是Java程序员使用Grails的十大优势

    grails快速开发web

    grails快速开发web.pdf

    使用Grails快速开发Web应用

    Grails开发Web应用的入门建立兴趣的最佳教程。

    Grails技术精解与Web开发实践.part2

    自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。 我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。

    Grails技术精解与Web开发实践.part1

    自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。...我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。

    java中的Grails开源框架 - 使用指南(chm)

    像Rails,Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和...

    Grails1.1中文文档(CHM)

    Grails1.1最新 中文 文档 当今的Java Web开发技术...Grails建立在这些概念之上,它极大地降低了在Java平台上建立Web应用的复杂性。与那些框架不同的是,Grails是构建在现有的像Spring、Hibernate这样的Java技术之上。

    Grails1.1中文文档

    Grails建立在这些概念之上,它极大地降低了在Java平台上建立Web应用的复杂性。与那些框架不同的是,Grails是构建在现有的像Spring、Hibernate这样的Java技术之上。 Grails是个一栈式开发框架,它尝试通过核心技术...

    Grails企业web应用开发与部署.pdf

    Grails企业web应用开发与部署.pdf 很好的资源

    Grails Grails Grails

    Grails Grails Grails Grails Grails

    Getting Started with Grails Second Edition

    Grails is a Java- and Groovy-based web framework that is built for speed. First-time developers are amazed at how quickly you can get a page-centric MVC web site up and running thanks to the ...

    Grails企业web应用开发与部署

    关于grails企业web开发环境和部署的一些经验,和大家分享。

    Grails 教程

    像Rails,Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和...

    Grails: A Quick-Start Guide

    Grails is a full stack web development framework that enables you to build complete web applications in a fraction of the time and with less code than other frameworks. In Grails: A Quick-Start Guide...

    GrailsWeb应用框架grails-core

    Grails Web 应用框架:grails-core

    grails 1.0.4

    Grails专为下一代JavaWeb应用程序而设计的框架,其借助于Groovy动态语言,使Web开发变得简单而方便。Grails尽量为更多现有的Java项目创建一个全面的框架(不仅局限于视图处理),这和当前一些Java框架提供给用户的一...

    Grails+快速开发+Web+应用程序.pdf

    本教程介绍 Grails,这是一个搭建在动态语言 Groovy 之上的开源 MVC 快速 Web 开发框架。使用 Grails 可以提高 Web 开发的效率,降低 Web 开发的复杂度。本文 从 Grails 自动生成代码入手,以示例为中心逐步增加深度...

    grails项目配置

    grails演示项目,配置了一对多,多对对模型配置,配置了Mysql,Sqlserver数据库

Global site tag (gtag.js) - Google Analytics