Class SharedData

  • All Implemented Interfaces:
    com.testplant.messaging.SharedData

    public class SharedData
    extends java.lang.Object
    implements com.testplant.messaging.SharedData
    Provides functionality for communication with the Shared Data Server. It provides the following features:
    • Data can saved in a common data store and read by other Virtual Users.
    • Data can be saved and retrieved as FIFO (first in, first out) and LIFO (last in, first out) lists against keys.
    • Data can be saved as single key/value pairs.
    • Virtual Users will optionally wait until the requested data becomes available.
    • Access is locked between Virtual Users for atomic operations.
    Data is stored in the Shared Data Server as a Dictionary in the form of a key and an associated value or list of values. It can be used in a similar way to a DataDictionary or DataTable. The keys and values are stored as String objects, typically (but not necessarily) text characters. To save data types such as integers you should convert the values to String objects.

    e.g.

     
     // Declare a double and convert to String
     double uid = 1234.567;
     String value = String.valueOf(uid);
    
     // Add it to the front of a shared list
     sharedData.addFirst("UID", value);
    
     // Retrieve and then convert back to a double
     value = sharedData.first("UID", -1);
     uid = Double.parseDouble(value);
     
    • Constructor Summary

      Constructors 
      Constructor Description
      SharedData​(java.lang.String host, int port)
      Creates a SharedData object which can be used to connect to a running instance of the Shared Data Server.
      SharedData​(java.lang.String host, int port, boolean secureConnection)
      Creates a SharedData object which can be used to connect to a running instance of the Shared Data Server.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void addFirst​(java.lang.String key, java.lang.String value)
      Adds a value to the start of the specified value list in the Shared Data Server.
      void addLast​(java.lang.String key, java.lang.String value)
      Adds a value to the end of the specified value list in the Shared Data Server.
      void clear()
      Clears all keys and values from a running instance of the Shared Data Server.
      void clear​(java.lang.String key)
      Clears all values which are associated with the specified key from the Shared Data Server.
      void close()
      Deprecated.
      void connect()
      Deprecated.
      int decrement​(java.lang.String key, int inc)
      Decrements an integer value associated with the specified key in the Shared Data Server.
      java.lang.String first​(java.lang.String key, int timeout)
      Reads the first value from the specified value list in the Shared Data Server.
      java.lang.String get​(java.lang.String key, int timeout)
      Reads the value associated with the specified key in the Shared Data Server.
      int getInt​(java.lang.String key, int timeout)
      Reads an integer value associated with the specified key in the Shared Data Server.
      int getMaxConnectionAttempts()
      Gets the maximum number of connection attempts to make when connecting to a Shared Data Server.
      static int getMaxConnections()
      Gets the maximum number of connections that will be opened from each engine process to each Shared Data Server.
      int increment​(java.lang.String key, int inc)
      Increments an integer value associated with the specified key in the Shared Data Server.
      java.lang.String last​(java.lang.String key, int timeout)
      Reads the last value from the specified value list in the Shared Data Server.
      java.lang.String removeFirst​(java.lang.String key, int timeout)
      Reads and removes the first value from the specified value list in the Shared Data Server.
      java.lang.String removeLast​(java.lang.String key, int timeout)
      Reads and removes the last value from the specified value list in the Shared Data Server.
      void set​(java.lang.String key, java.lang.String value)
      Creates or resets the value associated with the specified key in the Shared Data Server.
      void setInt​(java.lang.String key, int value)
      Creates or resets the integer value associated with the specified key in the Shared Data Server.
      void setMaxConnectionAttempts​(int maxConnectionAttempts)
      Sets the maximum number of connection attempts to make when connecting to a Shared Data Server.
      static void setMaxConnections​(int max)
      Sets the maximum number of connections that will be opened from each engine process to each Shared Data Server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SharedData

        public SharedData​(java.lang.String host,
                          int port)
        Creates a SharedData object which can be used to connect to a running instance of the Shared Data Server.

        Parameters:
        host - the address or host name of the Shared Data Server
        port - the TCP port that the Shared Data Server is listening on
      • SharedData

        public SharedData​(java.lang.String host,
                          int port,
                          boolean secureConnection)
        Creates a SharedData object which can be used to connect to a running instance of the Shared Data Server.

        Parameters:
        host - the address or host name of the Shared Data Server
        port - the TCP port that the Shared Data Server is listening on
        secureConnection - Flag indicating whether SSL encryption is to be used to secure connection to Shared Data Server
    • Method Detail

      • getMaxConnections

        public static int getMaxConnections()
        Gets the maximum number of connections that will be opened from each engine process to each Shared Data Server.

        The default value is 10. You cannot set a value less than 1.

        Returns:
        the maximum number of connections that will be opened from each engine process to each Shared Data Server.
        See Also:
        setMaxConnections(int)
      • setMaxConnections

        public static void setMaxConnections​(int max)
                                      throws BadValueException
        Sets the maximum number of connections that will be opened from each engine process to each Shared Data Server.

        The default value is 10. You cannot set a value less than 1.

        Parameters:
        max - the maximum number of connections that will be opened from each engine process to each Shared Data Server.
        Throws:
        BadValueException - max must not be less than 1
        See Also:
        getMaxConnections()
      • setMaxConnectionAttempts

        public void setMaxConnectionAttempts​(int maxConnectionAttempts)
        Sets the maximum number of connection attempts to make when connecting to a Shared Data Server.

        The default is 4.

        Specified by:
        setMaxConnectionAttempts in interface com.testplant.messaging.SharedData
        Parameters:
        maxConnectionAttempts - the maximum number of connection attempts to make when connecting to a Shared Data Server
        See Also:
        getMaxConnectionAttempts()
      • getMaxConnectionAttempts

        public int getMaxConnectionAttempts()
        Gets the maximum number of connection attempts to make when connecting to a Shared Data Server.

        The default is 4.

        Specified by:
        getMaxConnectionAttempts in interface com.testplant.messaging.SharedData
        Returns:
        the maximum number of connection attempts to make when connecting to a Shared Data Server
        See Also:
        setMaxConnectionAttempts(int)
      • connect

        @Deprecated
        public void connect()
                     throws java.io.IOException,
                            java.lang.IllegalArgumentException,
                            java.security.KeyManagementException,
                            java.security.NoSuchAlgorithmException
        Deprecated.
        Connects to a running instance of the Shared Data Server.

        Does nothing. This method is included for backwards compatibility only and will be removed in a later release.

        Throws:
        java.io.IOException - Deprecated - will never happen
        java.lang.IllegalArgumentException - Deprecated - will never happen
        java.security.NoSuchAlgorithmException - Deprecated - will never happen
        java.security.KeyManagementException - Deprecated - will never happen
      • close

        @Deprecated
        public void close()
                   throws java.io.IOException
        Deprecated.
        Closes a connection to a running instance of the Shared Data Server.

        Does nothing. This method is included for backwards compatibility only and will be removed in a later release.

        Throws:
        java.io.IOException - Deprecated - will never happen
      • addFirst

        public void addFirst​(java.lang.String key,
                             java.lang.String value)
                      throws java.lang.Exception
        Adds a value to the start of the specified value list in the Shared Data Server.

        If no current list of values exists for key, then a new list is created.

        The keys are case-sensitive.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         
         int id = 99;
         sharedData.addFirst("CustID", String.valueOf(id)); // add value (as a String) to shared data server
         
        Specified by:
        addFirst in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the data is stored
        value - the value to be added at the start of the list of values for key
        Throws:
        java.lang.Exception - Failed to add the key to the list
        See Also:
        addLast(String, String), first(String, int), last(String, int), removeFirst(String, int), removeLast(String, int)
      • first

        public java.lang.String first​(java.lang.String key,
                                      int timeout)
                               throws java.lang.Exception
        Reads the first value from the specified value list in the Shared Data Server.

        Set the timeout parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no list of values exists for key, then the Virtual User thread will be suspended and the call will only return (dependent upon timeout) when a value is read from the Shared Data server. If the connection to the Shared Data Server is lost an exception is raised.

        The keys are case-sensitive.

        The value is not removed from the list.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         String userID = sharedData.first("UID", 20000);   // wait 20 seconds; an exception is raised on timeout
         
         // Alternate method that catches the exception if no value for key
         try
         {
             userID = sharedData.first("UID", 0);   // do not wait for a value
         }
         catch (Exception e)
         {
             error("NoValue: " + e.toString()); // error is "key=NoValue"
             nextIteration();
         }
         
         
        Specified by:
        first in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the data is stored
        timeout - the maximum time in milliseconds to wait for a value to become available
        Returns:
        the first value in the list
        Throws:
        java.lang.Exception - Failed to get the key from the list
      • removeFirst

        public java.lang.String removeFirst​(java.lang.String key,
                                            int timeout)
                                     throws java.lang.Exception
        Reads and removes the first value from the specified value list in the Shared Data Server.

        Set the timeout parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no list of values exists for key, then the Virtual User thread will be suspended and the call will only return (dependent upon timeout) when a value is read from the Shared Data server. If the connection to the Shared Data Server is lost an exception is raised.

        The keys are case-sensitive.

        The value is removed from the list.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         String accountNum = sharedData.removeFirst("AccNum", 90000); // wait up to 90 seconds for a value
         
        Specified by:
        removeFirst in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the data is stored
        timeout - the maximum time in milliseconds to wait for a value to become available
        Returns:
        the first value in the list
        Throws:
        java.lang.Exception - Failed to remove the item from the list
      • addLast

        public void addLast​(java.lang.String key,
                            java.lang.String value)
                     throws java.lang.Exception
        Adds a value to the end of the specified value list in the Shared Data Server.

        If no current list of values exists for key, then a new list is created.

        The keys are case-sensitive.

        e.g.

        
         import java.util.*;
        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         
         String lastUpdate = new Date().toString(); // String containing current date
         sharedData.addLast("lastUpdated", lastUpdate); // add value to shared data server
         
        Specified by:
        addLast in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the data is stored
        value - the value to be added at the end of the list of values for key
        Throws:
        java.lang.Exception - Failed to add the item to the list
      • last

        public java.lang.String last​(java.lang.String key,
                                     int timeout)
                              throws java.lang.Exception
        Reads the last value from the specified value list in the Shared Data Server.

        Set the timeout parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no list of values exists for key, then the Virtual User thread will be suspended and the call will only return (dependent upon timeout) when a value is read from the Shared Data server. If the connection to the Shared Data Server is lost an exception is raised.

        The keys are case-sensitive.

        The value is not removed from the list.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         String userID = sharedData.last("UID", 20000);   // wait 20 seconds; an exception is raised on timeout
         
         // Alternate method that catches the exception if no value for key
         try
         {
             userID = sharedData.last("UID", 0);   // do not wait for a value
         }
         catch (Exception e)
         {
             error("NoValue: " + e.toString()); // error is "key=NoValue"
             nextIteration();
         }
         
         
        Specified by:
        last in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the data is stored
        timeout - the maximum time in milliseconds to wait for a value to become available
        Returns:
        the last value in the list
        Throws:
        java.lang.Exception - Failed to get the item from the list
      • removeLast

        public java.lang.String removeLast​(java.lang.String key,
                                           int timeout)
                                    throws java.lang.Exception
        Reads and removes the last value from the specified value list in the Shared Data Server.

        Set the timeout parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no list of values exists for key, then the Virtual User thread will be suspended and the call will only return (dependent upon timeout) when a value is read from the Shared Data server.

        The keys are case-sensitive.

        The value is removed from the list.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         String accountNum = sharedData.removeLast("AccNum", 90000); // wait up to 90 seconds for a value
         
        Specified by:
        removeLast in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the data is stored
        timeout - the maximum time in milliseconds to wait for a value to become available
        Returns:
        the last value in the list
        Throws:
        java.lang.Exception - Failed to remove the item from the list
      • get

        public java.lang.String get​(java.lang.String key,
                                    int timeout)
                             throws java.lang.Exception
        Reads the value associated with the specified key in the Shared Data Server.

        Set the timeout parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no value exists for key, then the Virtual User thread will be suspended and the call will only return (dependent upon timeout) when a value is read from the Shared Data server. If the connection to the Shared Data Server is lost an exception is raised.

        Values can be set by calling set(String, String).

        The keys are case-sensitive.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         String value = sharedData.get("CurrServer", 0);
         
        Specified by:
        get in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the value is stored
        timeout - the maximum time in milliseconds to wait for a value to become available
        Returns:
        the value associated with the specified key in the Shared Data Server
        Throws:
        java.lang.Exception - Failed to get the item
      • getInt

        public int getInt​(java.lang.String key,
                          int timeout)
                   throws java.lang.Exception
        Reads an integer value associated with the specified key in the Shared Data Server.

        Set the timeout parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no value exists for key, then the Virtual User thread will be suspended and the call will only return (dependent upon timeout) when a value is read from the Shared Data server.

        Values can be set by calling setInt(String, int).

        The keys are case-sensitive.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         int value = sharedData.getInt("AccountNum", 0);
         
        Specified by:
        getInt in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the value is stored
        timeout - the maximum time in milliseconds to wait for a value to become available
        Returns:
        the value associated with the specified key in the Shared Data Server
        Throws:
        java.lang.Exception - Failed to get the item
      • set

        public void set​(java.lang.String key,
                        java.lang.String value)
                 throws java.lang.Exception
        Creates or resets the value associated with the specified key in the Shared Data Server.

        If no current value exists for key, then a new key/value pair is created.

        The keys are case-sensitive.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         sharedData.set("CurrServer", "192.168.0.1");
         
        Specified by:
        set in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which value will be stored
        value - the value to be stored
        Throws:
        java.lang.Exception - Failed to set the value
      • setInt

        public void setInt​(java.lang.String key,
                           int value)
                    throws java.lang.Exception
        Creates or resets the integer value associated with the specified key in the Shared Data Server.

        If no current value exists for key, then a new key/value pair is created.

        The keys are case-sensitive.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         sharedData.setInt("AccountNum", 12345678);
         
        Specified by:
        setInt in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which value will be stored
        value - the value to be stored
        Throws:
        java.lang.Exception - Failed to set the value
      • increment

        public int increment​(java.lang.String key,
                             int inc)
                      throws java.lang.Exception
        Increments an integer value associated with the specified key in the Shared Data Server.

        The specified key must already be associated with a value, which can be set by calling set(String, String) or setInt(String, int). If the value associated with key is not an incrementable value then no action takes place.

        A negative increment value can be supplied.

        The keys are case-sensitive.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         sharedData.set("Count", "100");
         sharedData.increment("Count", 7);   // value will = 107
         
        Specified by:
        increment in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the value to be incremented is stored
        inc - the amount by which to increment
        Returns:
        the new, incremented value
        Throws:
        java.lang.Exception - Failed to increment the value
      • decrement

        public int decrement​(java.lang.String key,
                             int inc)
                      throws java.lang.Exception
        Decrements an integer value associated with the specified key in the Shared Data Server.

        The specified key must already be associated with a value, which can be set by calling set(String, String) or setInt(String, int). If the value associated with key is not a decrementable value then no action takes place.

        A negative decrement value can be supplied.

        The keys are case-sensitive.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         sharedData.set("Count", "100");
         sharedData.decrement("Count", 7);   // value will = 93
         
        Specified by:
        decrement in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the value to be decremented is stored
        inc - the amount by which to decrement
        Returns:
        the new, decremented value
        Throws:
        java.lang.Exception - Failed to decrement the value
      • clear

        public void clear()
                   throws java.lang.Exception
        Clears all keys and values from a running instance of the Shared Data Server.
        Specified by:
        clear in interface com.testplant.messaging.SharedData
        Throws:
        java.lang.Exception - Failed to clear all the values
      • clear

        public void clear​(java.lang.String key)
                   throws java.lang.Exception
        Clears all values which are associated with the specified key from the Shared Data Server.

        Removes any list or value associated with the specified key from the Shared Data Server. In this case both the list and value items will be removed. No exception is raised if the key does not exist.

        Note that it is possible to have both a value list (created using addFirst(String, String) or addLast(String, String)) and a value (created using set(String, String)) both using the same key. In this case both the list and value items will be removed.

        e.g.

        
         SharedData sharedData = new SharedData(getString("sharedDataHost", "localhost"), getInt("sharedDataPort", 5099));
         sharedData.clear("MyKey");   // remove the list or value for the key
         
        Specified by:
        clear in interface com.testplant.messaging.SharedData
        Parameters:
        key - the key against which the data is stored
        Throws:
        java.lang.Exception - Failed to cleart all the values