現在、同期を使用しています。電子メールを送信するための Web 呼び出し。これは、いくつかの基本的な機能を証明するための簡単な修正として行われましたが、今度は電子メールを非同期で送信する必要があります。ジョブをキューに入れ、メールを送信するようにすべてをかなり作り直しましたが、1 つの問題に遭遇しました。電子メール テンプレートに FTL を使用し、サーブレット コンテキストを FTL に渡してテンプレート フォルダーを取得する前に使用します。Spring @Scheduled ジョブによって処理されるキューに入れられたジョブでこれを行っているため、Web サーブレットにアクセスできなくなりました。私はしばらくの間、調査して遊んでいますが、実際に機能する方法を思い付いていないようです.
入手するための非常に簡単な方法があると感じています
以前に作業を行ったコードは、次のようになります。
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("someStuffHere")
@Transactional
public function someWebServiceCall(@Context javax.servlet.http.HttpServletRequest req)
{
SomeStuff foo = gotSomeStuff();
sendEmail(req.getServlet(), foo);
}
public sendEmail(Servlet context, SomeStuff foo) //<-- lives in another class somewhere, just showing how FTL uses the servlet
{
Configuration cfg = new Configuration();
cfg.setServletContextForTemplateLoading(context,"communicationTemplates/email");
}
新しいコードは次のようになります。
public class someClass
{
@Autowired
private SomeRepo someRepo;
@Scheduled(cron = "* */2 * * * ?")
public void sendAnyOutstandingStuffEmails()
{
SomeStuff foo = someRepo.getStuff();
sendEmail(/*how to get context, or a resource so FTL can get the template folder*/, foo)
}