我是一个调用Rest Web服务从服务器接收文件的类。传输字节时,我已经创建了一个异步任务,它会检查与服务器的连接是否正常,以便在出现错误时允许停止连接。 这个异步任务有一个我必须停止的循环:
@Component
public class ConnectionTest {
@Async
//Check connection with the server, if for three attemp it failes, throw exception
public void checkServerConnection(String serverIp) throws Exception{
int count=0;
for(;;Thread.sleep(7000)){
try{
System.out.println("TEST");
URL url = new URL(serverIp);
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
con.connect();
if (con.getResponseCode() == 200){
System.out.println("Connection established!!");
}
if (count>0) count=0;
}catch(Exception e){
count++;
if (count==3)
throw new Exception("Connection error");
}
}
}
}
但是如何从调用者处停止此方法?
@Autowired
private ConnectionTest connectionTest;
@Override
public Response getFile(String username, String password, String serverIp, String toStorePath, String filePath){
ResponseEntity<byte[]> responseEntity = null;
try{
//it is used to check if connection of the client with the server goes down
connectionTest.checkServerConnection();
RestClient restClient = new RestClient(username, password);
// SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
// requestFactory.setBufferRequestBody(false);
// restClient.setRequestFactory(requestFactory);
// RestTemplate restClient = new RestTemplate();
responseEntity = restClient.getForEntity(serverIp + "client/file/?filePath={filePath}", byte[].class, filePath);
//TODO kill async task and return false
更新 :正如@Thomas建议我在ConnectionTest中使用了一个布尔变量,我改为循环使用while (!stop)
并在网络之后服务电话我设置了ConnectionTest.setStop(true)
。
注意在循环之前设置stop = false(而不是实例字段),否则只有第一个请求具有此值并进入while。
更新2 这是我的最后一个代码,它似乎工作,也许我应该在使用wait-notify循环时更改:
public Response getFile(String username, String password, String serverIp, String toStorePath, String filePath){
try{
//it is used to check if connection of the client with the server goes down
Future<Boolean> isConnect = connectionTest.checkServerConnection(serverIp);
Future<ResponseEntity<byte[]>> downloadResult = downloadAsync.makeRequest(username, password, serverIp, filePath);
while(!isConnect.isDone() && !downloadResult.isDone()){
}
if (isConnect.isDone()){
downloadResult.cancel(true);
return new Response(false, false, "Error with server connection!", null);
}else{
connectionTest.setStop(true);
ResponseEntity<byte[]> responseEntity = downloadResult.get();
if (MediaType.TEXT_PLAIN.toString().equals(responseEntity.getHeaders().getContentType().toString())){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(new FileException("Error with file transfert!"));
return new Response(false, false, new String(Base64.decodeBase64(responseEntity.getBody()),Charset.forName("UTF-8")), errorResponse);
}else{
Path p = Paths.get(filePath);
String fileName = p.getFileName().toString();
FileOutputStream fos = new FileOutputStream(toStorePath+"\\"+ fileName);
fos.write(responseEntity.getBody());
fos.close();
return new Response(true, true, "Your file has been downloaded!", null);
}
}
}catch(Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
return new Response(false, false, "Error on the client side!" , errorResponse);
}
}
连接检查异步:
@Component
public class ConnectionTest {
private boolean stop;
@Async
//Check connection with the server, if for three attemp it failes, throw exception
/**
*
* @param serverIp
* @throws IOException
*/
public Future<Boolean> checkServerConnection(String serverIp) throws IOException {
int count=0;
stop = false;
while (!stop){
try{
Thread.sleep(7000);
System.out.println("TEST");
//java.net.InetAddress.getByName(SERVER_ADDRESSS);
URL url = new URL(serverIp);
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
con.connect();
if (count>0) count=0;
}catch(Exception e){
count++;
System.out.println(count);
if (count==3)
return new AsyncResult<Boolean>(stop);
}
}
return new AsyncResult<Boolean>(stop);
}
/**
* @return the stop
*/
public boolean isStop() {
return stop;
}
/**
* @param stop the stop to set
*/
public void setStop(boolean stop) {
this.stop = stop;
}
}
下载async:
@Component
public class DownloadAsync {
@Async
public Future<ResponseEntity<byte[]>> makeRequest(String username, String password, String serverIp, String filePath){
RestClient restClient = new RestClient(username, password);
ResponseEntity<byte[]> response= restClient.getForEntity(serverIp + "client/file/?filePath={filePath}", byte[].class, filePath);
return new AsyncResult<ResponseEntity<byte[]>>(response);
}
}
答案 0 :(得分:3)
当您处理@Async
方法时,一个好的做法是从中返回Future
对象,因为您需要客户端和任务代码之间的连接点。
让我们让您的任务方法返回Future
:
public Future<Integer> checkServerConnection(String serverIp) {
// other code here
return new AsyncResult<>(count);
}
您需要添加几个导入:
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.AsyncResult;
最后,在客户端代码中,我们获取Future
:
Future<Integer> checkTask = connectionTest.checkServerConnection();
现在,您可以使用checkTask
执行一些有用的操作。例如:
// Check if the task was completed including by an exception being thrown.
checkTask.isDone();
// Get the task result.
Integer count = checkTask.get(); // Note: this is a blocking method.
// If the task was finished by throwing an exception,
// get() method will also throw an exception.
// You can get the cause exception like this:
if (checkTask.isDone()) {
try {
checkTask.get();
} catch(Exception e) {
Exception cause = e.getCause(); // this will be your new Exception("Connection error")
}
}
// Not recommended, but you can also cancel the task:
checkTask.cancel(mayInterruptIfRunning);
答案 1 :(得分:0)
首先,我不想再对这个问题感到困惑,所以我将为您提供一个高级别的描述。特别是,看看如何使用发布委托在android中非常优雅地完成这项工作。
基本上,发布代理由2部分组成。首先,重写发布更改的方法,以及接收更改的另一种方法。接收更改的时间间隔取决于队列中当前的“CHUNK”大小和数据大小,但通常,您可以将此视为尝试接收发布事件的最佳尝试。
这是一个很重要的高级图片。
的AsyncTask
在背景中(下载时间过长) 背景(发布下载进展)
发布接收方(接收下载更新[也许以百分比表示] 从这里做出决定。
我并没有忽略Spring语境的重要性,但我认为一旦你收到这篇文章,无论框架如何,你都会接受它的适用性。
最佳, 移动开发 AT