forked from apache/dubbo
-
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.
Merge pull request apache#5 from markerking/feature/spring-javaconfig…
…-container spring javaconfig container
- Loading branch information
Showing
12 changed files
with
413 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!-- | ||
- Copyright 2006-2014 handu.com. | ||
- | ||
- 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. | ||
--> | ||
<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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>dubbo-container</artifactId> | ||
<version>2.8.2</version> | ||
</parent> | ||
<artifactId>dubbo-container-javaconfig</artifactId> | ||
<packaging>jar</packaging> | ||
<name>${project.artifactId}</name> | ||
<description>The spring container by java config module of dubbo project</description> | ||
<properties> | ||
<skip_maven_deploy>true</skip_maven_deploy> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>dubbo-container-api</artifactId> | ||
<version>${project.parent.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
64 changes: 64 additions & 0 deletions
64
...-javaconfig/src/main/java/com/alibaba/dubbo/container/javaconfig/JavaConfigContainer.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,64 @@ | ||
/** | ||
* Copyright 2006-2014 handu.com. | ||
* | ||
* 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.dubbo.container.javaconfig; | ||
|
||
import com.alibaba.dubbo.common.logger.Logger; | ||
import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
import com.alibaba.dubbo.common.utils.ConfigUtils; | ||
import com.alibaba.dubbo.container.Container; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
/** | ||
* JavaConfigContainer. (SPI, Singleton, ThreadSafe) | ||
* | ||
* @author Jinkai.Ma | ||
*/ | ||
public class JavaConfigContainer implements Container { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(JavaConfigContainer.class); | ||
|
||
public static final String SPRING_JAVACONFIG = "dubbo.spring.javaconfig"; | ||
|
||
public static final String DEFAULT_SPRING_JAVACONFIG = "dubbo.spring.javaconfig"; | ||
|
||
static AnnotationConfigApplicationContext context; | ||
|
||
public static AnnotationConfigApplicationContext getContext() { | ||
return context; | ||
} | ||
|
||
public void start() { | ||
String configPath = ConfigUtils.getProperty(SPRING_JAVACONFIG); | ||
if (configPath == null || configPath.length() == 0) { | ||
configPath = DEFAULT_SPRING_JAVACONFIG; | ||
} | ||
context = new AnnotationConfigApplicationContext(configPath); | ||
context.start(); | ||
} | ||
|
||
public void stop() { | ||
try { | ||
if (context != null) { | ||
context.stop(); | ||
context.close(); | ||
context = null; | ||
} | ||
} catch (Throwable e) { | ||
logger.error(e.getMessage(), e); | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...vaconfig/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.container.Container
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 @@ | ||
javaconfig=com.alibaba.dubbo.container.javaconfig.JavaConfigContainer |
36 changes: 36 additions & 0 deletions
36
...aconfig/src/test/java/com/alibaba/dubbo/container/javaconfig/JavaConfigContainerTest.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,36 @@ | ||
/** | ||
* Copyright 2006-2014 handu.com. | ||
* | ||
* 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.dubbo.container.javaconfig; | ||
|
||
import com.alibaba.dubbo.common.extension.ExtensionLoader; | ||
import com.alibaba.dubbo.container.Container; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author Jinkai.Ma | ||
*/ | ||
public class JavaConfigContainerTest { | ||
|
||
@Test | ||
public void testContainer() { | ||
JavaConfigContainer container = (JavaConfigContainer) ExtensionLoader.getExtensionLoader(Container.class).getExtension("javaconfig"); | ||
container.start(); | ||
Assert.assertEquals(JavaConfigContainer.class, container.context.getBean("container").getClass()); | ||
container.stop(); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...ner/dubbo-container-javaconfig/src/test/java/dubbo/spring/javaconfig/TestDubboConfig.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,33 @@ | ||
/** | ||
* Copyright 2006-2014 handu.com. | ||
* | ||
* 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 dubbo.spring.javaconfig; | ||
|
||
import com.alibaba.dubbo.container.javaconfig.JavaConfigContainer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Jinkai.Ma | ||
*/ | ||
@Configuration | ||
public class TestDubboConfig { | ||
|
||
@Bean | ||
public JavaConfigContainer container() { | ||
return new JavaConfigContainer(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
dubbo-container/dubbo-container-javaconfig/src/test/resources/log4j.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,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
- Copyright 2006-2014 handu.com. | ||
- | ||
- 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. | ||
--> | ||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> | ||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> | ||
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> | ||
<layout class="org.apache.log4j.PatternLayout"> | ||
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" /> | ||
</layout> | ||
</appender> | ||
<root> | ||
<level value="INFO" /> | ||
<appender-ref ref="CONSOLE" /> | ||
</root> | ||
</log4j:configuration> |
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
87 changes: 87 additions & 0 deletions
87
...bbo-demo-consumer/src/main/java/com/alibaba/dubbo/demo/consumer/DemoJavaConfigAction.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,87 @@ | ||
/* | ||
* Copyright 2006-2014 handu.com. | ||
* | ||
* 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.dubbo.demo.consumer; | ||
|
||
import com.alibaba.dubbo.config.annotation.Reference; | ||
import com.alibaba.dubbo.demo.bid.*; | ||
import com.alibaba.dubbo.demo.user.User; | ||
import com.alibaba.dubbo.demo.user.facade.AnotherUserRestService; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Jinkai.Ma | ||
*/ | ||
@Component | ||
public class DemoJavaConfigAction { | ||
|
||
@Reference | ||
private BidService bidService; | ||
|
||
@Reference | ||
private AnotherUserRestService anotherUserRestService; | ||
|
||
@PostConstruct | ||
public void start() throws Exception { | ||
BidRequest request = new BidRequest(); | ||
|
||
Impression imp = new Impression(); | ||
imp.setBidFloor(1.1); | ||
imp.setId("abc"); | ||
List<Impression> imps = new ArrayList<Impression>(1); | ||
imps.add(imp); | ||
request.setImpressions(imps); | ||
|
||
Geo geo = new Geo(); | ||
geo.setCity("beijing"); | ||
geo.setCountry("china"); | ||
geo.setLat(100.1f); | ||
geo.setLon(100.1f); | ||
|
||
Device device = new Device(); | ||
device.setMake("apple"); | ||
device.setOs("ios"); | ||
device.setVersion("7.0"); | ||
device.setLang("zh_CN"); | ||
device.setModel("iphone"); | ||
device.setGeo(geo); | ||
request.setDevice(device); | ||
|
||
// long start = System.currentTimeMillis(); | ||
|
||
// for (int i = 0; i < 10000; i ++) { | ||
// System.out.println(bidService.bid(request).getId()); | ||
System.out.println("SUCESS: got bid response id: " + bidService.bid(request).getId()); | ||
// } | ||
|
||
// System.out.println(">>>>> Total time consumed:" + (System.currentTimeMillis() - start)); | ||
|
||
try { | ||
bidService.throwNPE(); | ||
System.out.println("ERROR: no exception found"); | ||
} catch (NullPointerException e) { | ||
System.out.println("SUCCESS: caught exception " + e.getClass()); | ||
} | ||
|
||
User user = new User(1L, "dang"); | ||
System.out.println("SUCESS: registered user with id " + anotherUserRestService.registerUser(user).getId()); | ||
|
||
System.out.println("SUCESS: got user " + anotherUserRestService.getUser(1L)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...demo/dubbo-demo-consumer/src/main/java/dubbo/spring/javaconfig/DubboDemoActionConfig.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,33 @@ | ||
/* | ||
* Copyright 2006-2014 handu.com. | ||
* | ||
* 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 dubbo.spring.javaconfig; | ||
|
||
import com.alibaba.dubbo.demo.consumer.DemoJavaConfigAction; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Jinkai.Ma | ||
*/ | ||
@Configuration | ||
public class DubboDemoActionConfig { | ||
|
||
@Bean | ||
public DemoJavaConfigAction demoAnnotationAction() { | ||
return new DemoJavaConfigAction(); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...mo/dubbo-demo-consumer/src/main/java/dubbo/spring/javaconfig/DubboDemoConsumerConfig.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,56 @@ | ||
/* | ||
* Copyright 2006-2014 handu.com. | ||
* | ||
* 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 dubbo.spring.javaconfig; | ||
|
||
import com.alibaba.dubbo.config.ApplicationConfig; | ||
import com.alibaba.dubbo.config.RegistryConfig; | ||
import com.alibaba.dubbo.config.spring.AnnotationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Jinkai.Ma | ||
*/ | ||
@Configuration | ||
public class DubboDemoConsumerConfig { | ||
|
||
public static final String APPLICATION_NAME = "consumer-of-helloworld-app"; | ||
|
||
public static final String REGISTRY_ADDRESS = "zookeeper://127.0.0.1:2181"; | ||
|
||
public static final String ANNOTATION_PACKAGE = "com.alibaba.dubbo.demo.consumer"; | ||
|
||
@Bean | ||
public ApplicationConfig applicationConfig() { | ||
ApplicationConfig applicationConfig = new ApplicationConfig(); | ||
applicationConfig.setName(APPLICATION_NAME); | ||
return applicationConfig; | ||
} | ||
|
||
@Bean | ||
public RegistryConfig registryConfig() { | ||
RegistryConfig registryConfig = new RegistryConfig(); | ||
registryConfig.setAddress(REGISTRY_ADDRESS); | ||
return registryConfig; | ||
} | ||
|
||
@Bean | ||
public AnnotationBean annotationBean() { | ||
AnnotationBean annotationBean = new AnnotationBean(); | ||
annotationBean.setPackage(ANNOTATION_PACKAGE); | ||
return annotationBean; | ||
} | ||
} |
Oops, something went wrong.