Additional changes and fixes related to the previous GH-268 commit

- Fixed issue which was causing one of the new tests to fail *only* when running as ‘mvn install’
- Modified HttpSupplier to return a delayed Mono for non2xx responses. Add javadoc
- Added conditional retry ability to the SupplierExporter to handle both ConnectionException for cases when connection may not be available or disappears during subscription.
- Polished error handling and lifecycle logic in SupplierExporter
- Added test demonstrating both retries as well as lifecycle control

Resolves #268
This commit is contained in:
Oleg Zhurakousky
2019-02-28 09:52:18 +01:00
parent 428243ce48
commit 5f86e3ea1a
6 changed files with 212 additions and 62 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.function.web.source;
import java.time.Duration;
import java.util.function.Supplier;
import org.apache.commons.logging.Log;
@@ -31,9 +32,11 @@ import org.springframework.web.reactive.function.client.WebClient;
/**
* A {@link Supplier} that pulls data from an HTTP endpoint. Repeatedly polls the endpoint
* until a non-2xx response is received.
* until a non-2xx response is received, at which point it will repeatedly produced a
* Mono at 1 sec intervals until the next 2xx response.
*
* @author Dave Syer
* @author Oleg Zhurakousky
*/
public class HttpSupplier implements Supplier<Flux<?>> {
@@ -70,9 +73,9 @@ public class HttpSupplier implements Supplier<Flux<?>> {
HttpStatus status = response.statusCode();
if (!status.is2xxSuccessful()) {
if (this.props.isDebug()) {
logger.info("Terminated supplier with status=" + response.statusCode());
logger.info("Delaying supplier based on status=" + response.statusCode());
}
return Mono.error(TerminateException.INSTANCE);
return Mono.delay(Duration.ofSeconds(1));
}
return response.bodyToMono(this.props.getSource().getType())
.map(value -> message(response, value));
@@ -88,10 +91,10 @@ public class HttpSupplier implements Supplier<Flux<?>> {
.build();
}
@SuppressWarnings("serial")
private static class TerminateException extends RuntimeException {
static final TerminateException INSTANCE = new TerminateException();
@SuppressWarnings("unused")
TerminateException() {
super("Planned termination");
}
@@ -102,5 +105,4 @@ public class HttpSupplier implements Supplier<Flux<?>> {
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.function.web.source;
import java.net.ConnectException;
import java.net.URI;
import java.util.Collections;
import java.util.Set;
@@ -39,6 +40,7 @@ import org.springframework.web.reactive.function.client.WebClient;
* endpoint.
*
* @author Dave Syer
* @author Oleg Zhurakousky
*
*/
public class SupplierExporter implements SmartLifecycle {
@@ -82,9 +84,6 @@ public class SupplierExporter implements SmartLifecycle {
if (this.running) {
return;
}
this.running = true;
this.ok = true;
logger.info("Starting");
Flux<Object> streams = Flux.empty();
@@ -99,16 +98,62 @@ public class SupplierExporter implements SmartLifecycle {
streams = streams.mergeWith(forward(supplier, name));
}
this.subscription = streams.doOnError(error -> {
this.ok = false;
if (!this.debug) {
logger.info(error);
}
}).doOnTerminate(() -> this.running = false).doOnNext(value -> {
if (this.subscription != null && !this.running) {
this.subscription.dispose();
}
}).subscribe();
this.subscription = streams
.retry(error -> {
/*
* The ConnectException may happen if a server is not yet available/reachable
* The ClassCast is to handle delayed Mono issued by HttpSupplier.transform for non-2xx responses
*/
boolean retry = error instanceof ConnectException || error instanceof ClassCastException
&& this.running;
if (!retry) {
this.ok = false;
if (!this.debug) {
logger.info(error);
}
stop();
}
return retry;
})
.doOnComplete(() -> {
stop();
})
.subscribe();
this.ok = true;
this.running = true;
}
public boolean isOk() {
return this.ok;
}
@Override
public void stop() {
logger.info("Stopping");
this.running = false;
this.subscription.dispose();
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public int getPhase() {
return 0;
}
@Override
public boolean isAutoStartup() {
return this.autoStartup;
}
@Override
public void stop(Runnable callback) {
stop();
callback.run();
}
private Flux<ClientResponse> forward(Supplier<Flux<Object>> supplier, String name) {
@@ -144,36 +189,4 @@ public class SupplierExporter implements SmartLifecycle {
private URI uri(String destination) {
return this.requestBuilder.uri(destination);
}
public boolean isOk() {
return this.ok;
}
@Override
public void stop() {
logger.info("Stopping");
this.running = false;
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public int getPhase() {
return 0;
}
@Override
public boolean isAutoStartup() {
return this.autoStartup;
}
@Override
public void stop(Runnable callback) {
stop();
callback.run();
}
}