将来不能使用javax.mail [英] future not working with javax.mail

查看:144
本文介绍了将来不能使用javax.mail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩未来,似乎无法使用 javax.mail 。例如,为了乐趣,我试图设置一个compojure处理程序来抓取一堆电子邮件,并将它们放入数据库,但在电子邮件全部收集和插入之前传递响应客户端。



我有一些 println import-posts 函数(下面),当我运行这个从repl,它工作正常,打印 142日志消息。第一次(因为db是空的) c $ c>没有新消息。第二次。当我通过运行本地开发jetty服务器尝试它时,我每次获得 println 输出 142个日志消息加载页面,他们从来没有插入到db。好吧,那么我试图从repl中导入他们到db,然后从jetty尝试它,我仍然有 142个日志消息每个时间。



compojure处理程序:



  (GET/request 
(if-let [usr_id(:id(friend / current-authentication))]
(friend / authorize
#{:pojagi.models.usr / user }

;;这是我遇到的麻烦
(do(future(let [folder(mail / get-folder mail / gmailInbox)]
println从。(.getName文件夹)导入电子邮件。)
(mail / import-posts usr_id文件夹)))
(index / home)))

b $ b(index / index(:flash request))))



/ h3>

 (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。 )]
(println(count journals)journal messages。)
(if(< (计数日志)2)
(println没有新消息)
(map
(fn [journal]
(printIn插入日志
(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) )


解决方案

你可能被懒惰的bug :周围地图包裹 doall :



<它可以帮助

 (doall(map 
(fn [journal]

dorun 如果你不想检查结果(dorun就像doall不包含整个序列,因此不太可能耗尽内存)

 (dorun(map 
fn [journal]



当您从repl运行import-posts时,repl打印映射结果在日志上的 fn [journal] ,这导致了将它们插入数据库的副作用。 当jetty运行时,没有看到结果,序列保持懒惰 - 未实现,并且副作用不会发生。


I'm playing with future and and can't seem to get it to work with javax.mail. For example, for fun, I'm trying to set up a compojure handler to grab a bunch of emails and put them into a database, but deliver a response to the client before the emails have all been gathered and inserted.

I have a few printlns going on in the import-posts function (below), and when I run this from the repl, it works fine, printing 142 journal messages. the first time (because the db is empty), and No new messages. the second time. When I try it by running the local development jetty server, I get a println output of 142 journal messages every time I load the page, and they never get inserted into the db. Okay, so then I tried to import them into the db from the repl and then try it from jetty, and I still got 142 journal messages every time. What's the best way to accomplish this?

compojure handler:

(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))))

function for importing emails:

(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) )))

解决方案

you may have been bitten by the lazy-bug:

if you wrap a doall around the map it may help

(doall (map
    (fn [journal]

or dorun if you don't want to check the result after (dorun is like doall that does not hold the entire sequence, so less likely to run out of memory)

(dorun (map
    (fn [journal]

When you run import-posts from the repl, the repl prints the result of mapping the fn [journal] over the journals, which causes the side effect of inserting them into the database. When jetty runs this nothing looks at the result, the sequence remains lazily-unrealized, and the side effects to not take place.

这篇关于将来不能使用javax.mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆