异步接口支持AsyncHandler子类

This commit is contained in:
Redkale
2017-03-22 16:05:10 +08:00
parent eb57a25698
commit bce498885e
11 changed files with 362 additions and 32 deletions

View File

@@ -85,6 +85,10 @@ public class ABMainService implements Service {
url = "http://127.0.0.1:" + abport + "/pipes/abmain/asyncabtime/张先生";
System.out.println(Utility.postHttpContent(url));
//异步方法
url = "http://127.0.0.1:" + abport + "/pipes/abmain/asyncabtime2/张先生";
System.out.println(Utility.postHttpContent(url));
server.shutdown();
}
@@ -120,6 +124,10 @@ public class ABMainService implements Service {
url = "http://127.0.0.1:" + abport + "/pipes/abmain/asyncabtime/张先生";
System.out.println(Utility.postHttpContent(url));
//异步方法
url = "http://127.0.0.1:" + abport + "/pipes/abmain/asyncabtime2/张先生";
System.out.println(Utility.postHttpContent(url));
server.shutdown();
//远程模式
remotemain(args);
@@ -162,4 +170,25 @@ public class ABMainService implements Service {
if (handler != null) handler.failed(t, a);
}), name);
}
@RestMapping(name = "asyncabtime2")
public void abCurrentTime(final MyAsyncHandler<String, Void> handler, @RestParam(name = "#") final String name) {
bcService.bcCurrentTime(new MyAsyncHandler<String, Void>() {
@Override
public int id() {
return 1;
}
@Override
public void completed(String v, Void a) {
System.out.println("执行了 ABMainService.abCurrentTime----异步方法2");
String rs = "异步abCurrentTime: " + v;
if (handler != null) handler.completed(rs, a);
}
@Override
public void failed(Throwable exc, Void attachment) {
}
}, name);
}
}

View File

@@ -33,4 +33,14 @@ public class BCService implements Service {
if (handler != null) handler.failed(t, a);
}), name);
}
public void bcCurrentTime(final MyAsyncHandler<String, Void> handler, final String name) {
cService.ccCurrentTime(AsyncHandler.create((v, a) -> {
System.out.println("执行了 BCService.bcCurrentTime----异步方法2");
String rs = "异步bcCurrentTime: " + (v == null ? null : v.getResult());
if (handler != null) handler.completed(rs, null);
}, (t, a) -> {
if (handler != null) handler.failed(t, a);
}), name);
}
}

View File

@@ -25,4 +25,10 @@ public class CService implements Service {
System.out.println("执行了 CService.ccCurrentTime----异步方法");
if (handler != null) handler.completed(new RetResult(rs), null);
}
public void mcCurrentTime(final MyAsyncHandler<RetResult<String>, Void> handler, final String name) {
String rs = "异步mcCurrentTime: " + name + ": " + Utility.formatTime(System.currentTimeMillis());
System.out.println("执行了 CService.mcCurrentTime----异步方法2");
if (handler != null) handler.completed(new RetResult(rs), null);
}
}

View File

@@ -0,0 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.test.service;
import org.redkale.util.AsyncHandler;
/**
*
* @author zhangjx
* @param <V> V
* @param <A> A
*/
public abstract class MyAsyncHandler<V, A> implements AsyncHandler<V, A> {
public abstract int id();
}