0

i have a situation that i need to communicate using eureka client with external api just after spring boot web application up and before JPA start create database schema and insert data into it from the sql file.

the problem is eureka client start registering to eureka server at smartlifecycle phase 0 , which means after application context has been started and JPA already worked and finish its job.

so how to prevent jpa to start working or delay its work to phase 1 for example ?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
AdrY
  • 29
  • 7
  • 1
    A sample project would really help us understand your problem in more detail. – Kyle Anderson May 30 '16 at 18:04
  • Why? Why do you need to communicate before the service has started? What is so important that you need to do? Also remember that service registration and lookup are different things! – M. Deinum May 30 '16 at 19:20
  • @M.Deinum : Eureka Client communicate / consult other services apis , if apis returned true , application will run and create schema and insert the data , if return false , an exception will be raised and application won't run. this is the scenario , so do u have a solution for this ? – AdrY Jun 04 '16 at 17:08

1 Answers1

0

I was facing the same problem, then I found this SO question.

Basically, you just need to put your communication logic into start().

public class EurekaClientService implements SmartLifecycle {

    @Autowired
    private EurekaClient eurekaClient;

    private volatile boolean isRunning = false;

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable r) {
        isRunning = false;
    }

    @Override
    public void start() {
        eurekaClient.customService();
        isRunning = true;
    }

    @Override
    public void stop() {
        isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public int getPhase() {
        return 1;
    }
}

Hope this help

Community
  • 1
  • 1
lok77chan
  • 55
  • 5