Задача:
- Есть Rest API
- Я хотел бы отправлять GET-запросы по расписанию
Код: Выделить всё
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class IntegrationApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(IntegrationApplication.class, args);
}
}
Код: Выделить всё
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@Configuration
@EnableScheduling
@EnableAsync
@ConditionalOnProperty(name = "scheduler.enabled", matchIfMissing = true)
public class SchedulerConfig { }
Код: Выделить всё
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Service
public class GetAllData {
@Value("${server.url}")
private String serverUrl;
@Value("${security.oauth2.client.clientSecret}")
private String clientSecret;
private static RestClientGetExecute restClientGetExecute;
public void get() {
// Do send request
}
}
Код: Выделить всё
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.ZonedDateTime;
@Component
public class TestEngine {
private static GetAllData getAllData;
@Scheduled(fixedDelay = 2000)
public void getLocalDateTimeFixedDelay() throws InterruptedException {
getAllData.get();
}
}
Код: Выделить всё
java.lang.NullPointerException: Cannot invoke GetAllData.get()" because "TestEngine.getAllData" is null
Подробнее здесь: https://stackoverflow.com/questions/791 ... est-client
Мобильная версия