Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
b1dd9288
Commit
b1dd9288
authored
Jul 13, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Retry class file upload to remote application that fails to connect
Closes gh-6339
parent
68fb5789
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
22 deletions
+73
-22
ClassPathChangeUploader.java
.../boot/devtools/remote/client/ClassPathChangeUploader.java
+35
-11
ClassPathChangeUploaderTests.java
.../devtools/remote/client/ClassPathChangeUploaderTests.java
+38
-11
No files found.
spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/ClassPathChangeUploader.java
View file @
b1dd9288
/*
* Copyright 2012-201
5
the original author or authors.
* Copyright 2012-201
6
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -19,6 +19,7 @@ package org.springframework.boot.devtools.remote.client;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.ObjectOutputStream
;
import
java.net.ConnectException
;
import
java.net.MalformedURLException
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
...
...
@@ -51,6 +52,7 @@ import org.springframework.util.FileCopyUtils;
* Listens and pushes any classpath updates to a remote endpoint.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.3.0
*/
public
class
ClassPathChangeUploader
...
...
@@ -91,23 +93,45 @@ public class ClassPathChangeUploader
public
void
onApplicationEvent
(
ClassPathChangedEvent
event
)
{
try
{
ClassLoaderFiles
classLoaderFiles
=
getClassLoaderFiles
(
event
);
ClientHttpRequest
request
=
this
.
requestFactory
.
createRequest
(
this
.
uri
,
HttpMethod
.
POST
);
byte
[]
bytes
=
serialize
(
classLoaderFiles
);
HttpHeaders
headers
=
request
.
getHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM
);
headers
.
setContentLength
(
bytes
.
length
);
FileCopyUtils
.
copy
(
bytes
,
request
.
getBody
());
logUpload
(
classLoaderFiles
);
ClientHttpResponse
response
=
request
.
execute
();
Assert
.
state
(
response
.
getStatusCode
()
==
HttpStatus
.
OK
,
"Unexpected "
+
response
.
getStatusCode
()
+
" response uploading class files"
);
performUpload
(
classLoaderFiles
,
bytes
);
}
catch
(
IOException
ex
)
{
throw
new
IllegalStateException
(
ex
);
}
}
private
void
performUpload
(
ClassLoaderFiles
classLoaderFiles
,
byte
[]
bytes
)
throws
IOException
{
try
{
while
(
true
)
{
try
{
ClientHttpRequest
request
=
this
.
requestFactory
.
createRequest
(
this
.
uri
,
HttpMethod
.
POST
);
HttpHeaders
headers
=
request
.
getHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM
);
headers
.
setContentLength
(
bytes
.
length
);
FileCopyUtils
.
copy
(
bytes
,
request
.
getBody
());
ClientHttpResponse
response
=
request
.
execute
();
Assert
.
state
(
response
.
getStatusCode
()
==
HttpStatus
.
OK
,
"Unexpected "
+
response
.
getStatusCode
()
+
" response uploading class files"
);
logUpload
(
classLoaderFiles
);
return
;
}
catch
(
ConnectException
ex
)
{
logger
.
warn
(
"Failed to connect when uploading to "
+
this
.
uri
+
". Upload will be retried in 2 seconds"
);
Thread
.
sleep
(
2000
);
}
}
}
catch
(
InterruptedException
ex
)
{
Thread
.
currentThread
().
interrupt
();
throw
new
IllegalStateException
(
ex
);
}
}
private
void
logUpload
(
ClassLoaderFiles
classLoaderFiles
)
{
int
size
=
classLoaderFiles
.
size
();
logger
.
info
(
...
...
spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/ClassPathChangeUploaderTests.java
View file @
b1dd9288
/*
* Copyright 2012-201
5
the original author or authors.
* Copyright 2012-201
6
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -20,6 +20,7 @@ import java.io.ByteArrayInputStream;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.ObjectInputStream
;
import
java.net.ConnectException
;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.LinkedHashSet
;
...
...
@@ -45,12 +46,14 @@ import org.springframework.mock.http.client.MockClientHttpRequest;
import
org.springframework.util.FileCopyUtils
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Tests for {@link ClassPathChangeUploader}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public
class
ClassPathChangeUploaderTests
{
...
...
@@ -102,19 +105,28 @@ public class ClassPathChangeUploaderTests {
@Test
public
void
sendsClassLoaderFiles
()
throws
Exception
{
File
sourceFolder
=
this
.
temp
.
newFolder
();
Set
<
ChangedFile
>
files
=
new
LinkedHashSet
<
ChangedFile
>();
File
file1
=
createFile
(
sourceFolder
,
"File1"
);
File
file2
=
createFile
(
sourceFolder
,
"File2"
);
File
file3
=
createFile
(
sourceFolder
,
"File3"
);
files
.
add
(
new
ChangedFile
(
sourceFolder
,
file1
,
Type
.
ADD
));
files
.
add
(
new
ChangedFile
(
sourceFolder
,
file2
,
Type
.
MODIFY
));
files
.
add
(
new
ChangedFile
(
sourceFolder
,
file3
,
Type
.
DELETE
));
Set
<
ChangedFiles
>
changeSet
=
new
LinkedHashSet
<
ChangedFiles
>();
changeSet
.
add
(
new
ChangedFiles
(
sourceFolder
,
files
));
ClassPathChangedEvent
event
=
new
ClassPathChangedEvent
(
this
,
changeSet
,
false
);
ClassPathChangedEvent
event
=
createClassPathChangedEvent
(
sourceFolder
);
this
.
requestFactory
.
willRespond
(
HttpStatus
.
OK
);
this
.
uploader
.
onApplicationEvent
(
event
);
assertThat
(
this
.
requestFactory
.
getExecutedRequests
().
size
(),
is
(
1
));
MockClientHttpRequest
request
=
this
.
requestFactory
.
getExecutedRequests
().
get
(
0
);
verifyUploadRequest
(
sourceFolder
,
request
);
}
@Test
public
void
retriesOnConnectException
()
throws
Exception
{
File
sourceFolder
=
this
.
temp
.
newFolder
();
ClassPathChangedEvent
event
=
createClassPathChangedEvent
(
sourceFolder
);
this
.
requestFactory
.
willRespond
(
new
ConnectException
());
this
.
requestFactory
.
willRespond
(
HttpStatus
.
OK
);
this
.
uploader
.
onApplicationEvent
(
event
);
assertThat
(
this
.
requestFactory
.
getExecutedRequests
().
size
(),
is
(
2
));
verifyUploadRequest
(
sourceFolder
,
this
.
requestFactory
.
getExecutedRequests
().
get
(
1
));
}
private
void
verifyUploadRequest
(
File
sourceFolder
,
MockClientHttpRequest
request
)
throws
IOException
,
ClassNotFoundException
{
ClassLoaderFiles
classLoaderFiles
=
deserialize
(
request
.
getBodyAsBytes
());
Collection
<
SourceFolder
>
sourceFolders
=
classLoaderFiles
.
getSourceFolders
();
assertThat
(
sourceFolders
.
size
(),
equalTo
(
1
));
...
...
@@ -133,6 +145,21 @@ public class ClassPathChangeUploaderTests {
assertThat
(
file
.
getKind
(),
equalTo
(
kind
));
}
private
ClassPathChangedEvent
createClassPathChangedEvent
(
File
sourceFolder
)
throws
IOException
{
Set
<
ChangedFile
>
files
=
new
LinkedHashSet
<
ChangedFile
>();
File
file1
=
createFile
(
sourceFolder
,
"File1"
);
File
file2
=
createFile
(
sourceFolder
,
"File2"
);
File
file3
=
createFile
(
sourceFolder
,
"File3"
);
files
.
add
(
new
ChangedFile
(
sourceFolder
,
file1
,
Type
.
ADD
));
files
.
add
(
new
ChangedFile
(
sourceFolder
,
file2
,
Type
.
MODIFY
));
files
.
add
(
new
ChangedFile
(
sourceFolder
,
file3
,
Type
.
DELETE
));
Set
<
ChangedFiles
>
changeSet
=
new
LinkedHashSet
<
ChangedFiles
>();
changeSet
.
add
(
new
ChangedFiles
(
sourceFolder
,
files
));
ClassPathChangedEvent
event
=
new
ClassPathChangedEvent
(
this
,
changeSet
,
false
);
return
event
;
}
private
File
createFile
(
File
sourceFolder
,
String
name
)
throws
IOException
{
File
file
=
new
File
(
sourceFolder
,
name
);
FileCopyUtils
.
copy
(
name
.
getBytes
(),
file
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment