Skip to content

Commit

Permalink
Merge pull request apache#5 from markerking/feature/spring-javaconfig…
Browse files Browse the repository at this point in the history
…-container

spring javaconfig container
  • Loading branch information
li-shen committed Nov 14, 2014
2 parents 0c30bb2 + d1259a4 commit 7b29f97
Show file tree
Hide file tree
Showing 12 changed files with 413 additions and 0 deletions.
42 changes: 42 additions & 0 deletions dubbo-container/dubbo-container-javaconfig/pom.xml
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>
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);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
javaconfig=com.alibaba.dubbo.container.javaconfig.JavaConfigContainer
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();
}

}
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();
}

}
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>
1 change: 1 addition & 0 deletions dubbo-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<modules>
<module>dubbo-container-api</module>
<module>dubbo-container-spring</module>
<module>dubbo-container-javaconfig</module>
<module>dubbo-container-jetty</module>
<module>dubbo-container-log4j</module>
<module>dubbo-container-logback</module>
Expand Down
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));
}
}
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();
}

}
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;
}
}
Loading

0 comments on commit 7b29f97

Please sign in to comment.