Package org.rapidcontext.core.type
Class Connection
java.lang.Object
org.rapidcontext.core.storage.StorableObject
org.rapidcontext.core.type.Connection
A connection to an external system. This is an abstract base
class, providing a number of services:
- On-Demand Creation -- Both connections and their managed communication channels are created and initialized on demand.
- Usage Limits -- Configurable limits for the maximum number of communication channels are automatically upheld.
- Connection Pooling -- Communication channel pooling is built-in and enabled by implementing a few simple methods.
- Connection Sharing -- Communication channel sharing (multiple tasks sharing the same channel) is also built-in and easily enabled.
- Validation & Keep-Alive -- All pooled channels are validated after usage and kept-alive with regular validation requests when pooled for reuse.
- Version:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The dictionary key for the maximum idle time (in seconds).static final String
The dictionary key for the maximum number of open channels.protected String
The error message for the last error.protected long
The timestamp (in milliseconds) of the last usage time.static final Path
The connection object storage path.Fields inherited from class org.rapidcontext.core.storage.StorableObject
dict, KEY_ACTIVATED_TIME, KEY_CLASSNAME, KEY_ID, KEY_TYPE, PREFIX_COMPUTED, PREFIX_HIDDEN
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Connection
(String id, String type, Dict dict) Creates a new connection from a serialized representation. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
cleanupChannel
(Channel channel) Destroys and removes references to a channel.protected abstract Channel
Creates a new connection channel.protected void
destroy()
Destroys this connection.protected abstract void
destroyChannel
(Channel channel) Destroys a connection channel, freeing any resources used (such as database connections, networking sockets, etc).static Connection
Searches for a specific connection in the storage.static Metrics
getMetrics
(Storage storage) Returns the connection usage metrics.protected void
init()
Initializes this connection after loading it from a storage.protected boolean
isActive()
Checks if this object is in active use.Returns the error message for the last error.lastUsed()
Returns the timestamp of the last connection usage.int
Returns the maximum number of seconds a channel is allowed to be idle (in a pool).int
maxOpen()
Returns the maximum number of open channels.int
Returns the total number of open channels.protected void
Attempts to deactivate this object.void
Releases a previously reserved communication channel for this connection.protected void
Reports connection usage metrics for a single query/request/etc.reserve()
Reserves a communication channel for this connection.Returns a serialized representation of this object.int
Returns the number of channels in use (reserved).Methods inherited from class org.rapidcontext.core.storage.StorableObject
activate, activatedTime, id, isModified, path, sterilize, toString, type
-
Field Details
-
KEY_MAX_OPEN
The dictionary key for the maximum number of open channels.- See Also:
-
KEY_MAX_IDLE_SECS
The dictionary key for the maximum idle time (in seconds).- See Also:
-
PATH
The connection object storage path. -
lastUsedTime
protected long lastUsedTimeThe timestamp (in milliseconds) of the last usage time. This will be updated on each connection reservation or release. It is used in the default mechanism for determining if the connection is active. -
lastError
The error message for the last error. This will set or cleared on each connection reservation.
-
-
Constructor Details
-
Connection
Creates a new connection from a serialized representation.- Parameters:
id
- the object identifiertype
- the object type namedict
- the serialized representation- See Also:
-
-
Method Details
-
find
Searches for a specific connection in the storage.- Parameters:
storage
- the storage to search inid
- the connection identifier- Returns:
- the connection found, or null if not found
-
getMetrics
Returns the connection usage metrics. The metrics will be loaded from storage if not already in memory.- Parameters:
storage
- the storage to load from- Returns:
- the connection usage metrics
-
isActive
protected boolean isActive()Checks if this object is in active use. This method will only return false if the object haven't been used for 60 seconds and no channels remain open.- Overrides:
isActive
in classStorableObject
- Returns:
- true if the object is considered active, or false otherwise
-
init
Initializes this connection after loading it from a storage. Any object initialization that may fail or that causes the object to interact with any other part of the system (or external systems) should be implemented here.- Overrides:
init
in classStorableObject
- Throws:
StorageException
- if the initialization failed
-
destroy
protected void destroy()Destroys this connection. This method is used to free any resources used when this object is no longer used. This method is called when an object is removed from the in-memory storage (object cache).- Overrides:
destroy
in classStorableObject
-
passivate
protected void passivate()Attempts to deactivate this object. This method will evict old connection channels from the pool and should only be called from a background job.- Overrides:
passivate
in classStorableObject
-
maxOpen
public int maxOpen()Returns the maximum number of open channels. If the config parameter hasn't been set, a default value of four (4) will be returned.- Returns:
- the maximum number of open channels
-
maxIdleSeconds
public int maxIdleSeconds()Returns the maximum number of seconds a channel is allowed to be idle (in a pool). If the configuration parameter hasn't been set, a default value of 600 seconds (10 minutes) will be returned.- Returns:
- the maximum number of seconds to idle a channel
-
openChannels
public int openChannels()Returns the total number of open channels. This is the number of reserved channels plus any idle channels in the pool (if any).- Returns:
- the total number of open channels, or zero (0) if no channels are currently open
-
usedChannels
public int usedChannels()Returns the number of channels in use (reserved).- Returns:
- the number of channels in use
-
lastUsed
Returns the timestamp of the last connection usage. This will be updated on each connection reservation or release. It is used in the default mechanism for determining if the connection is active.- Returns:
- the timestamp of the last connection usage, or null if the connection hasn't been used
-
lastError
Returns the error message for the last error. This will set or cleared on each connection reservation.- Returns:
- the error message for the last error
-
reserve
Reserves a communication channel for this connection. If the channels supports being pooled, a previously created channel may be returned from this method.- Returns:
- the reserved connection channel
- Throws:
ConnectionException
- if no communication channel could be created and validated
-
release
Releases a previously reserved communication channel for this connection. If the channel supports being pooled, it will be added to the pool of channels, otherwise it will be destroyed immediately.- Parameters:
channel
- the channel to release
-
cleanupChannel
Destroys and removes references to a channel. No errors are logged, so this method should only be used after other errors have already been reported.- Parameters:
channel
- the channel to destroy
-
createChannel
Creates a new connection channel.- Returns:
- the channel created
- Throws:
ConnectionException
- if the channel couldn't be created properly
-
destroyChannel
Destroys a connection channel, freeing any resources used (such as database connections, networking sockets, etc).- Parameters:
channel
- the channel to destroy
-
report
Reports connection usage metrics for a single query/request/etc. If the start time isn't positive, no actual usage and duration will be reported, only potential errors.- Parameters:
start
- the start time (in millis), or zero (0) for nonesuccess
- the success flagerror
- the optional error message
-
serialize
Returns a serialized representation of this object. Used when persisting to permanent storage or when accessing the object from outside pure Java. Returns a shallow copy of the contained dictionary.- Overrides:
serialize
in classStorableObject
- Returns:
- the serialized representation of this object
-