Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts2 - Delaying an action execution

I have 2 different actions and I'm getting responses to those 2 actions at the same time, from the same host.

I want to delay one action for 30 seconds. I tried using Thread.sleep(30000) but, it seems to delay both the actions.

Both actions are background processes, hence I cannot use the ExecuteAndWaitInterceptor.

Below is ths action that I want to delay.

@Namespace(StrutsConstants.Namespace.PUBLIC)
public class CCAveDNHandlerAction extends  BaseRequestResponseAwareAction {

    private static Log log = LogFactory.getLog(CCAveDNHandlerAction.class);

    public String execute() throws ModuleException {

        log.info("CCAveDNHandlerAction ### Starting..");

        try {               
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

Struts2 documentation says that 2 different requests are executed in 2 different threads.

But in this scenario it is not happening. May be because the 2 requests are coming from the same host. Is there a way to force an action to get executed in a separate context ?

Here is the work flow.

  • User is redirected to an external payment gateway page.
  • User make a successful payment and redirected back to a predefined action in my app.
  • At the same time payment gateway sends another response to another action of my app.(This is a secondary response)
  • I use Thread.sleep for this action and the whole process is paused.
  • I want to pause only the secondary response. How can I do this?
like image 856
tharindu_DG Avatar asked Feb 03 '26 19:02

tharindu_DG


1 Answers

You can use Timer and do TimerTask definitions calling your actions. Then all you need to do is basically do a schedule for each timertask to be invoked and therefore invoke your action.

More on this in the javadoc@ http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html

GL

like image 101
vlex Avatar answered Feb 05 '26 09:02

vlex