是否可以从任务调用的 Spring 服务中获取 Web 应用程序的完整 url? [英] Is possible to get web application full url from Spring service that is invoked by task?

查看:54
本文介绍了是否可以从任务调用的 Spring 服务中获取 Web 应用程序的完整 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务不是由用户请求调用的,而是由任务调用的.现在我需要从此任务发送电子邮件,并且电子邮件包含指向我的 Web 应用程序的链接,如何获取我的应用程序的完整 url,以便我的电子邮件可以链接回我的 Web 应用程序?

I have service which is not invoked by user request, but by task. Now I need to send email from this task, and email contains link to my web application, how can I get full url of my application so that my email can link back to my web application?

服务示例:

@Service
public class MyService {

    @Scheduled(cron="0 */10 * * * *")
    @Transactional
    public void myTask() throws IOException, ParseException
    {
        // Now I have to send email with my app url, but how can I get it here ?
    }

}

推荐答案

您不能这样做,因为 Web 应用程序实际上并不知道自己的域名.它只能从该请求中提取用于发送请求的域名.

You cannot do it, since web application actually doesn't know its own domain name. It can only extract a domain name used to send a request from that request.

因此您的应用程序的域名应该是配置的一部分,您的任务应该从那里获取它.

Therefore domain name of your application should be a part of configuration, and your task should obtain it from there.

这篇关于是否可以从任务调用的 Spring 服务中获取 Web 应用程序的完整 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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