【发布时间】:2019-03-21 02:41:31
【问题描述】:
我使用消费者从队列中获取项目并将这些项目发送到外部 API
public void run(){
try {
while(true){
//will peek() an item from a queue and send it to an external API
sendRequest(item);
thread.sleep(100);
}
} catch(InterruptedException e){
Thread.currentThread().interrupt();
}
}
还有另一种发送请求的方法
public void sendRequest(JSONibject item){
// send request
if(response.getStatus() == 200){
//will remove the item from the queue
}
if(response.getStatus() == 500){
//keep the item in the queue and set time for resending
}
}
所以我想做的是当我得到 500 响应时,这意味着服务器没有启动。我会将这些项目保留在队列中,并尝试每 5 分钟重新发送这些项目。在这种情况下如何控制 while 循环?
【问题讨论】:
标签: java time while-loop timer