Search in sources :

Example 1 with MotanDemoServiceAsync

use of com.weibo.motan.demo.service.MotanDemoServiceAsync in project motan by weibocom.

the class DemoRpcAsyncClient method main.

/**
     * 使用motan异步调用方法: 1、在声明的service上增加@MotanAsync注解。 如MotanDemoService
     * 2、在项目pom.xml中增加build-helper-maven-plugin,用来把自动生成类的目录设置为source path。 参见motan-demo-api模块的pom声明。
     * 也可以不使用plugin,手动将target/generated-sources/annotations目录设置为source path。
     * 3、在client配置的motan:referer标签中配置interface为自动生成的以Async为后缀的对应service类。
     * 参见本模块的motan_demo_async_client.xml
     * 
     */
public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "classpath:motan_demo_async_client.xml" });
    MotanDemoServiceAsync service = (MotanDemoServiceAsync) ctx.getBean("motanDemoReferer");
    for (int i = 0; i < Integer.MAX_VALUE; i++) {
        // sync call
        System.out.println(service.hello("motan" + i));
        // async call
        ResponseFuture future = service.helloAsync("motan async " + i);
        System.out.println(future.getValue());
        // multi call
        ResponseFuture future1 = service.helloAsync("motan async multi-1-" + i);
        ResponseFuture future2 = service.helloAsync("motan async multi-2-" + i);
        System.out.println(future1.getValue() + ", " + future2.getValue());
        // async with listener
        ResponseFuture future3 = service.helloAsync("motan async multi-1-" + i);
        ResponseFuture future4 = service.helloAsync("motan async multi-2-" + i);
        FutureListener listener = new FutureListener() {

            @Override
            public void operationComplete(Future future) throws Exception {
                System.out.println("async call " + (future.isSuccess() ? "sucess! value:" + future.getValue() : "fail! exception:" + future.getException().getMessage()));
            }
        };
        future3.addListener(listener);
        future4.addListener(listener);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println("motan demo is finish.");
    System.exit(0);
}
Also used : FutureListener(com.weibo.api.motan.rpc.FutureListener) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MotanDemoServiceAsync(com.weibo.motan.demo.service.MotanDemoServiceAsync) Future(com.weibo.api.motan.rpc.Future) ResponseFuture(com.weibo.api.motan.rpc.ResponseFuture) ResponseFuture(com.weibo.api.motan.rpc.ResponseFuture)

Aggregations

Future (com.weibo.api.motan.rpc.Future)1 FutureListener (com.weibo.api.motan.rpc.FutureListener)1 ResponseFuture (com.weibo.api.motan.rpc.ResponseFuture)1 MotanDemoServiceAsync (com.weibo.motan.demo.service.MotanDemoServiceAsync)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1