Search in sources :

Example 1 with Future

use of com.weibo.api.motan.rpc.Future 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)

Example 2 with Future

use of com.weibo.api.motan.rpc.Future in project motan by weibocom.

the class NettyResponseFutureTest method testListener.

@Test
public void testListener() {
    DefaultRequest request = new DefaultRequest();
    NettyResponseFuture response = new NettyResponseFuture(request, 100, client);
    final AtomicBoolean result = new AtomicBoolean(false);
    response.addListener(new FutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                result.set(true);
            } else {
                result.set(false);
            }
        }
    });
    DefaultResponse defaultResponse = new DefaultResponse();
    defaultResponse.setValue(new Object());
    response.onSuccess(defaultResponse);
    Assert.assertTrue(result.get());
    response = new NettyResponseFuture(request, 100, client);
    response.addListener(new FutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                result.set(true);
            } else {
                result.set(false);
            }
        }
    });
    response.cancel();
    result.set(true);
    response.addListener(new FutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            if (future.isSuccess()) {
                result.set(true);
            } else {
                result.set(false);
            }
        }
    });
    Assert.assertFalse(result.get());
}
Also used : FutureListener(com.weibo.api.motan.rpc.FutureListener) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Future(com.weibo.api.motan.rpc.Future) Test(org.junit.Test)

Example 3 with Future

use of com.weibo.api.motan.rpc.Future in project motan by weibocom.

the class Motan2RpcClient method motan2XmlCommonClientDemo.

public static void motan2XmlCommonClientDemo(CommonHandler client) throws Throwable {
    System.out.println(client.call("hello", new Object[] { "a" }, String.class));
    User user = new User(1, "AAA");
    System.out.println(user);
    user = (User) client.call("rename", new Object[] { user, "BBB" }, User.class);
    System.out.println(user);
    ResponseFuture future = (ResponseFuture) client.asyncCall("rename", new Object[] { user, "CCC" }, User.class);
    user = (User) future.getValue();
    System.out.println(user);
    ResponseFuture future2 = (ResponseFuture) client.asyncCall("rename", new Object[] { user, "DDD" }, User.class);
    future2.addListener(new FutureListener() {

        @Override
        public void operationComplete(Future future) {
            System.out.println(future.getValue());
        }
    });
    Request request = client.buildRequest("rename", new Object[] { user, "EEE" });
    request.setAttachment("a", "a");
    user = (User) client.call(request, User.class);
    System.out.println(user);
// expect throw exception
// client.call("rename", new Object[]{null, "FFF"}, void.class);
}
Also used : FutureListener(com.weibo.api.motan.rpc.FutureListener) User(com.weibo.motan.demo.service.model.User) Request(com.weibo.api.motan.rpc.Request) 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)3 FutureListener (com.weibo.api.motan.rpc.FutureListener)3 ResponseFuture (com.weibo.api.motan.rpc.ResponseFuture)2 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)1 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)1 Request (com.weibo.api.motan.rpc.Request)1 MotanDemoServiceAsync (com.weibo.motan.demo.service.MotanDemoServiceAsync)1 User (com.weibo.motan.demo.service.model.User)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1