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.SharedDataProvides 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.
Stringobjects, typically (but not necessarily) text characters. To save data types such as integers you should convert the values toStringobjects.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 aSharedDataobject 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 aSharedDataobject 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 voidaddFirst(java.lang.String key, java.lang.String value)Adds a value to the start of the specified value list in the Shared Data Server.voidaddLast(java.lang.String key, java.lang.String value)Adds a value to the end of the specified value list in the Shared Data Server.voidclear()Clears all keys and values from a running instance of the Shared Data Server.voidclear(java.lang.String key)Clears all values which are associated with the specifiedkeyfrom the Shared Data Server.voidclose()Deprecated.voidconnect()Deprecated.intdecrement(java.lang.String key, int inc)Decrements anintegervalue associated with the specifiedkeyin the Shared Data Server.java.lang.Stringfirst(java.lang.String key, int timeout)Reads the first value from the specified value list in the Shared Data Server.java.lang.Stringget(java.lang.String key, int timeout)Reads the value associated with the specifiedkeyin the Shared Data Server.intgetInt(java.lang.String key, int timeout)Reads anintegervalue associated with the specifiedkeyin the Shared Data Server.intgetMaxConnectionAttempts()Gets the maximum number of connection attempts to make when connecting to a Shared Data Server.static intgetMaxConnections()Gets the maximum number of connections that will be opened from each engine process to each Shared Data Server.intincrement(java.lang.String key, int inc)Increments anintegervalue associated with the specifiedkeyin the Shared Data Server.java.lang.Stringlast(java.lang.String key, int timeout)Reads the last value from the specified value list in the Shared Data Server.java.lang.StringremoveFirst(java.lang.String key, int timeout)Reads and removes the first value from the specified value list in the Shared Data Server.java.lang.StringremoveLast(java.lang.String key, int timeout)Reads and removes the last value from the specified value list in the Shared Data Server.voidset(java.lang.String key, java.lang.String value)Creates or resets the value associated with the specifiedkeyin the Shared Data Server.voidsetInt(java.lang.String key, int value)Creates or resets theintegervalue associated with the specifiedkeyin the Shared Data Server.voidsetMaxConnectionAttempts(int maxConnectionAttempts)Sets the maximum number of connection attempts to make when connecting to a Shared Data Server.static voidsetMaxConnections(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 aSharedDataobject 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 aSharedDataobject 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 BadValueExceptionSets 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-maxmust 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:
setMaxConnectionAttemptsin 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:
getMaxConnectionAttemptsin 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.NoSuchAlgorithmExceptionDeprecated.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.IOExceptionDeprecated.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.ExceptionAdds 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:
addFirstin 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.ExceptionReads the first value from the specified value list in the Shared Data Server.Set the
timeoutparameter 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:
firstin 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.ExceptionReads and removes the first value from the specified value list in the Shared Data Server.Set the
timeoutparameter 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:
removeFirstin 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.ExceptionAdds 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:
addLastin 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.ExceptionReads the last value from the specified value list in the Shared Data Server.Set the
timeoutparameter 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:
lastin 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.ExceptionReads and removes the last value from the specified value list in the Shared Data Server.Set the
timeoutparameter 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:
removeLastin 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.ExceptionReads the value associated with the specifiedkeyin the Shared Data Server.Set the
timeoutparameter 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:
getin 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
keyin 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.ExceptionReads anintegervalue associated with the specifiedkeyin the Shared Data Server.Set the
timeoutparameter 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:
getIntin 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
keyin 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.ExceptionCreates or resets the value associated with the specifiedkeyin 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:
setin interfacecom.testplant.messaging.SharedData- Parameters:
key- the key against whichvaluewill 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.ExceptionCreates or resets theintegervalue associated with the specifiedkeyin 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:
setIntin interfacecom.testplant.messaging.SharedData- Parameters:
key- the key against whichvaluewill 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.ExceptionIncrements anintegervalue associated with the specifiedkeyin the Shared Data Server.The specified
keymust already be associated with a value, which can be set by callingset(String, String)orsetInt(String, int). If the value associated withkeyis 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:
incrementin 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.ExceptionDecrements anintegervalue associated with the specifiedkeyin the Shared Data Server.The specified
keymust already be associated with a value, which can be set by callingset(String, String)orsetInt(String, int). If the value associated withkeyis 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:
decrementin 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.ExceptionClears all keys and values from a running instance of the Shared Data Server.- Specified by:
clearin 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.ExceptionClears all values which are associated with the specifiedkeyfrom the Shared Data Server.Removes any list or value associated with the specified
keyfrom 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:
clearin interfacecom.testplant.messaging.SharedData- Parameters:
key- the key against which the data is stored- Throws:
java.lang.Exception- Failed to cleart all the values
-
-