Class SharedData
- java.lang.Object
-
- com.facilita.fc.runtime.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.
String
objects, typically (but not necessarily) text characters. To save data types such as integers you should convert the values toString
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 aSharedData
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 aSharedData
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 specifiedkey
from the Shared Data Server.void
close()
Deprecated.void
connect()
Deprecated.int
decrement(java.lang.String key, int inc)
Decrements aninteger
value associated with the specifiedkey
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 specifiedkey
in the Shared Data Server.int
getInt(java.lang.String key, int timeout)
Reads aninteger
value associated with the specifiedkey
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 aninteger
value associated with the specifiedkey
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 specifiedkey
in the Shared Data Server.void
setInt(java.lang.String key, int value)
Creates or resets theinteger
value associated with the specifiedkey
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.
-
-
-
Constructor Detail
-
SharedData
public SharedData(java.lang.String host, int port)
Creates aSharedData
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 Serverport
- the TCP port that the Shared Data Server is listening on
-
SharedData
public SharedData(java.lang.String host, int port, boolean secureConnection)
Creates aSharedData
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 Serverport
- the TCP port that the Shared Data Server is listening onsecureConnection
- 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 interfacecom.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 interfacecom.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 happenjava.lang.IllegalArgumentException
- Deprecated - will never happenjava.security.NoSuchAlgorithmException
- Deprecated - will never happenjava.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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the data is storedvalue
- the value to be added at the start of the list of values forkey
- 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 forkey
, 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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the data is storedtimeout
- 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 forkey
, 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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the data is storedtimeout
- 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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the data is storedvalue
- the value to be added at the end of the list of values forkey
- 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 forkey
, 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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the data is storedtimeout
- 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 forkey
, 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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the data is storedtimeout
- 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 specifiedkey
in the Shared Data Server.Set the
timeout
parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no value exists forkey
, 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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the value is storedtimeout
- 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 aninteger
value associated with the specifiedkey
in the Shared Data Server.Set the
timeout
parameter to 0 (zero) to prevent blocking. Use -1 to wait forever. If no value exists forkey
, 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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the value is storedtimeout
- 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 specifiedkey
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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against whichvalue
will be storedvalue
- 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 theinteger
value associated with the specifiedkey
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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against whichvalue
will be storedvalue
- 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 aninteger
value associated with the specifiedkey
in the Shared Data Server.The specified
key
must already be associated with a value, which can be set by callingset(String, String)
orsetInt(String, int)
. If the value associated withkey
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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the value to be incremented is storedinc
- 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 aninteger
value associated with the specifiedkey
in the Shared Data Server.The specified
key
must already be associated with a value, which can be set by callingset(String, String)
orsetInt(String, int)
. If the value associated withkey
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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the value to be decremented is storedinc
- 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 interfacecom.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 specifiedkey
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)
oraddLast(String, String)
) and a value (created usingset(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 interfacecom.testplant.messaging.SharedData
- Parameters:
key
- the key against which the data is stored- Throws:
java.lang.Exception
- Failed to cleart all the values
-
-