2012-06-19 6 views
6

가능한 복제를 배포 실행 : 나는 간단한 API를 구축, compojure과 함께 Clojure의 링 미들웨어를 사용하고
How to run an arbitrary startup function in a ring project?반지/compjure 응용 프로그램 시작시 함수 후

. 저는 앱을 전쟁으로 자주 배포합니다.

위대한 작품이지만, 응용 프로그램을 시작할 때 하나의 초기화 코드가 실행되도록하는 방법을 찾고 있습니다. "lein ring server"를 실행하면 잘 돌아갑니다. 그러나 전쟁으로 배포 된 경우 첫 번째 요청이 서버에 도달 할 때 (예 : lazy)에만 실행되는 것처럼 보입니다. (AOT를 사용하지 않고) 게으르지 않도록하는 방법이 있습니까? 아니면 링 미들웨어 수명주기에 연결하는 더 좋은 방법이 있습니까?

+0

캐 노니 컬이 작업을 수행하는

는 시작 및/또는 종료하는 동안 당신이 전화하고자하는 기능을 가진 파일을 생성 여기에 답변 : "lein 링 서버"를 실행할 때 작동하며 편리한 기능을 갖춘 http://stackoverflow.com/questions/13978623/how-to-run-an-arbitrary-startup-function-in-a-ring-project –

답변

2

당신이 찾고있는 것 같아요 : init param in lein-ring plugin. 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 프로젝트에서 서블릿 유틸리티를 사용할 수 있습니다.

(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> 
관련 문제