Skip to content

Commit

Permalink
perf($Starter): destroy SpringBootStartupHelper after it finishes work
Browse files Browse the repository at this point in the history
refine SpringBootStartupHelper logs

BREAKING CHANGE: refine SpringBootStartupHelper logs
  • Loading branch information
johnnymillergh committed Aug 27, 2021
1 parent 306d968 commit 5e4f58f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -62,9 +63,9 @@ public IpHelper ipHelper(MafProjectProperty mafProjectProperty) {

@Bean
public SpringBootStartupHelper springBootStartupHelper(MafProjectProperty mafProjectProperty,
IpHelper ipHelper) {
IpHelper ipHelper, ApplicationContext applicationContext) {
log.warn("Initial bean: '{}'", SpringBootStartupHelper.class.getSimpleName());
return new SpringBootStartupHelper(mafProjectProperty, ipHelper);
return new SpringBootStartupHelper(mafProjectProperty, ipHelper, applicationContext);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.jmsoftware.maf.reactivespringcloudstarter.helper;

import cn.hutool.core.lang.Console;
import com.jmsoftware.maf.reactivespringcloudstarter.configuration.MafProjectProperty;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

Expand All @@ -19,21 +24,33 @@
@Slf4j
@Component
@RequiredArgsConstructor
public class SpringBootStartupHelper {
public class SpringBootStartupHelper implements DisposableBean {
private static final String LINE_SEPARATOR = System.lineSeparator();
private final MafProjectProperty mafProjectProperty;
private final IpHelper ipHelper;
private final ApplicationContext applicationContext;

public void stop(@NonNull StopWatch stopWatch) {
stopWatch.stop();
log.info("🥳 Congratulations! 🎉");
log.info("🖥 {}@{} started!", mafProjectProperty.getProjectArtifactId(), mafProjectProperty.getVersion());
log.info("⚙️ Environment: {}", mafProjectProperty.getEnvironment());
log.info("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(),
stopWatch.getTotalTimeMillis());
log.info("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")), TimeZone.getDefault().getDisplayName());
log.info("{} App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/",
LINE_SEPARATOR, LINE_SEPARATOR, ipHelper.getServerPort(), mafProjectProperty.getContextPath(),
LINE_SEPARATOR, ipHelper.getPublicIp(), ipHelper.getServerPort(), mafProjectProperty.getContextPath());
Console.log("🥳 Congratulations! 🎉");
Console.log("🖥 {}@{} started!", this.mafProjectProperty.getProjectArtifactId(),
this.mafProjectProperty.getVersion());
Console.log("⚙️ Environment: {}", this.mafProjectProperty.getEnvironment());
Console.log("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(),
stopWatch.getTotalTimeMillis());
Console.log("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")),
TimeZone.getDefault().getDisplayName());
Console.error(" App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/",
LINE_SEPARATOR, this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath(),
LINE_SEPARATOR, this.ipHelper.getPublicIp(), this.ipHelper.getServerPort(),
this.mafProjectProperty.getContextPath());
val defaultListableBeanFactory =
(DefaultListableBeanFactory) this.applicationContext.getAutowireCapableBeanFactory();
defaultListableBeanFactory.destroyBean(this);
}

@Override
public void destroy() {
log.warn("Destroyed bean: {}", this.getClass().getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand Down Expand Up @@ -119,9 +120,9 @@ public IpHelper ipHelper(Environment environment) {

@Bean
public SpringBootStartupHelper springBootStartupHelper(MafProjectProperty mafProjectProperty,
IpHelper ipHelper) {
IpHelper ipHelper, ApplicationContext applicationContext) {
log.warn("Initial bean: '{}'", SpringBootStartupHelper.class.getSimpleName());
return new SpringBootStartupHelper(mafProjectProperty, ipHelper);
return new SpringBootStartupHelper(mafProjectProperty, ipHelper, applicationContext);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.jmsoftware.maf.springcloudstarter.helper;

import cn.hutool.core.lang.Console;
import com.jmsoftware.maf.springcloudstarter.configuration.MafProjectProperty;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

Expand All @@ -19,21 +24,33 @@
@Slf4j
@Component
@RequiredArgsConstructor
public class SpringBootStartupHelper {
public class SpringBootStartupHelper implements DisposableBean {
private static final String LINE_SEPARATOR = System.lineSeparator();
private final MafProjectProperty mafProjectProperty;
private final IpHelper ipHelper;
private final ApplicationContext applicationContext;

public void stop(@NonNull StopWatch stopWatch) {
stopWatch.stop();
log.info("🥳 Congratulations! 🎉");
log.info("🖥 {}@{} started!", this.mafProjectProperty.getProjectArtifactId(), this.mafProjectProperty.getVersion());
log.info("⚙️ Environment: {}", this.mafProjectProperty.getEnvironment());
log.info("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(),
Console.log("🥳 Congratulations! 🎉");
Console.log("🖥 {}@{} started!", this.mafProjectProperty.getProjectArtifactId(),
this.mafProjectProperty.getVersion());
Console.log("⚙️ Environment: {}", this.mafProjectProperty.getEnvironment());
Console.log("⏳ Deployment duration: {} seconds ({} ms)", stopWatch.getTotalTimeSeconds(),
stopWatch.getTotalTimeMillis());
log.info("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")), TimeZone.getDefault().getDisplayName());
log.info("{} App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/",
LINE_SEPARATOR, LINE_SEPARATOR, this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath(),
LINE_SEPARATOR, this.ipHelper.getPublicIp(), this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath());
Console.log("⏰ App started at {} (timezone - {})", Instant.now().atZone(ZoneId.of("UTC+8")),
TimeZone.getDefault().getDisplayName());
Console.error(" App running at{} - Local: http://localhost:{}{}/{} - Network: http://{}:{}{}/",
LINE_SEPARATOR, this.ipHelper.getServerPort(), this.mafProjectProperty.getContextPath(),
LINE_SEPARATOR, this.ipHelper.getPublicIp(), this.ipHelper.getServerPort(),
this.mafProjectProperty.getContextPath());
val defaultListableBeanFactory =
(DefaultListableBeanFactory) this.applicationContext.getAutowireCapableBeanFactory();
defaultListableBeanFactory.destroyBean(this);
}

@Override
public void destroy() {
log.warn("Destroyed bean: {}", this.getClass().getSimpleName());
}
}

0 comments on commit 5e4f58f

Please sign in to comment.