org.springframework.retry.RetryListener Java Examples

The following examples show how to use org.springframework.retry.RetryListener. 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: RetryLoadBalancerInterceptor.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
private RetryTemplate createRetryTemplate(String serviceName, HttpRequest request,
		LoadBalancedRetryPolicy retryPolicy) {
	RetryTemplate template = new RetryTemplate();
	BackOffPolicy backOffPolicy = this.lbRetryFactory
			.createBackOffPolicy(serviceName);
	template.setBackOffPolicy(
			backOffPolicy == null ? new NoBackOffPolicy() : backOffPolicy);
	template.setThrowLastExceptionOnExhausted(true);
	RetryListener[] retryListeners = this.lbRetryFactory
			.createRetryListeners(serviceName);
	if (retryListeners != null && retryListeners.length != 0) {
		template.setListeners(retryListeners);
	}
	template.setRetryPolicy(!this.lbProperties.isEnabled() || retryPolicy == null
			? new NeverRetryPolicy() : new InterceptorRetryPolicy(request,
					retryPolicy, this.loadBalancer, serviceName));
	return template;
}
 
Example #2
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test(expected = TerminatedRetryException.class)
public void retryListenerTestNoRetry() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://noRetry"));
	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	MyBackOffPolicy backOffPolicy = new MyBackOffPolicy();
	this.lbProperties.setEnabled(true);
	RetryListener myRetryListener = new RetryListenerSupport() {
		@Override
		public <T, E extends Throwable> boolean open(RetryContext context,
				RetryCallback<T, E> callback) {
			return false;
		}
	};
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			new MyLoadBalancedRetryFactory(policy, backOffPolicy,
					new RetryListener[] { myRetryListener }));
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	interceptor.intercept(request, new byte[] {}, execution);
}
 
Example #3
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void retryListenerTest() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://listener"));
	ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[] {},
			HttpStatus.OK);
	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class)))
			.thenReturn(true);
	MyBackOffPolicy backOffPolicy = new MyBackOffPolicy();
	ServiceInstance serviceInstance = mock(ServiceInstance.class);
	when(this.client.choose(eq("listener"))).thenReturn(serviceInstance);
	when(this.client.execute(eq("listener"), eq(serviceInstance),
			any(LoadBalancerRequest.class))).thenThrow(new IOException())
					.thenReturn(clientHttpResponse);
	this.lbProperties.setEnabled(true);
	MyRetryListener retryListener = new MyRetryListener();
	when(this.lbRequestFactory.createRequest(any(), any(), any()))
			.thenReturn(mock(LoadBalancerRequest.class));
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			new MyLoadBalancedRetryFactory(policy, backOffPolicy,
					new RetryListener[] { retryListener }));
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
	verify(this.client, times(2)).execute(eq("listener"), eq(serviceInstance),
			any(LoadBalancerRequest.class));
	then(rsp).isEqualTo(clientHttpResponse);
	verify(this.lbRequestFactory, times(2)).createRequest(request, body, execution);
	then(backOffPolicy.getBackoffAttempts()).isEqualTo(1);
	then(retryListener.getOnError()).isEqualTo(1);
}
 
Example #4
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public RetryListener[] createRetryListeners(String service) {
	if (this.retryListeners == null) {
		return new RetryListener[0];
	}
	else {
		return this.retryListeners;
	}
}
 
Example #5
Source File: ApimlRibbonRetryFactory.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
public ApimlRibbonRetryFactory(SpringClientFactory clientFactory, RetryListener... listeners) {
    super(clientFactory);
    this.listeners = listeners;
}
 
Example #6
Source File: ApimlRibbonRetryFactory.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public RetryListener[] createRetryListeners(String service) {
    return listeners;
}
 
Example #7
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
MyLoadBalancedRetryFactory(LoadBalancedRetryPolicy loadBalancedRetryPolicy,
		BackOffPolicy backOffPolicy, RetryListener[] retryListeners) {
	this(loadBalancedRetryPolicy, backOffPolicy);
	this.retryListeners = retryListeners;
}
 
Example #8
Source File: LoadBalancedRetryFactory.java    From spring-cloud-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an array of {@link RetryListener}s for a given service.
 * @param service The service to create the {@link RetryListener}s for.
 * @return An array of {@link RetryListener}s.
 */
default RetryListener[] createRetryListeners(String service) {
	return new RetryListener[0];
}
 
Example #9
Source File: DataServiceRetryAspect.java    From genie with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the retry listeners for the retry template in use.
 *
 * @param retryListeners retry listeners
 */
public void setRetryListeners(final RetryListener[] retryListeners) {
    this.retryTemplate.setListeners(retryListeners);
}