我正在玩future
并且似乎无法让它与javax.mail
. 例如,为了好玩,我试图设置一个复合处理程序来抓取一堆电子邮件并将它们放入数据库中,但在所有电子邮件被收集和插入之前向客户提供响应。
println
我在函数(下面)中有几个s import-posts
,当我从 repl 运行它时,它工作正常,142 journal messages.
第一次打印(因为 db 是空的),No new messages.
第二次打印。当我通过运行本地开发码头服务器进行尝试时,每次加载页面时都会得到println
输出,并且它们永远不会插入到数据库中。142 journal messages
好的,然后我尝试将它们从 repl 导入到数据库中,然后从码头尝试,我仍然142 journal messages
每次都得到。实现这一目标的最佳方法是什么?
复合处理程序:
(GET "/" request
(if-let [usr_id (:id (friend/current-authentication))]
(friend/authorize
#{:pojagi.models.usr/user}
;; this is where I'm having trouble
(do (future (let [folder (mail/get-folder mail/gmail "Inbox")]
(println "importing emails from " (.getName folder) ".")
(mail/import-posts usr_id folder)))
(index/home)))
(index/index (:flash request))))
导入邮件功能:
(defn import-posts
[usr_id ^javax.mail.Folder folder & {:keys [subject-term public?]}]
(let [messages (get-latest-messages usr_id folder)
journals (into [] (.search folder (SubjectTerm. (or subject-term "journal")) messages))]
(println (count journals) "journal messages.")
(if (< (count journals) 2)
(println "No new messages.")
(map
(fn [journal]
(println "inserting journal" (.getSubject journal))
(let [[title created bodyparts uid :as post] [(.getSubject journal)
(-> journal .getSentDate .getTime (java.sql.Timestamp.))
(-> journal .getContent)
(-> folder (.getUID journal))]
body (-> (.getBodyPart bodyparts 0)
.getDataHandler .getInputStream slurp md/md-to-html-string)]
(post/insert {:title title :body body :created created :usr_id 4 :public true :email_uid uid})
))
journals) )))