October 22, 2024
Chicago 12, Melborne City, USA
java

To develop scheduling rest client


I am trying to develop scheduling rest client.
The task is:

  1. There is Rest API
  2. I would like to send GET requests on schedule

I created the Spring boot application:

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);
    }

}

I created the scheduler config:

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 { }

I created java class which send requests to rest API:

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 
    }
}

Finally, I created java class which initiates request sending, as I am expecting:

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();
    }
}

But it doesn’t work. The error is:

java.lang.NullPointerException: Cannot invoke GetAllData.get()" because "TestEngine.getAllData" is null

Could you help me please to find out how to run schedule sending requests to REST API?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video