Skip to main content

Background Processes

Process Flow to an Execute in Background Action creates a new thread in which to execute the specified Execute Process ID.

Multiple threads executing the same Subprocess may be initiated by specifying the NumberOfThreads Input Item along with a ThreadGroupName by which to access a Thread Group Class of the Standard Java API. If a single thread is initiated the ProcessHandle Output Item may be used to identify the thread and used as described in section Stopping a Background Process following.

The Action returns a Success or Fail Outcome, which relates to initiation of the new process, not the outcome of the Process itself. Process Flow in the Calling Process continues, and the Background Process proceeds, in each of the threads, in parallel with the Calling Process and within the same View.

In this tutorial, you will create a new action that prints the current thread running the main Application process. This action will then be added to both the SignOn process and the newly created Execute in Background process.

What you will learn

  • How to add an Execute in Background action with a specified number of threads to an application process.

Pre-requisite

Should have followed Application Process Tutorial

Adding an Action to Main Application Process

Open the modified SignOn Application process.

From the Outline view, expand the Actions group and search for the action SignOnCompletedExtension

From the Palette, expand the Actions Group, Drag and drop a new Action near SignOnCompletedExtension

Move the link from Success outcome of SignOnCompletedExtension to the new Action.

Double click on the outcome section on the new action and add "Success" as an outcome.

Then, double click on the input section of the action and add a new input with the name enactor.coreUI.ThreadIdPrefix and enter type as java.lang.String.

To add a value to the above input, press SHIFT and double-click on the input section.

Click the Add button, and from the dropdown select enactor.coreUI.ThreadIdPrefix and enter main as the value.

Click OK and Save the changes.

Now, Right click on the action and select show properties.

In the Properties panel modify the Action ID property to PrintCurrentThreadIdAction.

Save the changes.

Your new action should look as below.

Creating a new Action to Log Current ThreadId

Follow the Creating a new Action and create a new action by double clicking on the above action added to Application Process Editor with below data.

  • package : com.enactor.training.actions
  • Name : LogCurrentThread

Import the Enactor logger package as in the Creating a new Action and add the line to log the current thread as follows.

logger.log(Logger.LOG_INFORMATION, String.format("Executing %s thread %d", prefix, threadId));

Remove unused packages and save the changes.

The final LogCurrentThread class should look as follows.

LogCurrentThread.java
package com.enactor.training.actions;

import com.enactor.core.application.process.ApplicationProcessException;
import com.enactor.core.application.process.IApplicationProcessData;
import com.enactor.core.application.process.IApplicationProcessOutcome;
import com.enactor.core.utilities.Logger;
import com.enactor.coreUI.actions.IUIAction;
import com.enactor.coreUI.processes.CoreUIOutcomes;
import com.enactor.coreUI.actions.UIActionFunctions;
import java.lang.String;
import com.enactor.coreUI.annotations.Inputs;
import com.enactor.coreUI.annotations.Input;
import com.enactor.coreUI.annotations.Outputs;
import com.enactor.coreUI.annotations.Outcomes;

@Inputs({ @Input(name = "enactor.coreUI.ThreadIdPrefix", type = java.lang.String.class) })
@Outputs({})
@Outcomes({ "Success" })
public class LogCurrentThread implements IUIAction {

/** Default Serial Version */
private static final long serialVersionUID = 1L;

private static final Logger logger = Logger.getLogger(DisplayThreads.class.getName());

@Override
public IApplicationProcessOutcome execute(IApplicationProcessData inputData, IApplicationProcessData outputData)
throws ApplicationProcessException {

String threadIdPrefix = UIActionFunctions.getOptionalDataItem(inputData, "enactor.coreUI.ThreadIdPrefix");

long threadId = Thread.currentThread().getId();

logger.log(Logger.LOG_INFORMATION, String.format("Executing %s thread %d", threadIdPrefix, threadId));
return CoreUIOutcomes.SUCCESS_OUTCOME;

}

}

Adding an Execute in Background Action

Navigate back to the SIgnOn application process.

From the Palette, expand the Actions Group, Drag and drop a new Execute in Background action near PrintCurrentThreadIdAction.

From the Palette, click the Link tool and then add a link from the Success outcome on the PrintCurrentThreadIdAction action connecting it to the new Execute in Background action.

And connect the Success outcome of new Execute in Background action to LogCurrentUserAction

Right click on the new action and select show properties.

In the Properties panel, modify the Action ID property to ExecuteDispalyingThreadsInBackground.

Set the Execute Process ID as Training/Pos/SignOn/InvokeDisplayingThreads

Save the changes.

In order to add the ThreadIdPrefix input to the new action, drag the ThreadIdPrefix input from the PrintCurrentThreadIdAction action to the input section of the new action.

(Else you can double click on the input section of the action and add a new input with the name enactor.coreUI.ThreadIdPrefix and enter type as java.lang.String)

To add a value to the above input, press SHIFT and double-click on the input section.

Add the following values to the given inputs as you did on Adding an Action previously.

InputValue
enactor.coreUI.ThreadIdPrefixbackground
NumberOfThreads5

Click OK and Save the changes.

The final process connections should look as follows.

Creating Application Process to run in Background

Navigate to the directory path Training/Pos/SignOn

Crate a new Application Process with the InvokeDisplayingThreads following the same steps in Creating a new Application Process

On the new process, double click on the input section and add the following inputs.

InputType
enactor.coreUI.ThreadIdPrefixjava.lang.String
enactor.coreUI.NumberOfThreadsjava.lang.String

Then, Add the Start state with the StateEntered Outcome.

Copy the PrintCurrentThreadIdAction from the SignOn process and paste it next to the Start state.

Then add an EndProcess action with the Success Outcome.

Connect the outcomes of the actions as follows.

Save the changes.

note

If you have add the properties correctly, Once double click on the ExecuteDispalyingThreadsInBackground on the SignOn process, you should be navigated to the newly created InvokeDisplayingThreads process

Re-launch the POS application. After signing in, you should see lines similar to the following appear in the console, showing messages from both the main thread and the background threads.

[AWT-EventQueue-0] INFO com.enactor.training.actions.DisplayThreads - Executing main thread 22:Execute Action:'PrintCurrentThreadIdAction' State:'CheckUserStatus' Process:'Training/Pos/SignOn/SignOn'

[main-Training/Pos/SignOn/InvokeDisplayingThreads-2] INFO com.enactor.training.actions.DisplayThreads - Executing background thread 147:Execute Action:'PrintCurrentThreadIdAction' State:'State' Process:'Training/Pos/SignOn/InvokeDisplayingThreads'
[main-Training/Pos/SignOn/InvokeDisplayingThreads-4] INFO com.enactor.training.actions.DisplayThreads - Executing background thread 149:Execute Action:'PrintCurrentThreadIdAction' State:'State' Process:'Training/Pos/SignOn/InvokeDisplayingThreads'
[main-Training/Pos/SignOn/InvokeDisplayingThreads-5] INFO com.enactor.training.actions.DisplayThreads - Executing background thread 150:Execute Action:'PrintCurrentThreadIdAction' State:'State' Process:'Training/Pos/SignOn/InvokeDisplayingThreads'
[main-Training/Pos/SignOn/InvokeDisplayingThreads-1] INFO com.enactor.training.actions.DisplayThreads - Executing background thread 146:Execute Action:'PrintCurrentThreadIdAction' State:'State' Process:'Training/Pos/SignOn/InvokeDisplayingThreads'
[main-Training/Pos/SignOn/InvokeDisplayingThreads-3] INFO com.enactor.training.actions.DisplayThreads - Executing background thread 148:Execute Action:'PrintCurrentThreadIdAction' State:'State' Process:'Training/Pos/SignOn/InvokeDisplayingThreads'

Further Reading

Refer Actions