Java Code Examples for org.springframework.retry.RetryListener

The following examples show how to use org.springframework.retry.RetryListener. These examples are extracted from open source projects. 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
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
@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
@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
@Override
public RetryListener[] createRetryListeners(String service) {
	if (this.retryListeners == null) {
		return new RetryListener[0];
	}
	else {
		return this.retryListeners;
	}
}
 
Example 5
Source Project: api-layer   Source File: ApimlRibbonRetryFactory.java    License: 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 Project: api-layer   Source File: ApimlRibbonRetryFactory.java    License: Eclipse Public License 2.0 4 votes vote down vote up
@Override
public RetryListener[] createRetryListeners(String service) {
    return listeners;
}
 
Example 7
MyLoadBalancedRetryFactory(LoadBalancedRetryPolicy loadBalancedRetryPolicy,
		BackOffPolicy backOffPolicy, RetryListener[] retryListeners) {
	this(loadBalancedRetryPolicy, backOffPolicy);
	this.retryListeners = retryListeners;
}
 
Example 8
/**
 * 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 Project: genie   Source File: DataServiceRetryAspect.java    License: 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);
}