commit
197b4eede0
|
|
@ -48,6 +48,7 @@ import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
import org.springframework.context.event.GenericApplicationListener;
|
import org.springframework.context.event.GenericApplicationListener;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
|
import org.springframework.core.log.LogMessage;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -107,17 +108,9 @@ public class LocalDevToolsAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
ApplicationListener<ClassPathChangedEvent> restartingClassPathChangedEventListener(
|
RestartingClassPathChangeChangedEventListener restartingClassPathChangedEventListener(
|
||||||
FileSystemWatcherFactory fileSystemWatcherFactory) {
|
FileSystemWatcherFactory fileSystemWatcherFactory) {
|
||||||
return (event) -> {
|
return new RestartingClassPathChangeChangedEventListener(fileSystemWatcherFactory);
|
||||||
if (event.isRestartRequired()) {
|
|
||||||
if (restarterLogger.isDebugEnabled()) {
|
|
||||||
restarterLogger.debug(
|
|
||||||
"Application restart required due to the following changes: " + event.getChangeSet());
|
|
||||||
}
|
|
||||||
Restarter.getInstance().restart(new FileWatchingFailureHandler(fileSystemWatcherFactory));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
@ -204,4 +197,25 @@ public class LocalDevToolsAutoConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class RestartingClassPathChangeChangedEventListener implements ApplicationListener<ClassPathChangedEvent> {
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(RestartingClassPathChangeChangedEventListener.class);
|
||||||
|
|
||||||
|
private final FileSystemWatcherFactory fileSystemWatcherFactory;
|
||||||
|
|
||||||
|
RestartingClassPathChangeChangedEventListener(FileSystemWatcherFactory fileSystemWatcherFactory) {
|
||||||
|
this.fileSystemWatcherFactory = fileSystemWatcherFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ClassPathChangedEvent event) {
|
||||||
|
if (event.isRestartRequired()) {
|
||||||
|
logger.info(LogMessage.format("Restarting due to %s", event.overview()));
|
||||||
|
logger.debug(LogMessage.of(() -> "Change set: %s" + event.getChangeSet()));
|
||||||
|
Restarter.getInstance().restart(new FileWatchingFailureHandler(this.fileSystemWatcherFactory));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -18,6 +18,8 @@ package org.springframework.boot.devtools.classpath;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.boot.devtools.filewatch.ChangedFile;
|
||||||
|
import org.springframework.boot.devtools.filewatch.ChangedFile.Type;
|
||||||
import org.springframework.boot.devtools.filewatch.ChangedFiles;
|
import org.springframework.boot.devtools.filewatch.ChangedFiles;
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.core.style.ToStringCreator;
|
import org.springframework.core.style.ToStringCreator;
|
||||||
|
|
@ -71,4 +73,37 @@ public class ClassPathChangedEvent extends ApplicationEvent {
|
||||||
.append("restartRequired", this.restartRequired).toString();
|
.append("restartRequired", this.restartRequired).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an overview of the changes that triggered this event.
|
||||||
|
* @return an overview of the changes
|
||||||
|
* @since 2.6.11
|
||||||
|
*/
|
||||||
|
public String overview() {
|
||||||
|
int added = 0;
|
||||||
|
int deleted = 0;
|
||||||
|
int modified = 0;
|
||||||
|
for (ChangedFiles changedFiles : this.changeSet) {
|
||||||
|
for (ChangedFile changedFile : changedFiles) {
|
||||||
|
Type type = changedFile.getType();
|
||||||
|
if (type == Type.ADD) {
|
||||||
|
added++;
|
||||||
|
}
|
||||||
|
else if (type == Type.DELETE) {
|
||||||
|
deleted++;
|
||||||
|
}
|
||||||
|
else if (type == Type.MODIFY) {
|
||||||
|
modified++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int size = added + deleted + modified;
|
||||||
|
return String.format("%s (%s, %s, %s)", quantityOfUnit(size, "class path change"),
|
||||||
|
quantityOfUnit(added, "addition"), quantityOfUnit(deleted, "deletion"),
|
||||||
|
quantityOfUnit(modified, "modification"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String quantityOfUnit(int quantity, String unit) {
|
||||||
|
return quantity + " " + ((quantity != 1) ? unit + "s" : unit);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,14 +92,15 @@ public class ClassPathChangeUploader implements ApplicationListener<ClassPathCha
|
||||||
try {
|
try {
|
||||||
ClassLoaderFiles classLoaderFiles = getClassLoaderFiles(event);
|
ClassLoaderFiles classLoaderFiles = getClassLoaderFiles(event);
|
||||||
byte[] bytes = serialize(classLoaderFiles);
|
byte[] bytes = serialize(classLoaderFiles);
|
||||||
performUpload(classLoaderFiles, bytes);
|
performUpload(classLoaderFiles, bytes, event);
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new IllegalStateException(ex);
|
throw new IllegalStateException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes) throws IOException {
|
private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes, ClassPathChangedEvent event)
|
||||||
|
throws IOException {
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -108,11 +109,11 @@ public class ClassPathChangeUploader implements ApplicationListener<ClassPathCha
|
||||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||||
headers.setContentLength(bytes.length);
|
headers.setContentLength(bytes.length);
|
||||||
FileCopyUtils.copy(bytes, request.getBody());
|
FileCopyUtils.copy(bytes, request.getBody());
|
||||||
|
logUpload(event);
|
||||||
ClientHttpResponse response = request.execute();
|
ClientHttpResponse response = request.execute();
|
||||||
HttpStatusCode statusCode = response.getStatusCode();
|
HttpStatusCode statusCode = response.getStatusCode();
|
||||||
Assert.state(statusCode == HttpStatus.OK,
|
Assert.state(statusCode == HttpStatus.OK,
|
||||||
() -> "Unexpected " + statusCode + " response uploading class files");
|
() -> "Unexpected " + statusCode + " response uploading class files");
|
||||||
logUpload(classLoaderFiles);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
catch (SocketException ex) {
|
catch (SocketException ex) {
|
||||||
|
|
@ -129,9 +130,8 @@ public class ClassPathChangeUploader implements ApplicationListener<ClassPathCha
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logUpload(ClassLoaderFiles classLoaderFiles) {
|
private void logUpload(ClassPathChangedEvent event) {
|
||||||
int size = classLoaderFiles.size();
|
logger.info(LogMessage.format("Uploading %s", event.overview()));
|
||||||
logger.info(LogMessage.format("Uploaded %s class %s", size, (size != 1) ? "resources" : "resource"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] serialize(ClassLoaderFiles classLoaderFiles) throws IOException {
|
private byte[] serialize(ClassLoaderFiles classLoaderFiles) throws IOException {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue