-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92b0b1d
commit 2e11cf0
Showing
12 changed files
with
202 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
framework/src/main/java/me/insidezhou/southernquiet/throttle/ThrottleBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.insidezhou.southernquiet.throttle; | ||
|
||
import me.insidezhou.southernquiet.throttle.annotation.ThrottledSchedule; | ||
import me.insidezhou.southernquiet.throttle.annotation.Throttle; | ||
import org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor; | ||
import org.springframework.aop.support.ComposablePointcut; | ||
import org.springframework.aop.support.DefaultPointcutAdvisor; | ||
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; | ||
|
||
public class ThrottleBeanPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor { | ||
public ThrottleBeanPostProcessor(ThrottleAdvice advice) { | ||
this.advisor = new DefaultPointcutAdvisor( | ||
new ComposablePointcut(AnnotationMatchingPointcut.forMethodAnnotation(Throttle.class)) | ||
.union(AnnotationMatchingPointcut.forMethodAnnotation(ThrottledSchedule.class)), | ||
advice); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
framework/src/main/java/me/insidezhou/southernquiet/throttle/ThrottleException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package me.insidezhou.southernquiet.throttle; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ThrottleException extends Exception { | ||
public ThrottleException(String throttleName, long threshold, TimeUnit timeUnit) { | ||
super("throttleName='" + throttleName + '\'' + | ||
", threshold=" + threshold + | ||
", timeUnit=" + timeUnit); | ||
|
||
this.throttleName = throttleName; | ||
this.threshold = threshold; | ||
this.timeUnit = timeUnit; | ||
} | ||
|
||
private String throttleName; | ||
private long threshold; | ||
private TimeUnit timeUnit; | ||
|
||
public String getThrottleName() { | ||
return throttleName; | ||
} | ||
|
||
public void setThrottleName(String throttleName) { | ||
this.throttleName = throttleName; | ||
} | ||
|
||
public long getThreshold() { | ||
return threshold; | ||
} | ||
|
||
public void setThreshold(long threshold) { | ||
this.threshold = threshold; | ||
} | ||
|
||
public TimeUnit getTimeUnit() { | ||
return timeUnit; | ||
} | ||
|
||
public void setTimeUnit(TimeUnit timeUnit) { | ||
this.timeUnit = timeUnit; | ||
} | ||
} |
24 changes: 14 additions & 10 deletions
24
framework/src/main/java/me/insidezhou/southernquiet/throttle/annotation/Throttle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
package me.insidezhou.southernquiet.throttle.annotation; | ||
|
||
import org.springframework.core.annotation.AliasFor; | ||
|
||
import java.lang.annotation.*; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* 节流器注解,在method上使用,对该method进行节流。 | ||
* <p>timeUnit可空,若为空则创建计数器节流器,否则创建时间节流器; | ||
* <p>throttleName可空,若为空则使用类名#方法名作为节流器名称 | ||
* | ||
*/ | ||
@Target({ElementType.METHOD}) | ||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Documented | ||
public @interface Throttle { | ||
@AliasFor("threshold") | ||
long value() default -1; | ||
|
||
String throttleName() default ""; | ||
@AliasFor("value") | ||
long threshold() default -1; | ||
|
||
long threshold(); | ||
/** | ||
* 若为空则使用类名#方法名作为节流器名称。 | ||
*/ | ||
String name() default ""; | ||
|
||
/** | ||
* 若为空则创建计数器节流器,否则创建时间节流器。 | ||
*/ | ||
TimeUnit[] timeUnit() default {}; | ||
|
||
} |
11 changes: 0 additions & 11 deletions
11
.../main/java/me/insidezhou/southernquiet/throttle/annotation/ThrottleBeanPostProcessor.java
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
...work/src/main/java/me/insidezhou/southernquiet/throttle/annotation/ThrottleException.java
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
...work/src/main/java/me/insidezhou/southernquiet/throttle/annotation/ThrottledSchedule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package me.insidezhou.southernquiet.throttle.annotation; | ||
|
||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
||
import java.lang.annotation.*; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Documented | ||
@Throttle | ||
@Scheduled | ||
public @interface ThrottledSchedule { | ||
@AliasFor(annotation = Throttle.class) | ||
long value() default -1; | ||
|
||
@AliasFor(annotation = Throttle.class) | ||
long threshold() default -1; | ||
|
||
@AliasFor(annotation = Throttle.class) | ||
String name() default ""; | ||
|
||
@AliasFor(annotation = Throttle.class) | ||
TimeUnit[] timeUnit() default {}; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
String cron() default ""; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
String zone() default ""; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
long fixedDelay() default -1; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
String fixedDelayString() default ""; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
long fixedRate() default -1; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
String fixedRateString() default ""; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
long initialDelay() default -1; | ||
|
||
@AliasFor(annotation = Scheduled.class) | ||
String initialDelayString() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.