Configure shared session using Package SDK
- Updated: 2023/04/05
Configure shared session using Package SDK
Share session between parent and child
-
Create the following POJO class DemoForSession that implements CloseableSessionObject.
package com.automationanywhere.botcommand.samples.commands.basic.GlobalSessionSampleProject; import com.automationanywhere.toolchain.runtime.session.CloseableSessionObject; import java.io.IOException; public class DemoForSession implements CloseableSessionObject { public void setClose(boolean close) { this.close = close; } boolean close=false; public String getDemo() { return demo; } public void setDemo(String demo) { this.demo = demo; } public DemoForSession(String demo){ this.demo=demo; } String demo; @Override public boolean isClosed() { return close; } @Override public void close() throws IOException { } }
- Add the
@BotCommand
annotation to make a class as an Action in the Control Room. For more information on annotations, see Annotations.@BotCommand
- Add the
@CommandPkg
annotation to define all the UI related components - labels, description , icon, return_type, and name. The elements return_label, return_settings, return_type, and return_required belonging to the@CommandPkg
ensures that the SESSION is returned by the parent bot.@CommandPkg( //Unique name inside a package and label to display. name = "GlobalSession", label = "Global session uppercase parent", node_label = "Shared session parent node label", description = "Shared session parent description", icon = "pkg.svg", //Return type information. return_type ensures only the right kind of variable is provided on the UI. return_label = "Return the parent session", return_settings = {ReturnSettingsType.SESSION_TARGET}, return_type = SESSION, return_required = true)
-
Create the GlobalSessionDemoUpperCaseParent object. The GlobalSessionDemoUpperCaseParent gets a lower case string input from the action's input, converts it to an upper case string and store the result in the SessionValue.
-
Annotate the method with
@Execute
to execute the method during compilation.package com.automationanywhere.botcommand.samples.commands.basic.GlobalSessionSampleProject; import com.automationanywhere.botcommand.data.impl.SessionValue; import com.automationanywhere.commandsdk.annotations.*; import com.automationanywhere.commandsdk.annotations.rules.NotEmpty; import com.automationanywhere.commandsdk.model.ReturnSettingsType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.Map; import static com.automationanywhere.commandsdk.model.AttributeType.TEXT; import static com.automationanywhere.commandsdk.model.DataType.SESSION; //BotCommand makes a class eligible for being considered as an action. @BotCommand //CommandPks adds required information to be dispalable on GUI. @CommandPkg( //Unique name inside a package and label to display. name = "GlobalSession", label = "Global session uppercase parent", node_label = "Shared session parent node label", description = "Shared session parent description", icon = "pkg.svg", //Return type information. return_type ensures only the right kind of variable is provided on the UI. return_label = "Return the parent session", return_settings = {ReturnSettingsType.SESSION_TARGET}, return_type = SESSION, return_required = true) public class GlobalSessionDemoForStringUpperCaseParent { private static Logger logger = LogManager.getLogger(GlobalSessionDemoForStringUpperCaseParent.class); //Identify the entry point for the action. Returns a SessionValue because the return_type is SESSION. @Execute public SessionValue action( //Idx 1 would be displayed first, with a text box for entering the value. @Idx(index = "1", type = TEXT) //UI labels. @Pkg(label = "Enter string in lower case") //Ensure that a validation error is thrown when the value is null. @NotEmpty String firstString) { String result = firstString.toUpperCase(); DemoForSession demoForSession= new DemoForSession(result); return SessionValue .builder() .withSessionObject(demoForSession) .build(); } }
-
-
Create the GlobalSessionDemoUpperCaseChild object. The GlobalSessionDemoUpperCaseChild gets a lower case string input from the action's input, converts it to upper case and concatenates it with the SessionValue received from the parent bot.
package com.automationanywhere.botcommand.samples.commands.basic.GlobalSessionSampleProject; import com.automationanywhere.botcommand.data.Value; import com.automationanywhere.botcommand.data.impl.StringValue; import com.automationanywhere.botcommand.samples.commands.basic.Uppercase; import com.automationanywhere.commandsdk.annotations.*; import com.automationanywhere.commandsdk.annotations.rules.NotEmpty; import com.automationanywhere.commandsdk.annotations.rules.SessionObject; import com.automationanywhere.commandsdk.model.AttributeType; import com.automationanywhere.commandsdk.model.DataType; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import static com.automationanywhere.commandsdk.model.AttributeType.TEXT; import static com.automationanywhere.commandsdk.model.DataType.STRING; //BotCommand makes a class eligible for being considered as an action. @BotCommand //CommandPks adds required information to be dispalable on GUI. @CommandPkg( //Unique name inside a package and label to display. name = "GlobalSessionChild", label = "Global session upper case child", node_label = "Global session child node label", description = "Global session child description", icon = "pkg.svg", //Return type information. return_type ensures only the right kind of variable is provided on the UI. return_label = "Return the concatenated string to", return_type = STRING, return_required = true) public class GlobalSessionDemoUpperCaseChild { private static Logger logger = LogManager.getLogger(Uppercase.class); @Execute public Value<String> action( //Idx 1 would be displayed first, with a text box for entering the value. @Idx(index = "1", type = TEXT) //UI labels. @Pkg(label = "Enter a sting in lower case to concatenate") //Ensure that a validation error is thrown when the value is null. @NotEmpty String sourceString, @Idx(index = "2", type = AttributeType.SESSION) @Pkg(label = "sharedSession", description = "sharedSession", default_value = "Default", default_value_type = DataType.SESSION) //Using the sessionObject annotation here as its a consumer class @SessionObject DemoForSession session) { //Business logic String result = sourceString.toUpperCase(); logger.info("session: {}",session); return new StringValue(result+session.getDemo().toUpperCase()); } }
- Build and upload the custom package to the Control Room. For more information on uploading the package, see Using the package SDK.
- Create a bot by using the Global session uppercase parent
action.
- Enter a lower case string (For example: parentbot string)
- Enter a name for the session. The parent bot returns the value to the Global session (For example: Global)
- Create a Task Bots by using the Global session upper case child action. You will be able to input a lower case string and also get the string from the shared parent session. Concatenate the parent and child strings.
- Use a Message box to show the output.
- Now call the Global session upper case child into the parent.
- Run the parent bot and you will see the message box showing the value from the Child bot which it derived using the Shared global session.