弹幕下载

master
10295 7 years ago
parent 14e3171722
commit a024c69641
  1. 15
      core/src/main/java/core/thrift/FactoryPool.java
  2. 7
      core/src/main/java/core/thrift/MyAsyncMethodCallback.java
  3. 12
      web/src/main/java/web/controller/DataController.java
  4. 11
      web/src/test/java/thrift/ThriftTest.java

@ -39,19 +39,20 @@ public class FactoryPool {
tNonblockingSocketPool.returnObject(trans);
}
public <C extends TServiceClient> void doExecute(Class<C> c, ServiceClient<C> thrift) throws Exception{
public <C extends TServiceClient,T> void doExecute(Class<C> c, ServiceClient<C,T> thrift,T t) throws Exception{
TSocket trans=tSocketPool.borrowObject();
thrift.doExecute(client(c,trans));
thrift.doExecute(client(c,trans),t);
tSocketPool.returnObject(trans);
}
private interface Thrift<E> {
void doExecute(E client) throws Exception;
}
public interface AsyncClient<E extends TAsyncClient> extends Thrift<E>{}
public interface AsyncClient<C extends TAsyncClient>{
void doExecute(C client) throws Exception;
}
public interface ServiceClient<E extends TServiceClient> extends Thrift<E>{}
public interface ServiceClient<C extends TServiceClient,T>{
void doExecute(C client,T t) throws Exception;
}
public <T extends TTransport,C> C client(Class<C> c, T trans) throws Exception {
if (trans instanceof TNonblockingTransport) {

@ -9,12 +9,13 @@ public class MyAsyncMethodCallback<E> implements AsyncMethodCallback<E> {
private E result;
public E getResult() throws InterruptedException {
while (onComplete){
while (result==null&&onComplete){
Thread.sleep(100);
}
return result;
}
@Override
public void onComplete(E o) {
onComplete=false;
@ -25,4 +26,8 @@ public class MyAsyncMethodCallback<E> implements AsyncMethodCallback<E> {
public void onError(Exception e) {
e.printStackTrace();
}
public void setResult(E result) {
this.result = result;
}
}

@ -1,6 +1,8 @@
package web.controller;
import core.thrift.FactoryPool;
import core.thrift.MyAsyncMethodCallback;
import core.thrift.comment.QueryComment;
import db.form.DBAction;
import db.model.bilibili.DataModel;
import org.apache.commons.io.FileUtils;
@ -22,6 +24,7 @@ import web.html.data.DataTable;
import web.model.DataModelForm;
import web.service.DataService;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
@ -36,6 +39,9 @@ public class DataController extends TableController<DataModel, DataModelForm, Da
private static final String key = "regionJson";
@Resource
private FactoryPool factoryPool;
public DataController() throws IOException {
super();
}
@ -51,9 +57,9 @@ public class DataController extends TableController<DataModel, DataModelForm, Da
@RequestMapping(value = "count/{cid}")
public Integer count(@PathVariable int cid) {
try {
MyAsyncMethodCallback<Integer> call = new MyAsyncMethodCallback<>();
// TTransportUtil.doExecute(client -> client.commentSum(cid, call),QueryComment.AsyncClient.class);
return call.getResult();
MyAsyncMethodCallback<Integer> result=new MyAsyncMethodCallback<>();
factoryPool.doExecute(QueryComment.Client.class, (client, aBoolean) ->result.setResult(client.commentSum(cid)),result);
return result.getResult();
} catch (Exception e) {
log.error(e);
}

@ -2,7 +2,7 @@ package thrift;
import core.thrift.FactoryPool;
import core.thrift.MyAsyncMethodCallback;
import core.thrift.task.TSDM;
import core.thrift.comment.QueryComment;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
@ -26,15 +26,18 @@ public class ThriftTest {
@Test
public void test1() throws Exception {
MyAsyncMethodCallback<Boolean> call=new MyAsyncMethodCallback<>();
factoryPool.doExecute(TSDM.AsyncClient.class,client -> client.qiandao(call));
MyAsyncMethodCallback<Integer> call=new MyAsyncMethodCallback<>();
factoryPool.doExecute(QueryComment.AsyncClient.class,client -> client.commentSum(49052,call));
log.info(call.getResult());
}
@Test
public void test2() throws Exception{
factoryPool.doExecute(TSDM.Client.class, client -> log.info(client.qiandao()));
MyAsyncMethodCallback result=new MyAsyncMethodCallback();
factoryPool.doExecute(QueryComment.Client.class, (client, aBoolean) ->
result.setResult(client.commentSum(49052)),result);
log.info(result.getResult());
}
}

Loading…
Cancel
Save