【问题标题】:Executing a function on ring/compjure application startup after deploy [duplicate]部署后在环/compjure应用程序启动时执行功能[重复]
【发布时间】:2012-06-21 09:19:24
【问题描述】:

可能重复:
How to run an arbitrary startup function in a ring project?

我正在使用带有 compojure 的 clojure ring 中间件来构建一个简单的 api。 我经常将应用部署为战争。

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

【问题讨论】:

标签: clojure compojure ring


【解决方案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 设置将其挂接到您的网络应用:

<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>

【讨论】:

    【解决方案2】:

    我认为您正在寻找 :init lein-ring 插件中的参数。复制自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.
    

    【讨论】:

    • 在运行“lein ring server”时工作并且很方便,但是当部署为战争时,根本不会调用该函数。
    • 诀窍是 :init 函数本身应该在 project.clj
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多