Marievi
Marievi

Reputation: 5001

Big delay until SpringApplication.run() method completes

I have the following class:

package org.edgexfoundry.pkg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
@EnableDiscoveryClient
public class Application {
  public static ConfigurableApplicationContext ctx;

  public static void main(String[] args) {
    ctx = SpringApplication.run(Application.class, args);
    System.out.println("WELCOME!");
  }

and in another class of the same project I have:

@ImportResource("spring-config.xml")
public class BaseService {
    @PostConstruct
    private void postConstructInitialize() {
        logger.debug("post construction initialization");
    }
}

where my spring-config.xml file is:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder
        location="classpath:*.properties" />

    <bean class="org.edgexfoundry.pkg.HeartBeat" />
    <context:component-scan base-package="org.edgexfoundry.pkg" />
    <context:component-scan base-package="org.edgexfoundry" />

</beans> 

and the HeartBeat class:

@EnableScheduling
public class HeartBeat {

    private static final EdgeXLogger logger = EdgeXLoggerFactory.getEdgeXLogger(HeartBeat.class);

    @Scheduled(fixedRateString = "${heart.beat.time}")
    public void pulse() {
        logger.info("Beating...");
    }
}

When I run the project, I get the following output:

BySpringCGLIB$$1088c4ff] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
/*******************************************************************************
 * Copyright 2017, Dell, Inc.  All Rights Reserved.
 ******************************************************************************/
WELCOME!

2019-04-02 12:50:24.574  INFO 1 --- [           main] org.edgexfoundry.pkg.Application         : No active profile set, falling back to default profiles: default

and after a long time, (about 8 minutes), I get the last line:

2019-04-02 12:57:04.611 DEBUG 1 --- [           main] org.edgexfoundry.pkg.BaseService         : post construction initialization

What is being done in the meantime? Why does the @PostConstruct method need so much time to run i.e. why does SpringApplication.run() method finish so late? Any ideas?

Upvotes: 0

Views: 181

Answers (1)

mle
mle

Reputation: 2542

One reason for a apparently slow startup is the default implementation of SecureRandom which scans network interfaces to provide an additional source of system entropy.

One can avoid this by registering an own java.security.Provider or by using the SecureRandomSpi.

Amongst others there is also this good introduction in the topic of (fast) and secure pseudo random number generation in Java.

Upvotes: 2

Related Questions