BAEL-20869 Move remaining spring boot modules

This commit is contained in:
mikr
2020-02-02 20:44:54 +01:00
parent 80a2cfec65
commit 56a9403564
704 changed files with 1303 additions and 1501 deletions

View File

@@ -0,0 +1,12 @@
package com.baeldung.compare;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ComparisonApplication {
public static void main(String[] args) {
SpringApplication.run(ComparisonApplication.class, args);
}
}

View File

@@ -0,0 +1,65 @@
package com.baeldung.compare;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Statistic;
@Component
public class StartupEventHandler {
// logger, constructor
private static Logger logger = LoggerFactory.getLogger(StartupEventHandler.class);
public StartupEventHandler(MeterRegistry registry) {
this.meterRegistry = registry;
}
private String[] METRICS = {
"jvm.memory.used",
"jvm.classes.loaded",
"jvm.threads.live"};
private String METRIC_MSG_FORMAT = "Startup Metric >> {}={}";
private MeterRegistry meterRegistry;
@EventListener
public void getAndLogStartupMetrics(
ApplicationReadyEvent event) {
Arrays.asList(METRICS)
.forEach(this::getAndLogActuatorMetric);
}
private void getAndLogActuatorMetric(String metric) {
Meter meter = meterRegistry.find(metric).meter();
Map<Statistic, Double> stats = getSamples(meter);
logger.info(METRIC_MSG_FORMAT, metric, stats.get(Statistic.VALUE).longValue());
}
private Map<Statistic, Double> getSamples(Meter meter) {
Map<Statistic, Double> samples = new LinkedHashMap<>();
mergeMeasurements(samples, meter);
return samples;
}
private void mergeMeasurements(Map<Statistic, Double> samples, Meter meter) {
meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(),
measurement.getValue(), mergeFunction(measurement.getStatistic())));
}
private BiFunction<Double, Double, Double> mergeFunction(Statistic statistic) {
return Statistic.MAX.equals(statistic) ? Double::max : Double::sum;
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.gracefulshutdown;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GracefulShutdownApplication {
public static void main(String args[]) {
SpringApplication.run(GracefulShutdownApplication.class, args);
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.gracefulshutdown.beans;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
@Component
public class LongRunningProcessBean {
private static final Logger LOG = LoggerFactory.getLogger(LongRunningProcessBean.class);
@Autowired
private TaskExecutor taskExecutor;
@PostConstruct
public void runTaskOnStartup() {
LOG.info("runTaskOnStartup entering");
for (int i = 0; i < 3; i++) {
final int processNumber = i;
taskExecutor.execute(() -> {
try {
LOG.info("Long running process {} using threadpool started", processNumber);
Thread.sleep(60_000);
LOG.info("Long running process {} using threadpool completed", processNumber);
} catch (Exception e) {
LOG.error("Error while executing task", e);
}
});
}
LOG.info("runTaskOnStartup exiting");
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.gracefulshutdown.config;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
public class SpringConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(SpringConfiguration.class);
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(2);
taskExecutor.setMaxPoolSize(2);
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(30);
taskExecutor.initialize();
return taskExecutor;
}
@PreDestroy
public void destroy() {
LOG.info("Shutdown initiated");
}
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.springbootconfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootConfigurationApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootConfigurationApplication.class, args);
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.springbootconfiguration.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingsController {
@GetMapping("/greetings/{username}")
public String getGreetings(@PathVariable("username") String userName) {
return "Hello " + userName + ", Good day...!!!";
}
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.springbootnonwebapp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 1. Act as main class for spring boot application
* 2. Also implements CommandLineRunner, so that code within run method
* is executed before application startup but after all beans are effectively created
* @author hemant
*
*/
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
private static Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class);
public static void main(String[] args) {
LOG.info("STARTING THE APPLICATION");
SpringApplication.run(SpringBootConsoleApplication.class, args);
LOG.info("APPLICATION FINISHED");
}
/**
* This method will be executed after the application context is loaded and
* right before the Spring Application main method is completed.
*/
@Override
public void run(String... args) throws Exception {
LOG.info("EXECUTING : command line runner");
for (int i = 0; i < args.length; ++i) {
LOG.info("args[{}]: {}", i, args[i]);
}
}
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.springbootsimple;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.web.servlet.support.*;
@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootTomcatApplication.class, args);
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.springbootsimple;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.stream.*;
@RestController
public class TomcatController {
@GetMapping(value = "/hello")
public Collection<String> sayHello() {
return IntStream.range(0, 10)
.mapToObj(i -> "Hello number " + i)
.collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,7 @@
management.endpoints.web.exposure.include=*
management.metrics.enable.root=true
management.metrics.enable.jvm=true
management.endpoint.restart.enabled=true
spring.datasource.jmx-enabled=false
spring.main.allow-bean-definition-overriding=true
management.endpoint.shutdown.enabled=true

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,15 @@
package com.baeldung.springbootconfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringContextTest {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.springbootsimple;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootTomcatApplicationIntegrationTest {
@Test
public void contextLoads() {
}
}