Merge branch 'master' into lor6-patch-12

This commit is contained in:
Loredana Crusoveanu 2019-05-12 20:28:37 +03:00 committed by GitHub
commit f1be660ddf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 53 additions and 50 deletions

View File

@ -0,0 +1,3 @@
## Relevant Articles
- [Extending an Arrays Length](https://www.baeldung.com/java-array-add-element-at-the-end)

View File

@ -3,4 +3,5 @@
## Core Java 8 Cookbooks and Examples (part 2) ## Core Java 8 Cookbooks and Examples (part 2)
### Relevant Articles: ### Relevant Articles:
- [Anonymous Classes in Java](http://www.baeldung.com/) - [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
- [Run JAR Application With Command Line Arguments](https://www.baeldung.com/java-run-jar-with-arguments)

View File

@ -20,7 +20,6 @@
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar) - [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation) - [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api) - [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api)
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new) - [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams) - [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional) - [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional)
@ -31,4 +30,4 @@
- [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity) - [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity)
- [Java 9 Platform Module API](https://www.baeldung.com/java-9-module-api) - [Java 9 Platform Module API](https://www.baeldung.com/java-9-module-api)
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api) - [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
- [Filtering a Stream of Optionals in Java](https://www.baeldung.com/java-filter-stream-of-optional)

View File

@ -40,3 +40,4 @@
- [How to Write to a CSV File in Java](https://www.baeldung.com/java-csv) - [How to Write to a CSV File in Java](https://www.baeldung.com/java-csv)
- [List Files in a Directory in Java](https://www.baeldung.com/java-list-directory-files) - [List Files in a Directory in Java](https://www.baeldung.com/java-list-directory-files)
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes) - [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
- [Introduction to the Java NIO Selector](https://www.baeldung.com/java-nio-selector)

View File

@ -0,0 +1,3 @@
## Relevant Articles
- [Void Type in Java](https://www.baeldung.com/java-void-type)

View File

@ -51,3 +51,4 @@
- [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators) - [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators)
- [Guide to Creating and Running a Jar File in Java](https://www.baeldung.com/java-create-jar) - [Guide to Creating and Running a Jar File in Java](https://www.baeldung.com/java-create-jar)
- [Making a JSON POST Request With HttpURLConnection](https://www.baeldung.com/httpurlconnection-post) - [Making a JSON POST Request With HttpURLConnection](https://www.baeldung.com/httpurlconnection-post)
- [Convert Hex to ASCII in Java](https://www.baeldung.com/java-convert-hex-to-ascii)

View File

@ -0,0 +1,3 @@
## Relevant Articles
- [Java 9 Migration Issues and Resolutions](https://www.baeldung.com/java-9-migration-issue)

View File

@ -1,2 +1,3 @@
## Relevant Articles: ## Relevant Articles:
- [Converting Between LocalDate and XMLGregorianCalendar](https://www.baeldung.com/java-localdate-to-xmlgregoriancalendar) - [Converting Between LocalDate and XMLGregorianCalendar](https://www.baeldung.com/java-localdate-to-xmlgregoriancalendar)
- [Convert Time to Milliseconds in Java](https://www.baeldung.com/java-time-milliseconds)

View File

@ -1,3 +1,4 @@
## Relevant Articles ## Relevant Articles
- [Java Localization Formatting Messages](https://www.baeldung.com/java-localization-messages-formatting) - [Java Localization Formatting Messages](https://www.baeldung.com/java-localization-messages-formatting)
- [Check If a String Contains a Substring](https://www.baeldung.com/java-string-contains-substring)

View File

@ -3,4 +3,5 @@
- [A Guide to jBPM with Java](https://www.baeldung.com/jbpm-java) - [A Guide to jBPM with Java](https://www.baeldung.com/jbpm-java)
- [Guide to Classgraph Library](https://www.baeldung.com/classgraph) - [Guide to Classgraph Library](https://www.baeldung.com/classgraph)
- [Create a Java Command Line Program with Picocli](https://www.baeldung.com/java-picocli-create-command-line-program)

View File

@ -32,8 +32,8 @@
</dependencies> </dependencies>
<properties> <properties>
<spring-boot.version>2.0.1.RELEASE</spring-boot.version> <spring-boot.version>2.1.2.RELEASE</spring-boot.version>
<spring-cloud-dependencies.version>Finchley.SR2</spring-cloud-dependencies.version> <spring-cloud-dependencies.version>Greenwich.RELEASE</spring-cloud-dependencies.version>
</properties> </properties>
</project> </project>

View File

@ -19,12 +19,10 @@
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring-cloud-starter-eureka.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -18,23 +18,19 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring-cloud-starter-feign.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring-cloud-starter-eureka.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring-boot.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
@ -49,8 +45,4 @@
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<properties>
<spring-cloud-eureka-client.version>1.0.0-SNAPSHOT</spring-cloud-eureka-client.version>
</properties>
</project> </project>

View File

@ -19,7 +19,6 @@
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>${spring-cloud-starter-eureka.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -8,10 +8,10 @@
<packaging>war</packaging> <packaging>war</packaging>
<parent> <parent>
<artifactId>parent-boot-1</artifactId> <artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-1</relativePath> <relativePath>../parent-boot-2</relativePath>
</parent> </parent>
<dependencies> <dependencies>
@ -129,6 +129,7 @@
<dependency> <dependency>
<groupId>org.javassist</groupId> <groupId>org.javassist</groupId>
<artifactId>javassist</artifactId> <artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
@ -287,6 +288,7 @@
<!-- util --> <!-- util -->
<guava.version>19.0</guava.version> <guava.version>19.0</guava.version>
<commons-lang3.version>3.5</commons-lang3.version> <commons-lang3.version>3.5</commons-lang3.version>
<javassist.version>3.25.0-GA</javassist.version>
<!-- Maven plugins --> <!-- Maven plugins -->
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version> <cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>

View File

@ -17,7 +17,7 @@ public abstract class AbstractService<T extends Serializable> implements IOperat
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public T findOne(final long id) { public T findOne(final long id) {
return getDao().findOne(id); return getDao().findById(id).orElse(null);
} }
// read - all // read - all

View File

@ -7,7 +7,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.context.request.RequestContextListener; import org.springframework.web.context.request.RequestContextListener;

View File

@ -6,13 +6,13 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration @Configuration
@ComponentScan("org.baeldung.web") @ComponentScan("org.baeldung.web")
@EnableWebMvc @EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter { public class WebConfig implements WebMvcConfigurer {
public WebConfig() { public WebConfig() {
super(); super();
@ -29,7 +29,6 @@ public class WebConfig extends WebMvcConfigurerAdapter {
// API // API
@Override @Override
public void addViewControllers(final ViewControllerRegistry registry) { public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/graph.html"); registry.addViewController("/graph.html");
registry.addViewController("/homepage.html"); registry.addViewController("/homepage.html");
} }

View File

@ -6,16 +6,18 @@ import java.util.Date;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
@Service @Service
public class ActuatorMetricService implements IActuatorMetricService { public class ActuatorMetricService implements IActuatorMetricService {
@Autowired @Autowired
private MetricReaderPublicMetrics publicMetrics; private MeterRegistry publicMetrics;
private final List<ArrayList<Integer>> statusMetricsByMinute; private final List<ArrayList<Integer>> statusMetricsByMinute;
private final List<String> statusList; private final List<String> statusList;
@ -68,7 +70,7 @@ public class ActuatorMetricService implements IActuatorMetricService {
private void exportMetrics() { private void exportMetrics() {
final ArrayList<Integer> lastMinuteStatuses = initializeStatuses(statusList.size()); final ArrayList<Integer> lastMinuteStatuses = initializeStatuses(statusList.size());
for (final Metric<?> counterMetric : publicMetrics.metrics()) { for (final Meter counterMetric : publicMetrics.getMeters()) {
updateMetrics(counterMetric, lastMinuteStatuses); updateMetrics(counterMetric, lastMinuteStatuses);
} }
@ -83,17 +85,17 @@ public class ActuatorMetricService implements IActuatorMetricService {
return counterList; return counterList;
} }
private void updateMetrics(final Metric<?> counterMetric, final ArrayList<Integer> statusCount) { private void updateMetrics(final Meter counterMetric, final ArrayList<Integer> statusCount) {
String status = ""; String status = "";
int index = -1; int index = -1;
int oldCount = 0; int oldCount = 0;
if (counterMetric.getName().contains("counter.status.")) { if (counterMetric.getId().getName().contains("counter.status.")) {
status = counterMetric.getName().substring(15, 18); // example 404, 200 status = counterMetric.getId().getName().substring(15, 18); // example 404, 200
appendStatusIfNotExist(status, statusCount); appendStatusIfNotExist(status, statusCount);
index = statusList.indexOf(status); index = statusList.indexOf(status);
oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index); oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index);
statusCount.set(index, counterMetric.getValue().intValue() + oldCount); statusCount.set(index, (int)((Counter) counterMetric).count() + oldCount);
} }
} }

View File

@ -6,20 +6,18 @@ import java.util.Date;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.search.Search;
@Service @Service
public class CustomActuatorMetricService implements ICustomActuatorMetricService { public class CustomActuatorMetricService implements ICustomActuatorMetricService {
private MetricRepository repo;
@Autowired @Autowired
private CounterService counter; private MeterRegistry registry;
private final List<ArrayList<Integer>> statusMetricsByMinute; private final List<ArrayList<Integer>> statusMetricsByMinute;
private final List<String> statusList; private final List<String> statusList;
@ -27,7 +25,6 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService
public CustomActuatorMetricService() { public CustomActuatorMetricService() {
super(); super();
repo = new InMemoryMetricRepository();
statusMetricsByMinute = new ArrayList<ArrayList<Integer>>(); statusMetricsByMinute = new ArrayList<ArrayList<Integer>>();
statusList = new ArrayList<String>(); statusList = new ArrayList<String>();
} }
@ -36,9 +33,10 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService
@Override @Override
public void increaseCount(final int status) { public void increaseCount(final int status) {
counter.increment("status." + status); String counterName = "counter.status." + status;
if (!statusList.contains("counter.status." + status)) { registry.counter(counterName).increment(1);
statusList.add("counter.status." + status); if (!statusList.contains(counterName)) {
statusList.add(counterName);
} }
} }
@ -78,17 +76,16 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService
@Scheduled(fixedDelay = 60000) @Scheduled(fixedDelay = 60000)
private void exportMetrics() { private void exportMetrics() {
Metric<?> metric;
final ArrayList<Integer> statusCount = new ArrayList<Integer>(); final ArrayList<Integer> statusCount = new ArrayList<Integer>();
for (final String status : statusList) { for (final String status : statusList) {
metric = repo.findOne(status); Search search = registry.find(status);
if (metric != null) { if (search != null) {
statusCount.add(metric.getValue().intValue()); Counter counter = search.counter();
repo.reset(status); statusCount.add(counter != null ? ((int) counter.count()) : 0);
registry.remove(counter);
} else { } else {
statusCount.add(0); statusCount.add(0);
} }
} }
statusMetricsByMinute.add(statusCount); statusMetricsByMinute.add(statusCount);
} }

View File

@ -1,3 +1,3 @@
server.port=8082 server.port=8082
server.context-path=/spring-rest-full server.servlet.context-path=/spring-rest-full
endpoints.metrics.enabled=true endpoints.metrics.enabled=true