forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add annotation CDI demo and Quarkus adapter demo (alibaba#1543)
- Loading branch information
1 parent
72366a1
commit d318449
Showing
29 changed files
with
1,762 additions
and
0 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
39 changes: 39 additions & 0 deletions
39
sentinel-demo/sentinel-demo-annotation-cdi-interceptor/pom.xml
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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>sentinel-demo</artifactId> | ||
<groupId>com.alibaba.csp</groupId> | ||
<version>1.8.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-demo-annotation-cdi-interceptor</artifactId> | ||
|
||
<properties> | ||
<weld-se-shaded.version>3.1.4.Final</weld-se-shaded.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-annotation-cdi-interceptor</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-transport-simple-http</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jboss.weld.se</groupId> | ||
<artifactId>weld-se-shaded</artifactId> | ||
<version>${weld-se-shaded.version}</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
50 changes: 50 additions & 0 deletions
50
...c/main/java/com/alibaba/csp/sentinel/demo/annotation/cdi/interceptor/DemoApplication.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,50 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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 | ||
* | ||
* https://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 com.alibaba.csp.sentinel.demo.annotation.cdi.interceptor; | ||
|
||
import javax.enterprise.inject.se.SeContainer; | ||
import javax.enterprise.inject.se.SeContainerInitializer; | ||
|
||
/** | ||
* @author sea | ||
*/ | ||
public class DemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SeContainerInitializer containerInit = SeContainerInitializer.newInstance(); | ||
SeContainer container = containerInit.initialize(); | ||
|
||
TestService testService = container.select(TestService.class).get(); | ||
|
||
testService.test(); | ||
|
||
System.out.println(testService.hello(-1)); | ||
System.out.println(testService.hello(1)); | ||
|
||
System.out.println(testService.helloAnother("bad")); | ||
|
||
try { | ||
System.out.println(testService.helloAnother("foo")); | ||
} catch (IllegalStateException e) { | ||
System.err.println(e.getMessage()); | ||
} | ||
|
||
System.out.println(testService.helloAnother("weld")); | ||
|
||
container.close(); | ||
System.exit(0); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...src/main/java/com/alibaba/csp/sentinel/demo/annotation/cdi/interceptor/ExceptionUtil.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,34 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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 com.alibaba.csp.sentinel.demo.annotation.cdi.interceptor; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
|
||
/** | ||
* @author Eric Zhao | ||
*/ | ||
public final class ExceptionUtil { | ||
|
||
public static void handleException(BlockException ex) { | ||
// Handler method that handles BlockException when blocked. | ||
// The method parameter list should match original method, with the last additional | ||
// parameter with type BlockException. The return type should be same as the original method. | ||
// The block handler 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). | ||
System.out.println("Oops: " + ex.getClass().getCanonicalName()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...r/src/main/java/com/alibaba/csp/sentinel/demo/annotation/cdi/interceptor/TestService.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,28 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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 com.alibaba.csp.sentinel.demo.annotation.cdi.interceptor; | ||
|
||
/** | ||
* @author Eric Zhao | ||
*/ | ||
public interface TestService { | ||
|
||
void test(); | ||
|
||
String hello(long s); | ||
|
||
String helloAnother(String name); | ||
} |
65 changes: 65 additions & 0 deletions
65
...c/main/java/com/alibaba/csp/sentinel/demo/annotation/cdi/interceptor/TestServiceImpl.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,65 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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 com.alibaba.csp.sentinel.demo.annotation.cdi.interceptor; | ||
|
||
import com.alibaba.csp.sentinel.annotation.cdi.interceptor.SentinelResourceBinding; | ||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
|
||
/** | ||
* @author Eric Zhao | ||
*/ | ||
@ApplicationScoped | ||
public class TestServiceImpl implements TestService { | ||
|
||
@Override | ||
@SentinelResourceBinding(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class}) | ||
public void test() { | ||
System.out.println("Test"); | ||
} | ||
|
||
@Override | ||
@SentinelResourceBinding(value = "hello", fallback = "helloFallback") | ||
public String hello(long s) { | ||
if (s < 0) { | ||
throw new IllegalArgumentException("invalid arg"); | ||
} | ||
return String.format("Hello at %d", s); | ||
} | ||
|
||
@Override | ||
@SentinelResourceBinding(value = "helloAnother", defaultFallback = "defaultFallback", | ||
exceptionsToIgnore = {IllegalStateException.class}) | ||
public String helloAnother(String name) { | ||
if (name == null || "bad".equals(name)) { | ||
throw new IllegalArgumentException("oops"); | ||
} | ||
if ("foo".equals(name)) { | ||
throw new IllegalStateException("oops"); | ||
} | ||
return "Hello, " + name; | ||
} | ||
|
||
public String helloFallback(long s, Throwable ex) { | ||
// Do some log here. | ||
return "Oops, error occurred at " + s + ", msg:" + ex.getMessage(); | ||
} | ||
|
||
public String defaultFallback() { | ||
System.out.println("Go to default fallback"); | ||
return "default_fallback"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
sentinel-demo/sentinel-demo-annotation-cdi-interceptor/src/main/resources/META-INF/beans.xml
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,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" | ||
bean-discovery-mode="all"> | ||
<interceptors> | ||
<class>com.alibaba.csp.sentinel.annotation.cdi.interceptor.SentinelResourceInterceptor</class> | ||
</interceptors> | ||
</beans> |
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,4 @@ | ||
* | ||
!target/*-runner | ||
!target/*-runner.jar | ||
!target/lib/* |
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,35 @@ | ||
# Eclipse | ||
.project | ||
.classpath | ||
.settings/ | ||
bin/ | ||
|
||
# IntelliJ | ||
.idea | ||
*.ipr | ||
*.iml | ||
*.iws | ||
|
||
# NetBeans | ||
nb-configuration.xml | ||
|
||
# Visual Studio Code | ||
.vscode | ||
|
||
# OSX | ||
.DS_Store | ||
|
||
# Vim | ||
*.swp | ||
*.swo | ||
|
||
# patch | ||
*.orig | ||
*.rej | ||
|
||
# Maven | ||
target/ | ||
pom.xml.tag | ||
pom.xml.releaseBackup | ||
pom.xml.versionsBackup | ||
release.properties |
Oops, something went wrong.