2012-06-19 39 views
6

可能重复:
How to run an arbitrary startup function in a ring project?执行上环/ compjure应用程序启动功能后部署

我使用Clojure的环中间件,用的Compojure,建立一个简单的API。 我经常将应用程序部署为一场战争。

这个伟大的工程,但我在看的方式有一次性初始化代码运行的应用程序启动时。当我运行“lein ring server”时,它运行的很好 - 但是,当部署为一场战争时,它似乎只在第一个请求到达服务器时运行(即懒惰)。有没有办法让它不被懒惰(没有使用AOT) - 还是有更好的方法来挂钩环中间件生命周期?

+0

规范回答此:http://stackoverflow.com/questions/13978623/how-to-run-an-arbitrary-startup-function-in-a-ring-project –

回答

2

我认为你正在寻找:在雷音环插件初始参数。从https://github.com/weavejester/lein-ring复制:

:init - A function to be called once before your handler starts. It should take no 
arguments. If you've compiled your Ring application into a war-file, this function will 
be called when your handler servlet is first initialized. 
+0

运行时,“雷音环服务器”,是得心应手的作品然而,当作为战争部署时,该功能根本不会被调用。 –

+1

诀窍是:init函数本身应该*不在* project.clj中 –

1

一个ServletContextListener实施将满足您的需求。如果您不想用:gen-class自己实现一个,则可以在ring-java-servlet项目中使用servlet实用程序。

要做到这一点,创建启动和/或关机期间您是否愿意叫函数的文件:

(ns my.project.init 
    (:require [org.lpetit.ring.servlet.util :as util])) 

(defn on-startup [context] 
    (do-stuff (util/context-params context))) 

(defn on-shutdown [context] 
    (do-other-stuff (util/context-params context))) 

然后通过以下web.xml设置挂钩到你的web应用这样的:

<context-param> 
    <param-name>context-init</param-name> 
    <param-value>my.project.init/on-startup</param-value> 
</context-param> 
<context-param> 
    <param-name>context-destroy</param-name> 
    <param-value>my.project.init/on-shutdown</param-value> 
</context-param> 
<listener> 
    <listener-class>org.lpetit.ring.servlet.RingServletContextListener</listener-class> 
</listener> 
相关问题