如何在Clojure中设置Hystrix命令的超时?

如何在Clojure中设置Hystrix命令的超时?,clojure,timeout,Clojure,Timeout,我正在学习Hystrix和Clojure,不知道如何(正确地)在Clojure中设置Hystrix命令的超时 我更广泛地搜索了StackOverflow和web。我查看了Hystrix的Clojure包装器源代码()。有一个init fn函数参数看起来很有希望,但评论似乎表明这不是一个可持续的解决方案。但这会是一个简单的开始吗 我在Clojure中运行了一个非常简单的Hystrix命令,如果能将其扩展到设置(比如)200毫秒超时,我将不胜感激: (ns hystrix-timeout.core

我正在学习Hystrix和Clojure,不知道如何(正确地)在Clojure中设置Hystrix命令的超时

我更广泛地搜索了StackOverflow和web。我查看了Hystrix的Clojure包装器源代码()。有一个init fn函数参数看起来很有希望,但评论似乎表明这不是一个可持续的解决方案。但这会是一个简单的开始吗

我在Clojure中运行了一个非常简单的Hystrix命令,如果能将其扩展到设置(比如)200毫秒超时,我将不胜感激:

(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.
)
我已经把我的lein项目放在了,以防这让回答更容易

非常感谢,
Oliver

最简单的方法就是只使用系统属性。您可以设置全局默认值:

(System/setProperty "hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds" "200")
或命令特定的值:

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

可以在-main方法中执行此操作。如果您正在运行一个真正的web应用程序sans-main,您可以在project.clj:ring{:handler您的handler:init您的config method}中添加一个ring init。启动时将调用您的config method。

这将引导您朝着正确的方向前进: