Skip to content

Commit

Permalink
Add annotation CDI demo and Quarkus adapter demo (alibaba#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
seasidesky authored Jun 16, 2020
1 parent 72366a1 commit d318449
Show file tree
Hide file tree
Showing 29 changed files with 1,762 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sentinel-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<module>sentinel-demo-log-logback</module>
<module>sentinel-demo-okhttp</module>
<module>sentinel-demo-jax-rs</module>
<module>sentinel-demo-quarkus</module>
<module>sentinel-demo-annotation-cdi-interceptor</module>
</modules>

<dependencies>
Expand Down
39 changes: 39 additions & 0 deletions sentinel-demo/sentinel-demo-annotation-cdi-interceptor/pom.xml
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>
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);
}
}
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());
}
}
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);
}
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";
}
}
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>
4 changes: 4 additions & 0 deletions sentinel-demo/sentinel-demo-quarkus/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!target/*-runner
!target/*-runner.jar
!target/lib/*
35 changes: 35 additions & 0 deletions sentinel-demo/sentinel-demo-quarkus/.gitignore
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
Loading

0 comments on commit d318449

Please sign in to comment.