2014-05-02 45 views
0

我正在學習Hystrix和Clojure,不明白如何(正確)在Clojure中設置Hystrix命令的超時時間。如何在Clojure中的Hystrix命令上設置超時?

我搜索了StackOverflow和web更普遍。我查看了Hystrix的Clojure包裝源代碼(https://github.com/Netflix/Hystrix/blob/master/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj)。有一個init-fn函數參數看起來很有希望,但評論似乎表明這不會是一個可持續的解決方案。但這會是一個簡單的開始?

我有一個可笑的簡單蝟命令Clojure中運行,並希望得到幫助擴展這個設置,也就是說,一個200ms的超時:

(ns hystrix-timeout.core 
    (:require [clojure.string :as str]) 
    (:require [com.netflix.hystrix.core :as hystrix]) 
    (:gen-class)) 


(defn my-primary [a] 
    (if (= a true) (throw (Exception. "Primary failed")) (str "primary: " a))) 

(defn my-fallback [a] 
    (str "fallback: " a)) 

(hystrix/defcommand my-command 
    {:hystrix/fallback-fn my-fallback} 
    [a] 
    (my-primary a)) 


(defn -main 
    "Executes a simple Hystrix command. Will use a timeout when I know how to do this in Clojure." 
    [& args] 

    (println (my-command false)) 
    (println (my-command true)) 

    (System/exit 0) ; Exit explicitly as Hystrix threads are still running. 
) 

我已經把我的雷音項目最多的https://github.com/oliverbaier/learning/tree/master/hystrix-timeout在這種情況下,使答覆更容易。

非常感謝, 奧利弗

+0

這應該引導你在正確的方向:http://stackoverflow.com/questions/6694530/executing-a-function-with-a-timeout/6697356#6697356 –

回答

0

最簡單的途徑是隻使用系統屬性。您可以設置爲全局默認:

(System/setProperty "hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds" "200") 

或指令中的規定值:

(System/setProperty "hystrix.command.my-command.execution.isolation.thread.timeoutInMilliseconds" "200") 

您可以在-main方法執行此。如果你正在運行一個真正的web-app sans -main,你可以在project.clj中添加一個ring init:ring {:handler your-handler:init your-config-method}你的配置方法將在啓動時被調用。