About the WSContextManager class and the WSRunnable interface
The WSContextManager class provides methods that allow for any object that implements the WSRunnable interface to be executed within a single transaction. The WSRunnable interface defines a run method that provides access to the WSContext object.
The
WSRunnable interface is not an object access interface. You need to implement this interface to create WorldServer SDK applications that can be executed outside of a hosted WorldServer environment. The WSRunnable interface is defined as follows:
public interface WSRunnable {
public boolean run(WSContext wsContext);
}
The
WSContextManager class contains certain methods that allow you to run a WSRunnable class instance, such as:
| Method | Requires you to provide.. |
|---|---|
public static void runAsUser(String username, String password, WSRunnable runnable) | The user name and password for authentication. |
public static void runAsUser(String username, String password, WSRunnable runnable, int retryCount) | The user name and password for authentication; also accepts a retry parameter which specifies how many times the runnable process can be retried in case of an exception. |
public static void run(WSContext context, WSRunnable runnable) | An existing context object from which credentials will be derived. |
You can use these methods in multiple ways, as shown in the following code snippets:
- Executing explicit class instances (via the
mainmethod of theWSRunnableclass):public class MySample implements WSRunnable { public boolean run(WSContext wsContext) { { try { // TO-DO: Provide custom code } catch (Exception ex) { // handle exception } } public static void main(String[] args) { MySample sample = new MySample(); WSContextManager.runAsUser(“username”, “password”, sample); } } - Executing anonymous class instances:
public class MySample { public void doSDKProcess(WSContext wsContext) { { // creates an anonymous WSRunnable process that // is executed WSContextManager.run(“username”, “password”, new WSRunnable() { public boolean run(WSContext wsContext) { // TO-DO: Do something useful return true; } // close anonymous run method }); } }
Creating a stand-alone SDK application or an SDK utility class is essentially the same as writing any standard Java program, because you need to implement the main method. You can call the WSContextManager explicitly to execute your SDK-enabled code using either explicit or anonymous class instances.