为了账号安全,请及时绑定邮箱和手机立即绑定

从 Quartz 调度程序调用函数时,如何将 HttpRequest/Reponse 作为参数传递给

从 Quartz 调度程序调用函数时,如何将 HttpRequest/Reponse 作为参数传递给

撒科打诨 2023-05-17 16:10:40
我正在运行 spring-boot 应用程序,我已经将 quartz 调度程序实现为应用程序的一部分。早些时候我有一个带有端点的休息控制器,例如http://localhost:8080/GoogleMail/ {id} 它触发如下所示的函数并接受 HttpServletRequest/Response 作为参数以及我传递的 Pathvariable。@PostMapping(value = "/GoogleMail/{id}", consumes = "application/json", produces = "application/json")    public String sendMail(HttpServletRequest request, HttpServletResponse response, @Valid @PathVariable(value = "id") String id,            @Valid @RequestBody MailMessage mailMsg) throws Exception {        if(id == null || id.isEmpty()) {            ResponseEntity.badRequest().build();        }        this.userId = id;        return GoogleMailIntegrationService.sendUserMails(request, response, id, mailMsg,                m -> !StringUtils.isBlank(mailMsg.getTo())                && !StringUtils.isBlank(mailMsg.getSubject())                && !StringUtils.isBlank(mailMsg.getBody()));    }现在不是进行 REST 调用,而是需要使用 Quartz 调度程序每 1 小时发布一次 JSON 正文来调用此函数。可能如下图if (context.getJobDetail().getKey().getName().equalsIgnoreCase(JobName.READRESPONSE.toString())) {           // emailService.readMail();            try {              sendMail(Request, Response, id);            } catch (IOException e) {                e.printStackTrace();            }我的问题:有没有办法使用调度程序进行 REST 调用,或者是否可以通过直接传递请求/响应参数来进行 sendMail() 调用。我不确定如何执行此操作,我花了大部分时间在发布之前浏览解决方案。
查看完整描述

1 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

您可以使用RestTemplate通过以下方式对某个控制器端点进行请求调用:


if (context.getJobDetail().getKey().getName().equalsIgnoreCase(JobName.READRESPONSE.toString())) {

     // emailService.readMail();

     try {

         RestTemplate restTemplate = new RestTemplate();

         HttpEntity<MailMessage > request = new HttpEntity<>(mailMsg, new HttpHeaders());


         ResponseEntity<String> responseEntityStr = 

             restTemplate.postForEntity(

             String.format("http://localhost:7777/GoogleMail/%s", id), 

             request, String.class);


     } catch (IOException e) {

            e.printStackTrace();

     }


查看完整回答
反对 回复 2023-05-17
  • 1 回答
  • 0 关注
  • 98 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信