如何在重试 struts 操作之前休眠?
How to sleep before retrying in struts action?
我有一个用例,我的 struts 操作从文件系统读取文件,然后 returns 在服务器响应中读取它。我想在重试读取文件之前让我的请求休眠一段时间的地方添加重试逻辑,实现该目标的最佳方法是什么?
我想在每次重试之间等待 1 秒后重试 10 次。我发现 Thread.sleep(1000) 使当前线程进入睡眠状态。这是正确的方法吗?
public String execute()
{
for(int i = 0; i < 10; i++) {
// Read the file system
if (break_condition) {
break;
}
Thread.sleep(1000);
}
}
有没有更好的方法来实现这个?
最好不要在服务器上下文中使用 Thread.sleep
,因为它可能会产生不必要的影响。
建议的方法会因可用的服务器和框架而异。然而,这个想法的核心是你使用特定的 API 来安排,或者在你的服务器提供的未来做(重试)某事,并避免使用 Thread.sleep()
.
关键区别在于线程不会休眠并在继续进行之前保持空闲状态。线程将在特定时间后通知服务器执行某些操作,然后线程将继续工作。
如果你在 Java-EE 环境中,be TimerService 会很好 idea.It 可以用 TimerService.createSingleActionTimer()
实现。
例如,如果您在 Jave EE 服务器中,您可以执行以下操作:
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Timer;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.TimerConfig;
@Stateless
public class RetryWithWaitBean {
@Resource
private SessionContext context;
/**
*Create a timer that will be activated after the duration passes.
*/
public void doActionAfterDuration(long durationMillis) {
final TimerConfig timerConfig= new TimerConfig()
timerConfig.setPersistent(false);
context.getTimerService()..createSingleActionTimer(durationMillis,timerConfig);
}
/** Automatically executed by server on timer expiration.
*/
@Timeout
public void timeout(Timer timer) {
System.out.println("Trying after timeout. Timer: " + timer.getInfo());
//Do custom action
doAction();
timer.cancel();
}
/**
* Doing the required action
*/
private void doAction(){
//add your logic here. This code will run after your timer.
System.out.println("Action DONE!");
}
}
然后你就可以这样使用了:
//This code should be in a managed context so that the server injects it.
@EJB
private RetryWithWaitBean retryWithWaitBean ;
那你就可以这样用了
//do an action after 3000 milliseconds
retryWithWaitBean.doActionAfterDuration(3000);
根据您使用的框架,有很多方法可以实现类似的结果。
我有一个用例,我的 struts 操作从文件系统读取文件,然后 returns 在服务器响应中读取它。我想在重试读取文件之前让我的请求休眠一段时间的地方添加重试逻辑,实现该目标的最佳方法是什么?
我想在每次重试之间等待 1 秒后重试 10 次。我发现 Thread.sleep(1000) 使当前线程进入睡眠状态。这是正确的方法吗?
public String execute()
{
for(int i = 0; i < 10; i++) {
// Read the file system
if (break_condition) {
break;
}
Thread.sleep(1000);
}
}
有没有更好的方法来实现这个?
最好不要在服务器上下文中使用 Thread.sleep
,因为它可能会产生不必要的影响。
建议的方法会因可用的服务器和框架而异。然而,这个想法的核心是你使用特定的 API 来安排,或者在你的服务器提供的未来做(重试)某事,并避免使用 Thread.sleep()
.
关键区别在于线程不会休眠并在继续进行之前保持空闲状态。线程将在特定时间后通知服务器执行某些操作,然后线程将继续工作。
如果你在 Java-EE 环境中,be TimerService 会很好 idea.It 可以用 TimerService.createSingleActionTimer()
实现。
例如,如果您在 Jave EE 服务器中,您可以执行以下操作:
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Timer;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.TimerConfig;
@Stateless
public class RetryWithWaitBean {
@Resource
private SessionContext context;
/**
*Create a timer that will be activated after the duration passes.
*/
public void doActionAfterDuration(long durationMillis) {
final TimerConfig timerConfig= new TimerConfig()
timerConfig.setPersistent(false);
context.getTimerService()..createSingleActionTimer(durationMillis,timerConfig);
}
/** Automatically executed by server on timer expiration.
*/
@Timeout
public void timeout(Timer timer) {
System.out.println("Trying after timeout. Timer: " + timer.getInfo());
//Do custom action
doAction();
timer.cancel();
}
/**
* Doing the required action
*/
private void doAction(){
//add your logic here. This code will run after your timer.
System.out.println("Action DONE!");
}
}
然后你就可以这样使用了:
//This code should be in a managed context so that the server injects it.
@EJB
private RetryWithWaitBean retryWithWaitBean ;
那你就可以这样用了
//do an action after 3000 milliseconds
retryWithWaitBean.doActionAfterDuration(3000);
根据您使用的框架,有很多方法可以实现类似的结果。