亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring的異常重試框架Spring Retry簡(jiǎn)單配置操作

 更新時(shí)間:2020年09月18日 09:58:33   作者:狂豐  
這篇文章主要介紹了Spring的異常重試框架Spring Retry簡(jiǎn)單配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

相關(guān)api見(jiàn):點(diǎn)擊進(jìn)入

/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.springframework.retry.annotation;
 
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * Annotation for a method invocation that is retryable.
 *
 * @author Dave Syer
 * @author Artem Bilan
 * @author Gary Russell
 * @since 1.1
 *
 */
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Retryable {
 
	/**
	 * Retry interceptor bean name to be applied for retryable method. Is mutually
	 * exclusive with other attributes.
	 * @return the retry interceptor bean name
	 */
	String interceptor() default "";
 
	/**
	 * Exception types that are retryable. Synonym for includes(). Defaults to empty (and
	 * if excludes is also empty all exceptions are retried).
	 * @return exception types to retry
	 */
	Class<? extends Throwable>[] value() default {};
 
	/**
	 * Exception types that are retryable. Defaults to empty (and if excludes is also
	 * empty all exceptions are retried).
	 * @return exception types to retry
	 */
	Class<? extends Throwable>[] include() default {};
 
	/**
	 * Exception types that are not retryable. Defaults to empty (and if includes is also
	 * empty all exceptions are retried).
	 * @return exception types to retry
	 */
	Class<? extends Throwable>[] exclude() default {};
 
	/**
	 * A unique label for statistics reporting. If not provided the caller may choose to
	 * ignore it, or provide a default.
	 *
	 * @return the label for the statistics
	 */
	String label() default "";
 
	/**
	 * Flag to say that the retry is stateful: i.e. exceptions are re-thrown, but the
	 * retry policy is applied with the same policy to subsequent invocations with the
	 * same arguments. If false then retryable exceptions are not re-thrown.
	 * @return true if retry is stateful, default false
	 */
	boolean stateful() default false;
 
	/**
	 * @return the maximum number of attempts (including the first failure), defaults to 3
	 */
	int maxAttempts() default 3;
 
	/**
	 * @return an expression evaluated to the maximum number of attempts (including the first failure), defaults to 3
	 * Overrides {@link #maxAttempts()}.
	 * @since 1.2
	 */
	String maxAttemptsExpression() default "";
 
	/**
	 * Specify the backoff properties for retrying this operation. The default is a
	 * simple {@link Backoff} specification with no properties - see it's documentation
	 * for defaults.
	 * @return a backoff specification
	 */
	Backoff backoff() default @Backoff();
 
	/**
	 * Specify an expression to be evaluated after the {@code SimpleRetryPolicy.canRetry()}
	 * returns true - can be used to conditionally suppress the retry. Only invoked after
	 * an exception is thrown. The root object for the evaluation is the last {@code Throwable}.
	 * Other beans in the context can be referenced.
	 * For example:
	 * <pre class=code>
	 * {@code "message.contains('you can retry this')"}.
	 * </pre>
	 * and
	 * <pre class=code>
	 * {@code "@someBean.shouldRetry(#root)"}.
	 * </pre>
	 * @return the expression.
	 * @since 1.2
	 */
	String exceptionExpression() default "";
 
}

下面就 Retryable的簡(jiǎn)單配置做一個(gè)講解:

首先引入maven依賴(lài):

<dependency>
      <groupId>org.springframework.retry</groupId>
      <artifactId>spring-retry</artifactId>
      <version>RELEASE</version>
    </dependency>

然后在方法上配置注解@Retryable

@Override
@SuppressWarnings("Duplicates")
@Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000l, multiplier = 0))
public boolean customSendText(String openid, String content) throws RemoteAccessException {
  String replyString = "{\n" +
      "\"touser\":" + openid + ",\n" +
      "\"msgtype\":\"text\",\n" +
      "\"text\":\n" +
      "{\n" +
      "\"content\":" + content + "\n" +
      "}\n" +
      "}";
  try {
    logger.info("wx:customSend=request:{}", replyString.toString());
    HttpsClient httpClient = HttpsClient.getAsyncHttpClient();
    String url = Constant.WX_CUSTOM_SEND;
    String token = wxAccessokenService.getAccessToken();
    url = url.replace("ACCESS_TOKEN", token);
    logger.info("wx:customSend=url:{}", url);
    String string = httpClient.doPost(url, replyString);
    logger.info("wx:customSend=response:{}", string);
    if (StringUtils.isEmpty(string)) throw new RemoteAccessException("發(fā)送消息異常");
    JSONObject jsonTexts = (JSONObject) JSON.parse(string);
    if (jsonTexts.get("errcode") != null) {
      String errcode = jsonTexts.get("errcode").toString();
      if (errcode == null) {
        throw new RemoteAccessException("發(fā)送消息異常");
      }
      if (Integer.parseInt(errcode) == 0) {
        return true;
      } else {
        throw new RemoteAccessException("發(fā)送消息異常");
      }
    } else {
      throw new RemoteAccessException("發(fā)送消息異常");
    }
  } catch (Exception e) {
    logger.error("wz:customSend:{}", ExceptionUtils.getStackTrace(e));
    throw new RemoteAccessException("發(fā)送消息異常");
  }
}

注解內(nèi)容介紹:

@Retryable注解

被注解的方法發(fā)生異常時(shí)會(huì)重試

value:指定發(fā)生的異常進(jìn)行重試

include:和value一樣,默認(rèn)空,當(dāng)exclude也為空時(shí),所有異常都重試

exclude:指定異常不重試,默認(rèn)空,當(dāng)include也為空時(shí),所有異常都重試

maxAttemps:重試次數(shù),默認(rèn)3

backoff:重試補(bǔ)償機(jī)制,默認(rèn)沒(méi)有

@Backoff注解

delay:指定延遲后重試

multiplier:指定延遲的倍數(shù),比如delay=5000l,multiplier=2時(shí),第一次重試為5秒后,第二次為10秒,第三次為20秒

注意:

1、使用了@Retryable的方法不能在本類(lèi)被調(diào)用,不然重試機(jī)制不會(huì)生效。也就是要標(biāo)記為@Service,然后在其它類(lèi)使用@Autowired注入或者@Bean去實(shí)例才能生效。

2、使用了@Retryable的方法里面不能使用try...catch包裹,要在發(fā)放上拋出異常,不然不會(huì)觸發(fā)。

3、在重試期間這個(gè)方法是同步的,如果使用類(lèi)似Spring Cloud這種框架的熔斷機(jī)制時(shí),可以結(jié)合重試機(jī)制來(lái)重試后返回結(jié)果。

4、Spring Retry不僅能注入方式去實(shí)現(xiàn),還可以通過(guò)API的方式實(shí)現(xiàn),類(lèi)似熔斷處理的機(jī)制就基于API方式實(shí)現(xiàn)會(huì)比較寬松。

以上這篇Spring的異常重試框架Spring Retry簡(jiǎn)單配置操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring中的事件監(jiān)聽(tīng)器使用學(xué)習(xí)記錄

    Spring中的事件監(jiān)聽(tīng)器使用學(xué)習(xí)記錄

    Spring框架中的事件監(jiān)聽(tīng)機(jī)制是一種設(shè)計(jì)模式,它允許你定義和觸發(fā)事件,同時(shí)允許其他組件監(jiān)聽(tīng)這些事件并在事件發(fā)生時(shí)作出響應(yīng),這篇文章主要介紹了Spring中的事件監(jiān)聽(tīng)器使用學(xué)習(xí),需要的朋友可以參考下
    2024-07-07
  • Spring中@Async的使用小結(jié)

    Spring中@Async的使用小結(jié)

    在Java開(kāi)發(fā)中,我們常常會(huì)遇到需要執(zhí)行耗時(shí)操作的場(chǎng)景,例如文件上傳、網(wǎng)絡(luò)請(qǐng)求等,本文將介紹如何在Java中使用異步方法,并探討其中的一些注意事項(xiàng),感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • Java while(scanner.hasNext())無(wú)法跳出的解決方案

    Java while(scanner.hasNext())無(wú)法跳出的解決方案

    這篇文章主要介紹了Java while(scanner.hasNext())無(wú)法跳出的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • springboot+dubbo+zookeeper的簡(jiǎn)單實(shí)例詳解

    springboot+dubbo+zookeeper的簡(jiǎn)單實(shí)例詳解

    本文主要介紹了springboot+dubbo+zookeeper的簡(jiǎn)單實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法

    idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法

    這篇文章主要介紹了idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 一文徹底理清SpringBoot CURD處理邏輯、順序

    一文徹底理清SpringBoot CURD處理邏輯、順序

    這篇文章主要給大家介紹了關(guān)于如何一文徹底理清SpringBoot CURD處理邏輯、順序的相關(guān)資料,CURD是一個(gè)數(shù)據(jù)庫(kù)技術(shù)中的縮寫(xiě)詞,一般的項(xiàng)目開(kāi)發(fā)的各種參數(shù)的基本功能都是CURD,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 配置Servlet兩種方法以及特點(diǎn)詳解

    配置Servlet兩種方法以及特點(diǎn)詳解

    這篇文章主要介紹了配置Servlet兩種方法以及特點(diǎn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • java匿名內(nèi)部類(lèi)實(shí)例簡(jiǎn)析

    java匿名內(nèi)部類(lèi)實(shí)例簡(jiǎn)析

    匿名類(lèi)是不能有名稱(chēng)的類(lèi),所以沒(méi)辦法引用它們,必須在創(chuàng)建時(shí),作為new語(yǔ)句的一部分來(lái)聲明它們,需要了解更多的可以參考本文
    2012-11-11
  • 基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    這篇文章主要介紹了基于SpringBoot中activeMq的JmsTemplate的實(shí)例問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java多線程中的異常處理機(jī)制簡(jiǎn)析

    java多線程中的異常處理機(jī)制簡(jiǎn)析

    在java多線程程序中,所有線程都不允許拋出未捕獲的checked exception,也就是說(shuō)各個(gè)線程需要自己把自己的checked exception處理掉,需要了解的朋友可以參考下
    2012-11-11

最新評(píng)論