import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.demo.CacheService;
public class Consumer {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "classpath:consumer.xml" });
context.start();
// DemoService demoService = (DemoService)
// context.getBean("demoService");
// while (true) {
// String hello = demoService.sayHello("world");
// System.out.println(hello);
//
// Thread.sleep(100);
// }
// 参数校验示例
// ValidationService validationService = (ValidationService)
// context.getBean("validationService");
// while (true) {
// ValidationParameter parameter = new ValidationParameter();
// parameter.setAge(23);
// parameter.setEmail("[email protected]");
//
// try {
// String result = validationService.intsert(parameter);
//
// System.out.println(result);
// } catch (RpcException e) { // 抛出的是RpcException
// ConstraintViolationException ve = (ConstraintViolationException)
// e.getCause(); // 里面嵌了一个ConstraintViolationException
// Set<ConstraintViolation<?>> violations =
// ve.getConstraintViolations(); // 可以拿到一个验证错误详细信息的集合
// System.out.println(violations);
// }
// }
CacheService cacheService = (CacheService) context.getBean( "cacheService" );
// 测试缓存生效,多次调用返回同样的结果。(服务器端自增长返回值)
String fix = null ;
for ( int i = 0 ; i < 5 ; i++) {
String result = cacheService.findCache( "0" );
if (fix == null || fix.equals(result)) {
System.out.println( "i=" + i + " OK: " + result);
} else {
System.err.println( "i=" + i + " ERROR: " + result);
}
fix = result;
Thread.sleep( 500 );
}
// LRU的缺省cache.size为1000,执行1001次,应有溢出
for ( int n = 0 ; n < 1001 ; n++) {
String pre = null ;
for ( int i = 0 ; i < 10 ; i++) {
String result = cacheService.findCache(String.valueOf(n));
if (pre != null && !pre.equals(result)) {
System.err.println( "n=" + n + " ERROR: " + result);
}
pre = result;
}
}
// 测试LRU有移除最开始的一个缓存项
String result = cacheService.findCache( "0" );
if (fix != null && !fix.equals(result)) {
System.out.println( "OK: " + result);
} else {
System.err.println( "ERROR: " + result);
}
}
}
|