使用软件包 SDK 配置共享会话
- Updated: 2023/04/05
在父级和子级之间共享会话
以下部分描述了如何创建共享会话。 父级 GlobalSessionDemoUpperCaseParent 将 Session value 返回到全局会话,并由子机器人 GlobalSessionDemoUpperCaseChild 使用。
注: 将以下三个类放置在以下位置:<PackageSDK>\src\main\java\com\automationanywhere\botcommand\samples\commands\basic
-
创建以下 POJO 类 DemoForSession,实现 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 { } }
- 添加
@BotCommand
注释,使类成为 Control Room 中的操作。 有关注释的更多信息,请参阅 注释。@BotCommand
- 添加
@CommandPkg
注释,以定义所有与 UI 相关的组件 - 标签、描述、图标、返回类型和名称。 属于@CommandPkg
的元素 return_label、return_settings、return_type 和 return_required 确保 SESSION 由父机器人返回。@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)
-
创建 GlobalSessionDemoUpperCaseParent 对象。 GlobalSessionDemoUpperCaseParent 从操作的输入中获取一个小写字符串,将其转换为大写字符串,并将结果存储在 SessionValue 中。
-
使用
@Execute
对该方法进行注释,以便于在编译期间执行该方法。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(); } }
-
-
创建 GlobalSessionDemoUpperCaseChild 对象。 GlobalSessionDemoUpperCaseChild 从操作的输入中获取小写字符串输入,将其转换为大写,并将其与从父机器人接收到的 SessionValue 连接起来。
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()); } }
- 构建并上传自定义软件包到 Control Room。 有关上传软件包的更多信息,请参阅 使用软件包 SDK。
- 创建 bot,使用全局会话大写父节点操作。
- 输入一个小写字符串(例如:parentbot string)
- 输入会话的名称。 父机器人将值返回到全局会话(例如: 全局)
- 创建 Task Bots,使用全局会话大写工单子节点操作。 您将能够输入一个小写字符串,并且还可以从共享的父会话中获取该字符串。 将父字符串和子字符串连接。
- 使用消息框显示输出。
- 现在调用全局会话大写字母子级进入父级。
- 运行父机器人,您将看到消息框显示子机器人通过共享全局会话得出的值。