一种在线程池中执行目标代码,异步获取返回值的方式。
仅供参考,欢迎拍砖。
/**
* 异步返回结果
* @author benyi
* @date 2017-12-9
*
*/
public class AsyResult {
private String tid;
private Object result;
private boolean finished=false;
public AsyResult(String id){
tid=id;
finished=false;
}
public String getId(){
return tid;
}
public void setResult(Object r){
result=r;
finished=true;
}
public Object getResult(){
return result;
}
public boolean isFinished(){
return finished;
}
public Exception getException(){
if(result!=null){
if(haveException()){
return (Exception)result;
}
}
return null;
}
public boolean haveException(){
if(result!=null){
if(result instanceof Exception ){
return true;
}
}
return false;
}
}
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 一个并行调用远程接口的工具
* 基本思路:调用者 添加到要调用的方法到本对象,获取回执ID,本对象代理执行方法,将返回值通知给调用者
* 调用者同步等待返回值, 或者直到超时
* @author admin
*
*/
public class ParalRunner {
private static final Logger log=LoggerFactory.getLogger(ParalRunner.class);
private ExecutorService threadPool = Executors.newCachedThreadPool();
public ParalRunner(){
//
}
public AsyResult putTask(CallTask task){
if(task==null){
return null;
}
if( !task.passCheck() ){
return null;
}
AsyResult a=new AsyResult(task.getTaskId());
boolean b=putThreadTask(task,a);
if(!b){
return null;
}
return a;
}
public static Method getMethod(Object obj,String name){
Method[] ms=obj.getClass().getMethods();
for(int i=0;i
return ms[i];
}
}
return null;
}
private boolean putThreadTask(CallTask task,AsyResult ret){
threadPool.execute(new TaskRunner(task,ret));
return true;
}
class TaskRunner implements Runnable {
public CallTask task;
public AsyResult result;
public TaskRunner(CallTask t,AsyResult r){
this.task=t;
this.result=r;
}
public void run(){
Method m=task.getMethod();
Object obj=task.getObj();
Object []ps=task.getParameters();
if(ps==null){
ps=new Object[0];
}
try {
Object ret=m.invoke(obj, ps);
result.setResult(ret);
} catch (Exception e) {
result.setResult(e);
log.error("执行:"+obj.getClass()+"."+m.getName()+"异常:"+e.getMessage(),e);
}
}
}
}
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
/** *测试*/
public class TestParal {
private static ParalRunner runner=new ParalRunner();
private PrintStream out;
public String test() throws Exception {
URL url = new URL("http://goods.scn.wl.cn/goodsRestApi/queryGoodsList?enterpriseid=157");
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb=new StringBuilder();
String current;
while((current = in.readLine()) != null){
sb.append(current);
}
in.close();
connection=null;
return sb.toString();
}
public String testPrice(String goodsids) throws Exception {
URL url = new URL("http://test.goods.scn.wl.cn/goodsRestApi/getPriceYN?enterpriseid=138&goodstype=0&goodsid="+goodsids);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb=new StringBuilder();
String current;
while((current = in.readLine()) != null){
sb.append(current);
}
in.close();
connection=null;
return sb.toString();
}
public String testStorage(String goodsids) throws Exception {
URL url = new URL("http://test.storage.scn.wl.cn/queryReport/getStockYN?enterpriseid=138&goodsid="+goodsids);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb=new StringBuilder();
String current;
while((current = in.readLine()) != null){
sb.append(current);
}
in.close();
connection=null;
return sb.toString();
}
public AsyResult putTast(String method,Object []ps){
CallTask c=new CallTask(this,runner.getMethod(this, method),ps);
AsyResult r=runner.putTask(c);
out.println("Add-id=" + r.getId() );
return r;
}
public void getReturn(AsyResult []ids){
long t1=System.currentTimeMillis();
boolean quit=false;
while(!quit){
try{
Thread.sleep(32);
}catch(Exception e){
e.printStackTrace();
}
quit=true;
for(int i=0;i
if(ids[i].isFinished()){
Object o=ids[i].getResult();
if(ids[i].haveException()){
Exception e=ids[i].getException();
e.printStackTrace(out);
}else{
out.println("ID="+ids[i].getId()+",Return="+o);
}
out.println("ID="+ids[i].getId()+",Time="+(System.currentTimeMillis()-t1));
ids[i]=null;
}else{
quit=false;
}
}
}
}
out.println("Total Time="+(System.currentTimeMillis()-t1));
}
public static void main(String []args) throws Exception {
TestParal t=new TestParal();
t.out = new PrintStream(new FileOutputStream("/d:/test.log"));
AsyResult a1=t.putTast("testPrice",new Object[]{"139949"});
AsyResult a2=t.putTast("testStorage",new Object[]{"139949"});
AsyResult a3=t.putTast("testPrice",new Object[]{"139949"});
AsyResult a4=t.putTast("testPrice",new Object[]{"139949"});
AsyResult []ids={a1,a2,a3,a4};
t.getReturn(ids);
t.out.close();
}
}