Skip to content

Commit

Permalink
consumer demo without spring xml config and dubbo annotation config
Browse files Browse the repository at this point in the history
  • Loading branch information
majinkai committed Nov 14, 2014
1 parent acf52af commit d1259a4
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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;

/**
* @author Jinkai.Ma
*/
public class DemoJavaConfigConsumer {
public static void main(String[] args) {
// add `javaconfig` to args
String[] customArgs = new String[]{"javaconfig"};
com.alibaba.dubbo.container.Main.main(customArgs);
}
}

0 comments on commit d1259a4

Please sign in to comment.