国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Spring的異常重試框架Spring Retry簡單配置操作

Spring的異常重試框架Spring Retry簡單配置操作

2020-09-18 13:15狂豐 JAVA教程

這篇文章主要介紹了Spring的異常重試框架Spring Retry簡單配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

相關api見:點擊進入

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * 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的簡單配置做一個講解:

首先引入maven依賴:

?
1
2
3
4
5
<dependency>
      <groupId>org.springframework.retry</groupId>
      <artifactId>spring-retry</artifactId>
      <version>RELEASE</version>
    </dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@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("發送消息異常");
    JSONObject jsonTexts = (JSONObject) JSON.parse(string);
    if (jsonTexts.get("errcode") != null) {
      String errcode = jsonTexts.get("errcode").toString();
      if (errcode == null) {
        throw new RemoteAccessException("發送消息異常");
      }
      if (Integer.parseInt(errcode) == 0) {
        return true;
      } else {
        throw new RemoteAccessException("發送消息異常");
      }
    } else {
      throw new RemoteAccessException("發送消息異常");
    }
  } catch (Exception e) {
    logger.error("wz:customSend:{}", ExceptionUtils.getStackTrace(e));
    throw new RemoteAccessException("發送消息異常");
  }
}

注解內容介紹:

@Retryable注解

被注解的方法發生異常時會重試

value:指定發生的異常進行重試

include:和value一樣,默認空,當exclude也為空時,所有異常都重試

exclude:指定異常不重試,默認空,當include也為空時,所有異常都重試

maxAttemps:重試次數,默認3

backoff:重試補償機制,默認沒有

@Backoff注解

delay:指定延遲后重試

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

注意:

1、使用了@Retryable的方法不能在本類被調用,不然重試機制不會生效。也就是要標記為@Service,然后在其它類使用@Autowired注入或者@Bean去實例才能生效。

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

3、在重試期間這個方法是同步的,如果使用類似Spring Cloud這種框架的熔斷機制時,可以結合重試機制來重試后返回結果。

4、Spring Retry不僅能注入方式去實現,還可以通過API的方式實現,類似熔斷處理的機制就基于API方式實現會比較寬松。

以上這篇Spring的異常重試框架Spring Retry簡單配置操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/fz13768884254/article/details/83176459

延伸 · 閱讀

精彩推薦
  • JAVA教程spring中定時任務taskScheduler的詳細介紹

    spring中定時任務taskScheduler的詳細介紹

    本文章向大家介紹spring中定時任務taskScheduler的詳細介紹,主要包括spring中定時任務taskScheduler的詳細介紹使用實例、應用技巧、基本知識點總結和需要注意...

    java教程網4752020-07-11
  • JAVA教程Java和Ceylon對象的構造和驗證

    Java和Ceylon對象的構造和驗證

    這篇文章主要為大家詳細介紹了Java和Ceylon對象的構造和驗證,具有一定的參考價值,感興趣的小伙伴們可以參考一下 ...

    shanshuiss1472020-07-05
  • JAVA教程Java Netty HTTP服務實現過程解析

    Java Netty HTTP服務實現過程解析

    這篇文章主要介紹了Java Netty HTTP服務實現過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參...

    猿天地2132020-08-13
  • JAVA教程java分形繪制科赫雪花曲線(科赫曲線)代碼分享

    java分形繪制科赫雪花曲線(科赫曲線)代碼分享

    部分與整體以某種形式相似的形,稱為分形,科赫曲線是一種外形像雪花的幾何曲線,所以又稱為雪花曲線,它是分形曲線中的一種,畫法如下 ...

    java技術網7322019-10-23
  • JAVA教程Java如何從服務器中下載圖片

    Java如何從服務器中下載圖片

    這篇文章主要為大家詳細介紹了Java如何從服務器中下載圖片,代碼中附有詳細注釋,感興趣的小伙伴們可以參考一下 ...

    java教程網3402020-04-30
  • JAVA教程Java(enum)枚舉用法詳解

    Java(enum)枚舉用法詳解

    本篇文章主要介紹了Java 枚舉用法詳解,枚舉的好處:可以將常量組織起來,統一進行管理。有興趣的可以一起來了解一下。 ...

    靜默虛空3642020-07-07
  • JAVA教程Java 解析XML數據的4種方式

    Java 解析XML數據的4種方式

    這篇文章主要介紹了Java 解析XML數據的4種方式,幫助大家更好的用Java處理數據,感興趣的朋友可以了解下...

    dirft1962020-09-02
  • JAVA教程常用Eclipse快捷方式(推薦)

    常用Eclipse快捷方式(推薦)

    下面小編就為大家帶來一篇常用Eclipse快捷方式(推薦)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧 ...

    jingxian2622020-05-08
主站蜘蛛池模板: 欧美午夜一区二区 | 欧美一级片 | 日韩av高清| 国产黄色av网站 | 亚洲性视屏 | 国产精品视频久久久 | 日本一区二区中文字幕 | 永久av | 国内精品久久久久久久影视红豆 | 2023国产精品久久久精品双 | 国产在线视频一区 | 亚洲国产aⅴ成人精品无吗 久久久91 | 色www精品视频在线观看 | 山岸逢花在线观看 | 龙珠z国语291集普通话 | 懂色av中文字幕一区二区三区 | 久久国产精品免费一区二区三区 | 久久精品99久久 | 欧美a在线 | 日本免费一区二区在线 | 黄网在线观看 | 精品在线91 | 精品视频网站 | 亚洲精品不卡 | 亚洲视频一区二区三区 | 亚洲色综合 | 成年人在线观看免费视频 | 日韩精品视频久久 | 久久精彩| 黄色录像特级 | 国产一区二区久久 | 中文字幕在线三区 | 中文字幕在线影院 | 国产激情视频 | 91精品国产高清久久久久久久久 | 中国freesex | 中文字幕精品一区二区三区精品 | 久久久久久亚洲精品视频 | jlzzjlzz亚洲日本少妇 | 精品久久av | 国产高清在线精品一区二区三区 |