org.springframework.util.concurrent.FailureCallback Java Examples

The following examples show how to use org.springframework.util.concurrent.FailureCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FailureCallbackWithTracingTest.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@Before
public void beforeMethod() {
    failureCallbackMock = mock(FailureCallback.class);

    inObj = new Exception("kaboom");
    throwExceptionDuringCall = false;
    currentSpanStackWhenFailureCallbackWasCalled = new ArrayList<>();
    currentMdcInfoWhenFailureCallbackWasCalled = new ArrayList<>();
    doAnswer(invocation -> {
        currentSpanStackWhenFailureCallbackWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy());
        currentMdcInfoWhenFailureCallbackWasCalled.add(MDC.getCopyOfContextMap());
        if (throwExceptionDuringCall)
            throw new RuntimeException("kaboom");
        return null;
    }).when(failureCallbackMock).onFailure(inObj);

    resetTracing();
}
 
Example #2
Source File: AsyncResult.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super V> successCallback, FailureCallback failureCallback) {
	try {
		if (this.executionException != null) {
			failureCallback.onFailure(exposedException(this.executionException));
		}
		else {
			successCallback.onSuccess(this.value);
		}
	}
	catch (Throwable ex) {
		// Ignore
	}
}
 
Example #3
Source File: WingtipsSpringUtilTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Before
public void beforeMethod() {
    resetTracing();

    httpMessageMock = mock(HttpMessage.class);
    headersMock = mock(HttpHeaders.class);
    doReturn(headersMock).when(httpMessageMock).getHeaders();

    successCallbackMock = mock(SuccessCallback.class);
    failureCallbackMock = mock(FailureCallback.class);
    listenableFutureCallbackMock = mock(ListenableFutureCallback.class);

    tagStrategyMock = mock(HttpTagAndSpanNamingStrategy.class);
    tagAdapterMock = mock(HttpTagAndSpanNamingAdapter.class);
}
 
Example #4
Source File: FailureCallbackWithTracing.java    From wingtips with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor that uses the given trace and MDC information, which will be associated with the thread when the
 * given operation is executed.
 *
 * <p>The operation you pass in cannot be null (an {@link IllegalArgumentException} will be thrown if you pass in
 * null for the operation).
 *
 * <p>The trace and/or MDC info can be null and no error will be thrown, however any trace or MDC info that is null
 * means the corresponding info will not be available to the thread when the operation is executed.
 */
public FailureCallbackWithTracing(FailureCallback origFailureCallback,
                                  Deque<Span> spanStackForExecution,
                                  Map<String, String> mdcContextMapForExecution) {
    if (origFailureCallback == null)
        throw new IllegalArgumentException("origFailureCallback cannot be null");

    this.origFailureCallback = origFailureCallback;
    this.spanStackForExecution = spanStackForExecution;
    this.mdcContextMapForExecution = mdcContextMapForExecution;
}
 
Example #5
Source File: WingtipsSpringUtilTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
private void verifyFailureCallbackWithTracing(FailureCallback result,
                                              FailureCallback expectedCoreInstance,
                                              Deque<Span> expectedSpanStack,
                                              Map<String, String> expectedMdcInfo) {
    assertThat(result).isInstanceOf(FailureCallbackWithTracing.class);
    assertThat(Whitebox.getInternalState(result, "origFailureCallback")).isSameAs(expectedCoreInstance);
    assertThat(Whitebox.getInternalState(result, "spanStackForExecution")).isEqualTo(expectedSpanStack);
    assertThat(Whitebox.getInternalState(result, "mdcContextMapForExecution")).isEqualTo(expectedMdcInfo);
}
 
Example #6
Source File: WingtipsSpringUtilTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Test
public void failureCallbackWithTracing_using_current_thread_info_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = setupCurrentThreadWithTracingInfo();

    // when
    FailureCallback result = failureCallbackWithTracing(failureCallbackMock);

    // then
    verifyFailureCallbackWithTracing(result, failureCallbackMock, setupInfo.getLeft(), setupInfo.getRight());
}
 
Example #7
Source File: WingtipsSpringUtilTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Test
public void failureCallbackWithTracing_pair_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = generateTracingInfo();

    // when
    FailureCallback result = failureCallbackWithTracing(failureCallbackMock, setupInfo);

    // then
    verifyFailureCallbackWithTracing(result, failureCallbackMock, setupInfo.getLeft(), setupInfo.getRight());
}
 
Example #8
Source File: WingtipsSpringUtilTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Test
public void failureCallbackWithTracing_separate_args_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = generateTracingInfo();

    // when
    FailureCallback result = failureCallbackWithTracing(
        failureCallbackMock, setupInfo.getLeft(), setupInfo.getRight()
    );

    // then
    verifyFailureCallbackWithTracing(result, failureCallbackMock, setupInfo.getLeft(), setupInfo.getRight());
}
 
Example #9
Source File: AsyncResult.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super V> successCallback, FailureCallback failureCallback) {
	try {
		successCallback.onSuccess(this.value);
	}
	catch (Throwable ex) {
		failureCallback.onFailure(ex);
	}
}
 
Example #10
Source File: KafkaRegister.java    From bird-java with MIT License 5 votes vote down vote up
/**
 * 事件注册
 *
 * @param eventArg 事件参数
 */
@Override
public void regist(IEventArg eventArg) {
    ListenableFuture<SendResult<String, IEventArg>> listenableFuture = kafkaTemplate.send(getTopic(eventArg), eventArg);

    EventRegisterResult registerResult = new EventRegisterResult(eventArg);
    //发送成功回调
    SuccessCallback<SendResult<String, IEventArg>> successCallback = result -> {
        if (registerStore == null) return;
        registerResult.setSuccess(true);
        Map<String, Object> map = new HashMap<>(2);
        if (result != null) {
            map.put("producerRecord", result.getProducerRecord());
            map.put("metadata", result.getRecordMetadata());
        }
        registerResult.setExtJson(JSON.toJSONString(map));
        registerStore.register(registerResult);
    };

    //发送失败回调
    FailureCallback failureCallback = ex -> {
        if (registerStore == null) return;
        registerResult.setSuccess(false);
        registerResult.setMessage(ex.getMessage());
        registerStore.register(registerResult);

        log.error(ex.getMessage());
    };
    listenableFuture.addCallback(successCallback, failureCallback);
}
 
Example #11
Source File: AsyncResult.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super V> successCallback, FailureCallback failureCallback) {
	try {
		if (this.executionException != null) {
			Throwable cause = this.executionException.getCause();
			failureCallback.onFailure(cause != null ? cause : this.executionException);
		}
		else {
			successCallback.onSuccess(this.value);
		}
	}
	catch (Throwable ex) {
		// Ignore
	}
}
 
Example #12
Source File: AsyncServiceOne.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@GetMapping
public Future<?> process() {
    AsyncRestTemplate template = new AsyncRestTemplate();
    SuccessCallback onSuccess = r -> System.out.println("Success");
    FailureCallback onFailure = e -> System.out.println("Failure");
    ListenableFuture<?> response = template.getForEntity(
            "http://localhost:" + PORT + "/api/v2/resource/b",
            ExamplesCollection.class
    );

    response.addCallback(onSuccess, onFailure);

    return response;
}
 
Example #13
Source File: MultiPartitionMessageProducer.java    From kafka-with-springboot with Apache License 2.0 5 votes vote down vote up
public void send(String topic, String key, String payload){
    LOGGER.info("Sending payload='{}' to topic='{}' with key='{}'", payload, topic, key);
    ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, key ,payload);
    SuccessCallback<SendResult<String,String>> successCallback = sendResult -> {
        LOGGER.info("Sent payload='{}' with key='{}' to topic-partition@offset='{}'", payload, key, sendResult.getRecordMetadata().toString());
    };
    FailureCallback failureCallback = throwable -> {
        LOGGER.info("Sending payload='{}' to topic='{}' with key='{}' failed!!!", payload, topic, key);
    };
    future.addCallback(successCallback, failureCallback);
}
 
Example #14
Source File: HttpComponentsAsyncClientHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super ClientHttpResponse> successCallback,
		FailureCallback failureCallback) {

	this.callback.addSuccessCallback(successCallback);
	this.callback.addFailureCallback(failureCallback);
}
 
Example #15
Source File: TraceContextListenableFuture.java    From brave with Apache License 2.0 5 votes vote down vote up
public void addCallback(SuccessCallback<? super T> successCallback,
  FailureCallback failureCallback) {
  delegate.addCallback(
    successCallback != null
      ? new TraceContextSuccessCallback<>(successCallback, this)
      : null,
    failureCallback != null
      ? new TraceContextFailureCallback(failureCallback, this)
      : null
  );
}
 
Example #16
Source File: AsyncResult.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super V> successCallback, FailureCallback failureCallback) {
	try {
		if (this.executionException != null) {
			failureCallback.onFailure(exposedException(this.executionException));
		}
		else {
			successCallback.onSuccess(this.value);
		}
	}
	catch (Throwable ex) {
		// Ignore
	}
}
 
Example #17
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super ClientHttpResponse> successCallback,
		FailureCallback failureCallback) {

	this.callback.addSuccessCallback(successCallback);
	this.callback.addFailureCallback(failureCallback);
}
 
Example #18
Source File: SpringFutureUtils.java    From future-converter with Apache License 2.0 4 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback) {
    getWrappedFuture().addCallbacks(successCallback::onSuccess, failureCallback::onFailure);
}
 
Example #19
Source File: SpringFutureUtils.java    From future-converter with Apache License 2.0 4 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback) {
    getWrappedFuture().addCallback(successCallback, failureCallback);
}
 
Example #20
Source File: CompletableToListenableFutureAdapter.java    From riptide with MIT License 4 votes vote down vote up
@Override
public void addCallback(final SuccessCallback<? super T> successCallback, final FailureCallback failureCallback) {
    callbacks.addSuccessCallback(successCallback);
    callbacks.addFailureCallback(failureCallback);
}
 
Example #21
Source File: TraceContextListenableFuture.java    From brave with Apache License 2.0 4 votes vote down vote up
TraceContextFailureCallback(FailureCallback delegate,
  TraceContextListenableFuture<?> future) {
  this.delegate = delegate;
  this.currentTraceContext = future.currentTraceContext;
  this.invocationContext = future.invocationContext;
}
 
Example #22
Source File: WingtipsSpringUtil.java    From wingtips with Apache License 2.0 4 votes vote down vote up
/**
 * @return A {@link FailureCallback} that wraps the given original so that the given distributed tracing and MDC
 * information is registered with the thread and therefore available during execution and unregistered after
 * execution.
 */
public static FailureCallback failureCallbackWithTracing(FailureCallback failureCallback,
                                                         Deque<Span> spanStackToLink,
                                                         Map<String, String> mdcContextMapToLink) {
    return new FailureCallbackWithTracing(failureCallback, spanStackToLink, mdcContextMapToLink);
}
 
Example #23
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super ClientHttpResponse> successCallback, FailureCallback failureCallback) {
	this.callback.addSuccessCallback(successCallback);
	this.callback.addFailureCallback(failureCallback);
}
 
Example #24
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void addFailureCallback(FailureCallback callback) {
	this.callbacks.addFailureCallback(callback);
}
 
Example #25
Source File: AbstractPromiseToListenableFutureAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback) {
	this.registry.addSuccessCallback(successCallback);
	this.registry.addFailureCallback(failureCallback);
}
 
Example #26
Source File: HttpComponentsAsyncClientHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void addCallback(SuccessCallback<? super ClientHttpResponse> successCallback, FailureCallback failureCallback) {
	this.callback.addSuccessCallback(successCallback);
	this.callback.addFailureCallback(failureCallback);
}
 
Example #27
Source File: HttpComponentsAsyncClientHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void addFailureCallback(FailureCallback callback) {
	this.callbacks.addFailureCallback(callback);
}
 
Example #28
Source File: HttpComponentsAsyncClientHttpRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void addFailureCallback(FailureCallback callback) {
	this.callbacks.addFailureCallback(callback);
}
 
Example #29
Source File: HttpComponentsAsyncClientHttpRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void addFailureCallback(FailureCallback callback) {
	this.callbacks.addFailureCallback(callback);
}
 
Example #30
Source File: FailureCallbackWithTracing.java    From wingtips with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor that uses the given trace and MDC information, which will be associated with the thread when the
 * given operation is executed.
 *
 * <p>The operation you pass in cannot be null (an {@link IllegalArgumentException} will be thrown if you pass in
 * null for the operation).
 *
 * <p>The {@link Pair} can be null, or you can pass null for the left and/or right side of the pair, and no error
 * will be thrown. Any trace or MDC info that is null means the corresponding info will not be available to the
 * thread when the operation is executed however.
 *
 * <p>You can pass in a {@link TracingState} for clearer less verbose code since it extends
 * {@code Pair<Deque<Span>, Map<String, String>>}.
 */
public FailureCallbackWithTracing(FailureCallback origFailureCallback,
                                  Pair<Deque<Span>, Map<String, String>> originalThreadInfo) {
    this(
        origFailureCallback,
        (originalThreadInfo == null) ? null : originalThreadInfo.getLeft(),
        (originalThreadInfo == null) ? null : originalThreadInfo.getRight()
    );
}