-
Notifications
You must be signed in to change notification settings - Fork 53.3k
BAEL-6179: Add Actuator sample without Spring Boot #14032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## Parent Spring 5 | ||
|
|
||
| This is a parent module for all projects using Spring 5 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>parent-spring-6</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <name>parent-spring-6</name> | ||
| <packaging>pom</packaging> | ||
| <description>Parent for all spring 6 core modules</description> | ||
|
|
||
| <parent> | ||
| <groupId>com.baeldung</groupId> | ||
| <artifactId>parent-modules</artifactId> | ||
| <version>1.0.0-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-framework-bom</artifactId> | ||
| <version>${spring.version}</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-core</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <properties> | ||
| <spring.version>6.0.8</spring.version> | ||
| </properties> | ||
|
|
||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>spring-actuator</artifactId> | ||
| <name>spring-actuator</name> | ||
| <packaging>war</packaging> | ||
| <description>Demo project for Spring Boot Actuator without Spring Boot</description> | ||
|
|
||
| <parent> | ||
| <groupId>com.baeldung</groupId> | ||
| <artifactId>parent-spring-6</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <relativePath>../parent-spring-6</relativePath> | ||
| </parent> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>jakarta.servlet</groupId> | ||
| <artifactId>jakarta.servlet-api</artifactId> | ||
| <version>5.0.0</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-webmvc</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-actuator-autoconfigure</artifactId> | ||
| <version>${spring-boot.version}</version> | ||
| </dependency> | ||
| <!-- Jetty embedded --> | ||
| <dependency> | ||
| <groupId>org.eclipse.jetty</groupId> | ||
| <artifactId>jetty-servlet</artifactId> | ||
| <version>${jetty.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <pluginManagement> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-war-plugin</artifactId> | ||
| <version>3.3.2</version> | ||
| </plugin> | ||
| <!-- to run the server, use mvn jetty:run --> | ||
| <plugin> | ||
| <groupId>org.eclipse.jetty</groupId> | ||
| <artifactId>jetty-maven-plugin</artifactId> | ||
| <version>${jetty.version}</version> | ||
| </plugin> | ||
| </plugins> | ||
| </pluginManagement> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.eclipse.jetty</groupId> | ||
| <artifactId>jetty-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <properties> | ||
| <spring-boot.version>3.0.6</spring-boot.version> | ||
| <jetty.version>11.0.15</jetty.version> | ||
| </properties> | ||
|
|
||
| </project> |
26 changes: 26 additions & 0 deletions
26
spring-actuator/src/main/java/com/baeldung/spring/actuator/ActuatorConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.baeldung.spring.actuator; | ||
|
|
||
| import org.springframework.boot.actuate.health.Health; | ||
| import org.springframework.boot.actuate.health.HealthIndicator; | ||
| import org.springframework.boot.actuate.info.InfoContributor; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class ActuatorConfiguration { | ||
|
|
||
| @Bean | ||
| public HealthIndicator sampleHealthIndicator() { | ||
| return Health.up() | ||
| .withDetail("info", "Sample Health") | ||
| ::build; | ||
| } | ||
|
|
||
| @Bean | ||
| public InfoContributor provideSampleInfos() { | ||
| return builder -> | ||
| builder | ||
| .withDetail("app-title", "Actuator Sample Application"); | ||
|
|
||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
spring-actuator/src/main/java/com/baeldung/spring/actuator/AppConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.baeldung.spring.actuator; | ||
|
|
||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.context.annotation.ComponentScan; | ||
| import org.springframework.context.annotation.PropertySource; | ||
|
|
||
| @EnableAutoConfiguration | ||
| @PropertySource("classpath:application.properties") | ||
| @ComponentScan(basePackageClasses = AppConfig.class) | ||
| public class AppConfig { | ||
| } |
64 changes: 64 additions & 0 deletions
64
spring-actuator/src/main/java/com/baeldung/spring/actuator/Start.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.baeldung.spring.actuator; | ||
|
|
||
| import org.eclipse.jetty.server.Server; | ||
| import org.eclipse.jetty.servlet.ServletContextHandler; | ||
| import org.eclipse.jetty.servlet.ServletHolder; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.web.context.ContextLoaderListener; | ||
| import org.springframework.web.context.WebApplicationContext; | ||
| import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||
| import org.springframework.web.servlet.DispatcherServlet; | ||
|
|
||
| public class Start { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(Start.class); | ||
| private static final int DEFAULT_PORT = 8080; | ||
| private static final String CONTEXT_PATH = "/"; | ||
| private static final String CONFIG_LOCATION = AppConfig.class.getName(); | ||
| private static final String MAPPING_URL = "/*"; | ||
|
|
||
| /* | ||
| * This application runs a Jetty webcontainer that runs the | ||
| * Spring Dispatcher Servlet. | ||
| */ | ||
| public static void main(String[] args) throws Exception { | ||
| new Start().startJetty(getPortFromArgs(args)); | ||
| } | ||
|
|
||
| private static int getPortFromArgs(String[] args) { | ||
| if (args.length > 0) { | ||
| try { | ||
| return Integer.parseInt(args[0]); | ||
| } catch (NumberFormatException ignore) { | ||
| } | ||
| } | ||
| logger.info("No server port configured, falling back to {}", DEFAULT_PORT); | ||
| return DEFAULT_PORT; | ||
| } | ||
|
|
||
| private void startJetty(int port) throws Exception { | ||
| logger.info("Starting server at port {}", port); | ||
| Server server = new Server(port); | ||
| server.setHandler(getServletContextHandler(getContext())); | ||
| server.start(); | ||
| logger.info("Server started at port {}", port); | ||
| server.join(); | ||
| } | ||
|
|
||
| private static ServletContextHandler getServletContextHandler(WebApplicationContext context) { | ||
| ServletContextHandler contextHandler = new ServletContextHandler(); | ||
| contextHandler.setErrorHandler(null); | ||
| contextHandler.setContextPath(CONTEXT_PATH); | ||
| contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL); | ||
| contextHandler.addEventListener(new ContextLoaderListener(context)); | ||
| return contextHandler; | ||
| } | ||
|
|
||
| private static WebApplicationContext getContext() { | ||
| AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); | ||
| context.setConfigLocation(CONFIG_LOCATION); | ||
| return context; | ||
| } | ||
|
|
||
| } |
23 changes: 23 additions & 0 deletions
23
spring-actuator/src/main/java/com/baeldung/spring/actuator/WebMvcConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.baeldung.spring.actuator; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| public class WebMvcConfiguration { | ||
|
|
||
| @Bean | ||
| public WebMvcConfigurer webMvcConfigurer() { | ||
| return new WebMvcConfigurer() { | ||
| @Override | ||
| public void addViewControllers(ViewControllerRegistry registry) { | ||
| registry | ||
| .addViewController("/") | ||
| .setViewName("redirect:/actuator"); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| management.endpoints.web.exposure.include=* | ||
| management.endpoint.health.show-details=always |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spring 6