我正在嘗試在事務中使用send-off
函數println
。clojure sendoff println函數
(ns com.lapots.functional.clojure.transact
(:gen-class))
(defn transfer [from to amount]
(alter
(.balance from) - amount)
(alter
(.balance from) + amount))
(defrecord Account [balance])
(defn -main [& args]
(def account1 (Account. (ref 100)))
(def account2 (Account. (ref 100)))
(def trx-agent (agent 0))
(future
(dosync
(send-off trx-agent println "T2 transfer")
(Thread/sleep 5000)
(transfer account1 account2 10)))
(dosync
(println "T1 transfer")
(transfer account1 account2 10))
(shutdown-agents)
)
如果我不喜歡這個
(println "T2 transfer")
(Thread/sleep 5000)
它會顯示消息兩倍的交易重試。所以我決定使用agents
使副作用運行println
只運行一次。
但是當我做這樣的
(send-off trx-agent println "T2 transfer")
它不會在所有打印T2 transfer
消息。問題是什麼?
哦,我明白了。基本上在調用'println transfer'之前,我等待10秒鐘(事務重試),但在提交時代理已關閉 – lapots