Java Code Examples for org.springframework.retry.policy.SimpleRetryPolicy#setMaxAttempts()

The following examples show how to use org.springframework.retry.policy.SimpleRetryPolicy#setMaxAttempts() . 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: SimpleDemo.java    From retry with Apache License 2.0 8 votes vote down vote up
public static void main(String[] args) throws Exception {
    RetryTemplate template = new RetryTemplate();

    // 策略
    SimpleRetryPolicy policy = new SimpleRetryPolicy();
    policy.setMaxAttempts(2);
    template.setRetryPolicy(policy);

    String result = template.execute(
            new RetryCallback<String, Exception>() {
                @Override
                public String doWithRetry(RetryContext arg0) {
                    throw new NullPointerException();
                }
            }
            ,
            new RecoveryCallback<String>() {
                @Override
                public String recover(RetryContext context) {
                    return "recovery callback";
                }
            }
    );

    LOGGER.info("result: {}", result);
}
 
Example 2
Source File: FeatureFileImporterImpl.java    From IridiumApplicationTesting with MIT License 8 votes vote down vote up
private String processRemoteUrl(@NotNull final String path) throws IOException {
	final File copy = File.createTempFile("webapptester", ".feature");

	try {
		final RetryTemplate template = new RetryTemplate();
		final SimpleRetryPolicy policy = new SimpleRetryPolicy();
		policy.setMaxAttempts(Constants.URL_COPY_RETRIES);
		template.setRetryPolicy(policy);
		return template.execute(context -> {
			FileUtils.copyURLToFile(new URL(path), copy);
			return FileUtils.readFileToString(copy, Charset.defaultCharset());
		});
	} finally {
		FileUtils.deleteQuietly(copy);
	}
}
 
Example 3
Source File: FeatureFileUtilsImpl.java    From IridiumApplicationTesting with MIT License 8 votes vote down vote up
private List<File> processRemoteUrl(@NotNull final String path) throws IOException {
	final File copy = File.createTempFile("webapptester", ".feature");

	try {
		final RetryTemplate template = new RetryTemplate();
		final SimpleRetryPolicy policy = new SimpleRetryPolicy();
		policy.setMaxAttempts(Constants.URL_COPY_RETRIES);
		template.setRetryPolicy(policy);
		template.execute(context -> {
			FileUtils.copyURLToFile(new URL(path), copy);
			return null;
		});

		return Arrays.asList(copy);
	} catch (final FileNotFoundException ex) {
		/*
			Don't leave an empty file hanging around
		 */
		FileUtils.deleteQuietly(copy);
		throw new RemoteFeatureException("The remote file could not be downloaded."
			+ " Either the URL was invalid, or the path was actually supposed to reference a"
			+ " local file but that file could not be found an so was assumed to be a URL.",  ex);
	}
}
 
Example 4
Source File: HttpBlockchainEventBroadcasterTest.java    From eventeum with Apache License 2.0 6 votes vote down vote up
@Before
public void init() {
    final HttpBroadcasterSettings settings = new HttpBroadcasterSettings();
    settings.setBlockEventsUrl("http://localhost:8082/consumer/block-event");
    settings.setContractEventsUrl("http://localhost:8082/consumer/contract-event");

    RetryTemplate retryTemplate = new RetryTemplate();

    FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    fixedBackOffPolicy.setBackOffPeriod(3000l);
    retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(3);
    retryTemplate.setRetryPolicy(retryPolicy);

    underTest = new HttpBlockchainEventBroadcaster(settings, retryTemplate);
}
 
Example 5
Source File: FileContentRetrieval.java    From IridiumApplicationTesting with MIT License 6 votes vote down vote up
private String retrieveStringFromRemoteFile(@NotNull final String remoteFileName)  {
	checkArgument(StringUtils.isNoneBlank(remoteFileName));

	File copy = null;
	try {
		copy = File.createTempFile("capabilities", ".tmp");

		final File finalCopy = copy;
		final RetryTemplate template = new RetryTemplate();
		final SimpleRetryPolicy policy = new SimpleRetryPolicy();
		policy.setMaxAttempts(Constants.URL_COPY_RETRIES);
		template.setRetryPolicy(policy);
		template.execute(context -> {
			FileUtils.copyURLToFile(new URL(remoteFileName), finalCopy, TIMEOUT, TIMEOUT);
			return null;
		});

		return retrieveStringFromLocalFile(copy.getAbsolutePath());
	} catch (final IOException ex) {
		throw new ConfigurationException(ex);
	} finally {
		if (copy != null) {
			FileUtils.deleteQuietly(copy);
		}
	}
}
 
Example 6
Source File: KafkaTopicProvisioner.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
	if (this.metadataRetryOperations == null) {
		RetryTemplate retryTemplate = new RetryTemplate();

		SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
		simpleRetryPolicy.setMaxAttempts(10);
		retryTemplate.setRetryPolicy(simpleRetryPolicy);

		ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
		backOffPolicy.setInitialInterval(100);
		backOffPolicy.setMultiplier(2);
		backOffPolicy.setMaxInterval(1000);
		retryTemplate.setBackOffPolicy(backOffPolicy);
		this.metadataRetryOperations = retryTemplate;
	}
}
 
Example 7
Source File: Application.java    From mojito with Apache License 2.0 6 votes vote down vote up
@Bean
public RetryTemplate retryTemplate() {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(5);

    ExponentialRandomBackOffPolicy exponentialRandomBackOffPolicy = new ExponentialRandomBackOffPolicy();
    exponentialRandomBackOffPolicy.setInitialInterval(10);
    exponentialRandomBackOffPolicy.setMultiplier(3);
    exponentialRandomBackOffPolicy.setMaxInterval(5000);

    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(retryPolicy);
    template.setBackOffPolicy(exponentialRandomBackOffPolicy);
    template.setThrowLastExceptionOnExhausted(true);

    return template;
}
 
Example 8
Source File: RetryInterceptor.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static RetryTemplate createRetryTemplate(RetryPolicy policy) {
	RetryTemplate template = new RetryTemplate();
	// 重试策略
	SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
	retryPolicy.setMaxAttempts(policy.getMaxAttempts());
	// 设置间隔策略
	FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
	backOffPolicy.setBackOffPeriod(policy.getSleepMillis());
	template.setRetryPolicy(retryPolicy);
	template.setBackOffPolicy(backOffPolicy);
	return template;
}
 
Example 9
Source File: RabbitAutoConfiguration.java    From summerframework with Apache License 2.0 5 votes vote down vote up
private RetryTemplate createRetryTemplate(RabbitProperties.Retry properties) {
    RetryTemplate template = new RetryTemplate();
    SimpleRetryPolicy policy = new SimpleRetryPolicy();
    policy.setMaxAttempts(properties.getMaxAttempts());
    template.setRetryPolicy(policy);
    ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(properties.getInitialInterval());
    backOffPolicy.setMultiplier(properties.getMultiplier());
    backOffPolicy.setMaxInterval(properties.getMaxInterval());
    template.setBackOffPolicy(backOffPolicy);
    return template;
}
 
Example 10
Source File: RetryServiceImpl.java    From IridiumApplicationTesting with MIT License 5 votes vote down vote up
@Override
public RetryTemplate getRetryTemplate() {
	final SimpleRetryPolicy policy = new SimpleRetryPolicy();
	policy.setMaxAttempts(Constants.WEBDRIVER_ACTION_RETRIES);

	final ExponentialBackOffPolicy backoff = new ExponentialBackOffPolicy();
	backoff.setInitialInterval(RETRY_INTERVAL);

	final RetryTemplate template = new RetryTemplate();
	template.setRetryPolicy(policy);
	template.setBackOffPolicy(backoff);
	return template;
}
 
Example 11
Source File: JobStatusWebhookEventListener.java    From piper with Apache License 2.0 5 votes vote down vote up
private RetryTemplate createRetryTemplate (Accessor aWebhook) {
  MapObject retryParams = aWebhook.get("retry",MapObject.class,new MapObject());
  RetryTemplate retryTemplate = new RetryTemplate();
  ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
  backOffPolicy.setInitialInterval(retryParams.getDuration("initialInterval", "2s").toMillis());
  backOffPolicy.setMaxInterval(retryParams.getDuration("maxInterval", "30s").toMillis());
  backOffPolicy.setMultiplier(retryParams.getDouble("multiplier",2.0));
  retryTemplate.setBackOffPolicy(backOffPolicy);
  SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
  retryPolicy.setMaxAttempts(retryParams.getInteger("maxAttempts", 5));
  retryTemplate.setRetryPolicy(retryPolicy);
  return retryTemplate;
}
 
Example 12
Source File: RetryExampleApplication.java    From blog-examples with Apache License 2.0 5 votes vote down vote up
@Bean
public RetryTemplate retryTemplate() {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(5);

    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(1500); // 1.5 seconds

    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(retryPolicy);
    template.setBackOffPolicy(backOffPolicy);

    return template;
}
 
Example 13
Source File: ClickingStepDefinitions.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
/**
 * Clicks a link on the page
 *
 * @param alias       If this word is found in the step, it means the linkContent is found from the data
 *                    set.
 * @param linkContent The text content of the link we are clicking
 * @param timesAlias  If this word is found in the step, it means the times is found from the
 *                    data set.
 * @param times       If this text is set, the click operation will be repeated the specified number of times.
 * @param exists      If this text is set, an error that would be thrown because the element was not found
 *                    is ignored. Essentially setting this text makes this an optional statement.
 */
@When("^I click (?:a|an|the) link with the text content of( alias)? \"([^\"]*)\"(?:( alias)? \"(.*?)\" times)?( if it exists)?$")
public void clickLinkStep(
	final String alias,
	final String linkContent,
	final String timesAlias,
	final String times,
	final String exists) {

	try {
		final Integer fixedTimes = countConverter.convertCountToInteger(timesAlias, times);

		final String text = autoAliasUtils.getValue(
			linkContent, StringUtils.isNotBlank(alias), State.getFeatureStateForThread());

		checkState(text != null, "the aliased link content does not exist");

		final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();

		for (int i = 0; i < fixedTimes; ++i) {
			final WebElement element = browserInteropUtils.getLinkByText(webDriver, text);

			mouseMovementUtils.mouseGlide(
				webDriver,
				(JavascriptExecutor) webDriver,
				element,
				Constants.MOUSE_MOVE_TIME,
				Constants.MOUSE_MOVE_STEPS);

			final RetryTemplate template = new RetryTemplate();
			final SimpleRetryPolicy policy = new SimpleRetryPolicy();
			policy.setMaxAttempts(Constants.WEBDRIVER_ACTION_RETRIES);
			template.setRetryPolicy(policy);
			final FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
			template.setBackOffPolicy(fixedBackOffPolicy);
			template.execute(context -> {
				element.click();
				return null;
			});

			sleepUtils.sleep(State.getFeatureStateForThread().getDefaultSleep());
		}
	} catch (final TimeoutException | NoSuchElementException ex) {
		if (StringUtils.isBlank(exists)) {
			throw ex;
		}
	}
}
 
Example 14
Source File: ClickingStepDefinitions.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
/**
 * Clicks a link that may or may not be visible on the page
 *
 * @param alias       If this word is found in the step, it means the linkContent is found from the data
 *                    set.
 * @param linkContent The text content of the link we are clicking
 * @param timesAlias  If this word is found in the step, it means the times is found from the
 *                    data set.
 * @param times       If this text is set, the click operation will be repeated the specified number of times.
 * @param exists      If this text is set, an error that would be thrown because the element was not found
 *                    is ignored. Essentially setting this text makes this an optional statement.
 */
@When("^I click (?:a|an|the) hidden link with the text content( alias)? of \"([^\"]*)\"(?:( alias)? \"(.*?)\" times)?( if it exists)?$")
public void clickHiddenLinkStep(
	final String alias,
	final String linkContent,
	final String timesAlias,
	final String times,
	final String exists) {

	try {
		final Integer fixedTimes = countConverter.convertCountToInteger(timesAlias, times);

		final String text = autoAliasUtils.getValue(
			linkContent, StringUtils.isNotBlank(alias), State.getFeatureStateForThread());

		checkState(text != null, "the aliased link content does not exist");

		final WebDriver webDriver = State.getThreadDesiredCapabilityMap().getWebDriverForThread();

		for (int i = 0; i < fixedTimes; ++i) {
			final RetryTemplate template = new RetryTemplate();
			final SimpleRetryPolicy policy = new SimpleRetryPolicy();
			policy.setMaxAttempts(Constants.WEBDRIVER_ACTION_RETRIES);
			template.setRetryPolicy(policy);
			final FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
			template.setBackOffPolicy(fixedBackOffPolicy);
			template.execute(context -> {
				final WebElement element = browserInteropUtils.getLinkByText(webDriver, text);

				mouseMovementUtils.mouseGlide(
					webDriver,
					(JavascriptExecutor) webDriver,
					element,
					Constants.MOUSE_MOVE_TIME,
					Constants.MOUSE_MOVE_STEPS);

				final JavascriptExecutor js = (JavascriptExecutor) webDriver;
				js.executeScript("arguments[0].click();", element);
				return null;
			});

			sleepUtils.sleep(State.getFeatureStateForThread().getDefaultSleep());
		}
	} catch (final TimeoutException | NoSuchElementException ex) {
		if (StringUtils.isBlank(exists)) {
			throw ex;
		}
	}
}