Search code examples
angularspringrestspring-bootscheduler

Can you make a rest api call from spring (Boot) if you are using it on the backend? (Using a scheduler) If so, should I use rest template?


I am doing a fantasy football site and I have ANGULAR on the front end and SPRING BOOT on the backend. Every Tuesday morning I want my scheduler to use the NFL API to retrieve the scores so I can update the database. I don't think I can use a scheduler w/ Angular? Is using a scheduler with Spring and making this API call the right approach? ALSO, if I do an API call, should I use Rest Template?

THANK YOU!


Solution

  • Since Angular is a javascript framework, your angular code will only be running if someone has the application open in their browser. If no one has their browser open on Tuesday morning then you wouldn't be able to trigger an update. Also you wouldn't want every user's browser trying to perform the same update, you only want to pull in this information once on the server.

    You can use Spring's @Scheduled annotation to create a task that runs on whatever schedule you need:

    https://www.baeldung.com/spring-scheduled-tasks

    What is best way to schedule task in spring boot application

    In your scheduled task you can use RestTemplate, or any http client you like (Jersey, commons http-client), to make the call to the NFL API.