问题描述
我有一个用Java编写的Webervice和一个客户端。客户端同步调用服务。我可能碰巧该Web服务不可用,或者可能在执行过程中死了。
当我为jersey客户端设置连接并读取超时属性时,执行线程卡住了。
以下是该服务的代码:
@Path("/connection")
@Singleton
public class ConnectionJaxRSService {
private static final Logger log = LogManager.getLogger("DetectionLogger");
@Inject
private ExecutorService executor;
private static copyOnWriteArrayList<Callable<Boolean>> services = new copyOnWriteArrayList<>();
private static volatile boolean running;
public ConnectionJaxRSService() {
running = false;
}
@GET
@Path("/check")
public Response getStatus(@QueryParam("c") int cNumber,@QueryParam("section") String section) {
Config cfg = Util.getConfigurationFromXML(c);
int pos = 0;
Boolean erg = Boolean.FALSE;
if(!running && services.isEmpty()) {
for(IrC irC : cfg.getIrC()) {
Callable<Boolean> service = new IrCConnection(irC.getIp(),irC.getStream(),irC.getimageWidth(),irC.getimageHeight(),irC.getFps(),irC.getRtspTransportOption(),irC.getPosition(),section);
services.add(service);
}
List<Future<Boolean>> results = null;
try {
running = true;
results = executor.invokeAll(services);
if(results != null && !results.isEmpty()) {
for (Future<Boolean> future : results) {
if(future.isDone()) {
try {
if(future.get().equals(Boolean.FALSE)) {
break;
}else {
pos++;
}
} catch (InterruptedException | ExecutionException e) {
log.error("Error iterating through result list: "+e.getMessage());
}
}
}
}
} catch (InterruptedException e) {
log.error("InterruptedException on calling the connection check: "+e.getMessage());
} finally {
if(services != null && !services.isEmpty()) {
for(int i=0; i<services.size(); i++) {
IrCConnection irc = (IrCConnection) services.get(i);
irc.terminate();
}
services.clear();
running = false;
}
}
}else {
log.warn("Services List is not empty or running is set to true: ");
log.warn("services-size:"+services.size());
log.warn("running: "+running);
}
if(pos == 3) {
erg = Boolean.TRUE;
return Response.status(Status.OK).entity(Boolean.TRUE).build();
}else {
return Response.status(Status.CONFLICT).entity(Boolean.FALSE).build();
}
}
}
客户端是一个简单的实现:
this.checkConnectionClient = ClientBuilder.newBuilder()
.property(ClientProperties.CONNECT_TIMEOUT,7_000)
.property(ClientProperties.READ_TIMEOUT,7_000)
.build();
String webtargetUri = this.connectionCheckServiceUrl+"/check?c="+this.sps.getCNumber()+"§ion="+section;
log.info("Set connectioncheck WebTarget URI to: "+webtargetUri);
this.checkConnectionTarget = this.checkConnectionClient.target(webtargetUri);
javax.ws.rs.core.Response state = this.cameraServiceRunTarget.request().get();
如果我设置了连接并读取了超时,则调用线程保持静止。如果我删除这些属性,那么所有想法都会起作用,但是如果服务在调用客户端的过程中中断,那么该请求将再次停滞。
我是否错过了连接和读取超时有效的依赖项或附加调用?
我该怎么做才能使客户更有弹性?读取和连接时间的属性不起作用。
谢谢 曼努埃尔
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)