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

How to pass HttpRequest/Reponse as parameter to function when function is called from Quartz scheduler?

我是 运行 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()));
    }

现在,我需要使用 Quartz 调度程序每 1 小时发布一次 JSON 正文来调用此函数,而不是进行 REST 调用。可能如下图

if (context.getJobDetail().getKey().getName().equalsIgnoreCase(JobName.READRESPONSE.toString())) {
           // emailService.readMail();
            try {
              sendMail(Request, Response, id);
            } catch (IOException e) {
                e.printStackTrace();
            }

我的问题:有没有办法使用调度程序进行 REST 调用,或者是否可以通过直接传递 request/response 参数来进行 sendMail() 调用。

我不确定该怎么做,我花了大部分时间在发布之前浏览解决方案。

您可以使用 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();
     }