我必须创建Java Springboot应用程序,其中我必须一次将请求发送到多个端点的时间表

发布于 2025-01-28 22:13:18 字数 377 浏览 3 评论 0原文

说明 - 我有IP地址/端口,并且在DB中保存的时间频率。

我必须创建将在计划时间内运行的服务或作业。它将从数据库读取端点,并同时发送请求到每个端点。

示例数据:

10.20.30.111/8890 and time frequency 5 min
10.30.40.112/6764 and time frequency 2 min

意味着我必须创建服务,该服务将每5分钟发送每5分钟和10.30.40.112/6764每2分钟向10.20.30.111/8890发送一次。

如何创建将根据不同时间频率运行的服务并同时发送请求到多个端点。

Explanation - I have IP address/port and thier time frequency saved in DB.

I have to create service or job that will run on scheduled time. It will read endpoints from db and send request to each endpoint at same time.

Example data:

10.20.30.111/8890 and time frequency 5 min
10.30.40.112/6764 and time frequency 2 min

Means that i have to create service that will send request to 10.20.30.111/8890 every 5 min and 10.30.40.112/6764 to every 2 min.

How can I create that Service that will run based on different time frequency and send request to multiple endpoints at same time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2025-02-04 22:13:18

您可以注入进入应用程序的配置类。假设已经(或可以)从数据库中读取(端点,频率)的所有时间表相关记录,则可以将这些记录转换为呼叫taskscheduler.scheduuleatfixedrate(转换频率到类型的间隔持续时间)。

例如,骨骼将是:

@Configuration
@EnableScheduling
public class Question72238004Config
{
    // represents the database repository for tasks FooTask
    @Autowired 
    FooTaskRepository fooTaskRepository;
    
    @Bean
    public TaskScheduler taskScheduler() {
        final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // configure task scheduler ...
        scheduler.setPoolSize(2);
        return scheduler;
    }
    
    @PostConstruct
    void setup()
    {
        final TaskScheduler scheduler = this.taskScheduler();
        // Read records of (endpoint,freq) from fooTaskRepository
        for (FooTaskEntity t: fooTaskRepository.findAll()) {
            // extract endpoint from t, build a Runnable from it
            Runnable r = ... ;
            // extract frequency and transform to Duration
            Duration d = Duration.ofMinutes(...);
            // Schedule
            scheduler.scheduleAtFixedRate(r, d);
        }
    }
}

You can inject a TaskScheduler into a configuration class of your application. Assuming that all schedule-related records of (endpoint, frequency) are already (or can be) read from the database, you can translate each of these records to a calls to TaskScheduler.scheduleAtFixedRate (converting the frequency to interval of type Duration).

For example, a skeleton would be:

@Configuration
@EnableScheduling
public class Question72238004Config
{
    // represents the database repository for tasks FooTask
    @Autowired 
    FooTaskRepository fooTaskRepository;
    
    @Bean
    public TaskScheduler taskScheduler() {
        final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        // configure task scheduler ...
        scheduler.setPoolSize(2);
        return scheduler;
    }
    
    @PostConstruct
    void setup()
    {
        final TaskScheduler scheduler = this.taskScheduler();
        // Read records of (endpoint,freq) from fooTaskRepository
        for (FooTaskEntity t: fooTaskRepository.findAll()) {
            // extract endpoint from t, build a Runnable from it
            Runnable r = ... ;
            // extract frequency and transform to Duration
            Duration d = Duration.ofMinutes(...);
            // Schedule
            scheduler.scheduleAtFixedRate(r, d);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文