Configure shared session using Package SDK

Package SDK provides configuration for the SDK users to create and use shared sessions. Sessions are seamlessly shared between different bots to share resources between them. This means that if a session has been created, the other Task Bots can read and update the session and vice versa.

Share session between parent and child

The following section describes creating a shared session. The parent GlobalSessionDemoUpperCaseParent returns a Session value to the global session and is used by the child bot GlobalSessionDemoUpperCaseChild.
Note: Place the three classes below in the following location: <PackageSDK>\src\main\java\com\automationanywhere\botcommand\samples\commands\basic
  1. 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 {
    
        }
    }
    
  2. Add the @BotCommand annotation to make a class as an Action in the Control Room. For more information on annotations, see Annotations.
    @BotCommand
  3. 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)
  4. 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();
      	}
      }
  5. 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());
    	}
    }
  6. Build and upload the custom package to the Control Room. For more information on uploading the package, see Using the package SDK.
  7. Create a bot by using the Global session uppercase parent action.
    1. Enter a lower case string (For example: parentbot string)
    2. Enter a name for the session. The parent bot returns the value to the Global session (For example: Global)
  8. 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.
  9. Use a Message box to show the output.

    The image shows Variable SDK Demo

  10. Now call the Global session upper case child into the parent. The image shows Variable SDK Demo
  11. 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.