通过接口调用-启动一个远程应用-用Timer方法开启线程轮巡获取状态完整代码
直接上代码:
package com.fh.util;
import org.apache.http.client.CookieStore;
import java.util.TimerTask;
/**
* Created by CP_xiawei on 2019/9/18.
*/
/**
* 定义自己的TimerTask
*/
public abstract class MyTimerTask extends TimerTask {
public String jid;
public CookieStore cookieStore;
public FlagStatusListener flagListener;
public boolean flag = false;
//定义构造方法
public MyTimerTask(String jid, CookieStore cookieStore,FlagStatusListener flagListener) {
super();
this.jid = jid;
this.cookieStore = cookieStore;
this.flagListener = flagListener;
}
}
================================================================
package com.fh.util;
/**
* Created by CP_xiawei on 2019/9/19.
*/
/**
* 定义监听器
*/
public interface FlagStatusListener {
public void flagValueChanged(boolean newFlag);
}
================================================================
package com.fh.service.AIModel.AIModel.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.service.AIModel.AIModel.AIModelManager;
import com.fh.service.aiparticiple.aiparticiple.AiParticipleManager;
import com.fh.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.*;
import static com.fh.controller.base.BaseController.logBefore;
/**
* 会话启动(调用接口api/exec/ensure)
* @return
* @throws Exception
*/
private String sessionStar(CookieStore cookieStore) throws Exception{
//获取定义的HttpClient对象
CloseableHttpClient client = null;
String url = Config.getConfig().getTagBatchUpdateSessionStarUrl();
if(null != cookieStore) {
try {
//获取定义的HttpClient对象
client = ByPassSSLUtils.getByPassSSLHttpClient(cookieStore);
HttpPost post = new HttpPost(url);
//构建Body
JSONObject jsonObject = new JSONObject();
jsonObject.put("groupId","default");
jsonObject.put("isGroup",true);
//构建消息实体
StringEntity entityStr = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8"));
entityStr.setContentEncoding("UTF-8");
//发送Json格式的数据请求
entityStr.setContentType("application/json");
post.setEntity(entityStr);
//获取Response
CloseableHttpResponse response = client.execute(post);
try {
//获取请求实体
HttpEntity entity = response.getEntity();
if (entity != null) {
int code = response.getStatusLine().getStatusCode();
logger.info("返回状态码:" + code);
if (code == 200) {
//获取响应结果
String result = EntityUtils.toString(entity, Charset.forName("UTF-8"));
logger.info("获取返回结果:" + result);
return result;
} else {
return "fail";
}
} else {
return "fail";
}
} catch (Exception e) {
e.printStackTrace();
return "fail";
} finally {
response.close();
}
}catch(Exception e){
e.printStackTrace();
return "fail";
}finally {
client.close();
}
}else{
return "fail";
}
}
/**
* 启动运行实验
* @param jsonObject
* @param sessionId
* @param projectId
* @return
*/
private String submitTest(JSONObject jsonObject,String sessionId,String projectId,CookieStore cookieStore) throws Exception{
//定义必传参数
//CookieStore cookieStore = null;
//获取定义的HttpClient对象
CloseableHttpClient client = null;
String url = Config.getConfig().getSubmitTestUrl();
try {
//获取定义的HttpClient对象
client = ByPassSSLUtils.getByPassSSLHttpClient(cookieStore);
if(StringUtils.isNotBlank(sessionId) && StringUtils.isNotBlank(projectId)){
url = url+"?sessionId="+sessionId+"&p;
}
}
}