0

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?

Ivan
  • 8,508
  • 2
  • 19
  • 30
Marievi
  • 4,951
  • 1
  • 16
  • 33
  • Maybe the logs are written in different log file, e.g. catalina.out? – Ori Marko Apr 02 '19 at 13:42
  • @user7294900 where exactly do you mean? No file is produced in the folder where I am working... Also, according to this thread: https://stackoverflow.com/questions/31939849/spring-boot-default-log-location, spring boot outputs in the console... – Marievi Apr 02 '19 at 13:43
  • Since you are using `@EnableDiscoveryClient` I think your app tries to connect to discovery server which might take time – Ivan Apr 02 '19 at 13:48
  • @Ivan I have another project which is the same as this, but does not have this annotation. Still, it takes the same time... – Marievi Apr 02 '19 at 13:54
  • Do you have any other annotations on top of `BaseService`? As it currently stands, I don't see how your `BaseService` would be picked up by Spring as a bean. – g00glen00b Apr 02 '19 at 14:09
  • @g00glen00b no, but I have another class which extends `BaseService` and has the `@Component` annotation. – Marievi Apr 02 '19 at 14:12
  • I can't seem to reproduce this problem locally when creating a Spring boot 2.1.3 project using a similar setup as yours. – g00glen00b Apr 02 '19 at 14:18
  • The whole project is being deployed inside a VM created with `virt-install`... In my host machine I don't face this problem either, only inside the VM, but I didn't mention it because it has plenty of resources and I thought that it just happened to run fine in my host... – Marievi Apr 02 '19 at 14:27
  • @Marievi Very difficult for others to troubleshoot then. The first thing that comes to mind is this issue: https://stackoverflow.com/q/39636792/1915448. I can't guarantee that it's the same issue though. The first answer at least describes a similar situation as yours, where startup times could increase from 15 seconds to 6 minutes. – g00glen00b Apr 03 '19 at 07:32
  • Reminds me of an issue with the JDK's PRNG provider: https://stackoverflow.com/a/38944130/1770433 – mle Apr 03 '19 at 17:05
  • @mle Eventually I solved it with secure random. Thanks! – Marievi Apr 04 '19 at 07:21
  • Shall I elaborate a little bit more about this pitfall and provide an answer for everyone here? – mle Apr 04 '19 at 07:28
  • @mle of course! – Marievi Apr 04 '19 at 07:49
  • @Marievi: elabrated a little more on my last comment and added it as answer, feel free to accept or upvote if it was helpful for you. – mle Apr 04 '19 at 17:32

1 Answers1

2

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.

mle
  • 2,466
  • 1
  • 19
  • 25