|
@@ -1,26 +1,28 @@ |
|
|
# Sentinel Annotation cdi interceptor |
|
|
|
|
|
|
|
|
# Sentinel Annotation CDI extension |
|
|
|
|
|
|
|
|
This extension is an implementation using cdi interceptor for Sentinel annotations. [JSR 318: Enterprise JavaBeansTM 3.1/Interceptors 1.2](https://jcp.org/en/jsr/detail?id=318) define the javax interceptor and [CDI](http://www.cdi-spec.org/) related specifications extends the Java Interceptors specification and allows interceptor bindings to be applied to CDI stereotypes. |
|
|
|
|
|
|
|
|
This extension is an implementation using CDI interceptor for Sentinel annotations. [JSR 318: Enterprise JavaBeansTM 3.1/Interceptors 1.2](https://jcp.org/en/jsr/detail?id=318) define the javax interceptor and [CDI](http://www.cdi-spec.org/) related specifications extends the Java Interceptors specification and allows interceptor bindings to be applied to CDI stereotypes. |
|
|
|
|
|
|
|
|
[CDI](http://www.cdi-spec.org/) is an abbreviation for Contexts and Dependency Injection, the related JSRs are : [JSR 365: Contexts and Dependency Injection for JavaTM 2.0](https://jcp.org/en/jsr/detail?id=365), [JSR 346: Contexts and Dependency Injection for JavaTM EE 1.1](https://jcp.org/en/jsr/detail?id=346), [JSR 299: Contexts and Dependency Injection for the JavaTM EE platform](https://jcp.org/en/jsr/detail?id=299) |
|
|
[CDI](http://www.cdi-spec.org/) is an abbreviation for Contexts and Dependency Injection, the related JSRs are : [JSR 365: Contexts and Dependency Injection for JavaTM 2.0](https://jcp.org/en/jsr/detail?id=365), [JSR 346: Contexts and Dependency Injection for JavaTM EE 1.1](https://jcp.org/en/jsr/detail?id=346), [JSR 299: Contexts and Dependency Injection for the JavaTM EE platform](https://jcp.org/en/jsr/detail?id=299) |
|
|
|
|
|
|
|
|
## Annotation |
|
|
## Annotation |
|
|
|
|
|
|
|
|
The `@SentinelResourceBinding` is modified from `@SentinelResource` by adding `@InterceptorBinding` for CDI specification, and in order to interceptor all kinds of `@SentinelResourceBinding` with different attributes in one interceptor, `@SentinelResourceBinding`'s all attribute is annotated by `@Nonbinding` |
|
|
|
|
|
|
|
|
The `@SentinelResourceBinding` is modified from `@SentinelResource` by adding `@InterceptorBinding` for CDI specification, |
|
|
|
|
|
and in order to intercept all kinds of `@SentinelResourceBinding` with different attributes in one interceptor, |
|
|
|
|
|
all attributes of `@SentinelResourceBinding` are annotated with `@Nonbinding`. |
|
|
|
|
|
|
|
|
The `@SentinelResource` annotation indicates a resource definition, including: |
|
|
|
|
|
|
|
|
The `@SentinelResourceBinding` annotation indicates a resource definition, including: |
|
|
|
|
|
|
|
|
- `value`: Resource name, required (cannot be empty) |
|
|
- `value`: Resource name, required (cannot be empty) |
|
|
- `entryType`: Resource entry type (inbound or outbound), `EntryType.OUT` by default |
|
|
|
|
|
- `fallback` (refactored since 1.6.0): Fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). The fallback method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The method signature requirement: |
|
|
|
|
|
|
|
|
- `entryType`: Traffic type (inbound or outbound), `EntryType.OUT` by default |
|
|
|
|
|
- `fallback`: Fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). The fallback method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The method signature requirement: |
|
|
- The return type should match the origin method; |
|
|
- The return type should match the origin method; |
|
|
- The parameter list should match the origin method, and an additional `Throwable` parameter can be provided to get the actual exception. |
|
|
- The parameter list should match the origin method, and an additional `Throwable` parameter can be provided to get the actual exception. |
|
|
- `defaultFallback` (since 1.6.0): The default fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). Its intended to be a universal common fallback method. The method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The default fallback method signature requirement: |
|
|
|
|
|
|
|
|
- `defaultFallback`: The default fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). Its intended to be a universal common fallback method. The method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The default fallback method signature requirement: |
|
|
- The return type should match the origin method; |
|
|
- The return type should match the origin method; |
|
|
- parameter list should be empty, and an additional `Throwable` parameter can be provided to get the actual exception. |
|
|
- parameter list should be empty, and an additional `Throwable` parameter can be provided to get the actual exception. |
|
|
- `blockHandler`: Handler method that handles `BlockException` when blocked. The parameter list of the method should match original method, with the last additional parameter type `BlockException`. The return type should be same as the original method. The `blockHandler` method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `blockHandlerClass` with corresponding `Class` (Note the method in other classes must be *static*). |
|
|
- `blockHandler`: Handler method that handles `BlockException` when blocked. The parameter list of the method should match original method, with the last additional parameter type `BlockException`. The return type should be same as the original method. The `blockHandler` method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `blockHandlerClass` with corresponding `Class` (Note the method in other classes must be *static*). |
|
|
- `exceptionsToIgnore` (since 1.6.0): List of business exception classes that should not be traced and caught in fallback. |
|
|
|
|
|
- `exceptionsToTrace` (since 1.5.1): List of business exception classes to trace and record. In most cases, using `exceptionsToIgnore` is better. If both `exceptionsToTrace` and `exceptionsToIgnore` are present, only `exceptionsToIgnore` will be activated. |
|
|
|
|
|
|
|
|
- `exceptionsToIgnore`: List of business exception classes that should not be traced and caught in fallback. |
|
|
|
|
|
- `exceptionsToTrace`: List of business exception classes to trace and record. In most cases, using `exceptionsToIgnore` is better. If both `exceptionsToTrace` and `exceptionsToIgnore` are present, only `exceptionsToIgnore` will be activated. |
|
|
|
|
|
|
|
|
For example: |
|
|
For example: |
|
|
|
|
|
|
|
@@ -42,7 +44,8 @@ public String defaultFallback(Throwable t) { |
|
|
|
|
|
|
|
|
## Configuration |
|
|
## Configuration |
|
|
|
|
|
|
|
|
according to [9.4. Interceptor enablement and ordering](https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#enabled_interceptors) to enable interceptor, it should be configured in resources/META-INF/beans.xml : |
|
|
|
|
|
|
|
|
According to [9.4. Interceptor enablement and ordering](https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#enabled_interceptors), to enable the interceptor, |
|
|
|
|
|
we may add configuration in `resources/META-INF/beans.xml` like this: |
|
|
|
|
|
|
|
|
``` |
|
|
``` |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
|
<?xml version="1.0" encoding="UTF-8"?> |
|
@@ -54,7 +57,3 @@ according to [9.4. Interceptor enablement and ordering](https://docs.jboss.org/c |
|
|
</interceptors> |
|
|
</interceptors> |
|
|
</beans> |
|
|
</beans> |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|