public final class ServiceRuntime extends Object implements AutoCloseable
Server; allows the native code to load and unload artifacts (JAR archives with Exonum
services), create and stop services defined in the loaded artifacts.
This class is thread-safe and does not support client-side locking. The thread-safety is provided because the class is a singleton and may be provided to other objects. Currently, however, there is a single injection point where ServiceRuntime is instantiated (during bootstrap) and it is used by the native runtime only in a single-threaded context, hence thread-safety isn't strictly required, but rather provided to avoid possible errors if it is ever accessed by other objects.
| Constructor and Description |
|---|
ServiceRuntime(com.exonum.binding.core.runtime.ServiceLoader serviceLoader,
com.exonum.binding.core.runtime.ServicesFactory servicesFactory,
RuntimeTransport runtimeTransport,
Path artifactsDir)
Creates a new Java service runtime.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterCommit(BlockCommittedEvent event)
Notifies the services in the runtime of the block commit event.
|
void |
beforeCommit(int serviceId,
Fork fork)
Performs the before commit operation on the specified service in this runtime.
|
void |
close() |
void |
commitService(ServiceInstanceSpec instanceSpec)
Adds a service instance to the runtime after it has been successfully initialized
in
startAddingService(Fork, ServiceInstanceSpec, byte[]). |
void |
deployArtifact(ServiceArtifactId id,
String filename)
Loads a Java service artifact from the specified file.
|
void |
executeTransaction(int serviceId,
String interfaceName,
int txId,
byte[] arguments,
Fork fork,
int callerServiceId,
com.exonum.binding.common.hash.HashCode txMessageHash,
com.exonum.binding.common.crypto.PublicKey authorPublicKey)
Executes a transaction belonging to the given service.
|
ServiceRuntimeProtos.ServiceRuntimeStateHashes |
getStateHashes(Snapshot snapshot)
Returns the state hashes of this runtime and the services registered in it as a protobuf
message.
|
void |
initialize(Node node)
Initializes the runtime with the given node.
|
boolean |
isArtifactDeployed(ServiceArtifactId id)
Returns true if an artifact with the given id is currently deployed in this runtime.
|
void |
shutdown()
Stops this runtime.
|
void |
startAddingService(Fork fork,
ServiceInstanceSpec instanceSpec,
byte[] configuration)
Starts registration of a new service instance with the given specification.
|
void |
verifyTransaction(int serviceId,
int txId,
byte[] arguments)
Verifies that an Exonum raw transaction can be correctly converted to an executable
transaction of given service.
|
@Inject
public ServiceRuntime(com.exonum.binding.core.runtime.ServiceLoader serviceLoader,
com.exonum.binding.core.runtime.ServicesFactory servicesFactory,
RuntimeTransport runtimeTransport,
Path artifactsDir)
serviceLoader - a loader of service artifactsservicesFactory - the factory of servicesruntimeTransport - a web server providing transport to Java servicesartifactsDir - the directory in which administrators place and from which
the service runtime loads service artifacts; may not exist at instantiation timepublic void initialize(Node node)
public void deployArtifact(ServiceArtifactId id, String filename) throws ServiceLoadingException
id - a service artifact identifier; artifacts with non-equal ids will be rejectedfilename - a filename of the service artifact in the directory for artifactsServiceLoadingException - if it failed to load an artifact; or if the given artifact is
already loadedpublic boolean isArtifactDeployed(ServiceArtifactId id)
id - a service artifact identifierpublic void startAddingService(Fork fork, ServiceInstanceSpec instanceSpec, byte[] configuration)
commitService(ServiceInstanceSpec)
is invoked.fork - a database view to apply configurationinstanceSpec - a service instance specification; must reference a deployed artifactconfiguration - service instance configuration parameters as a serialized protobuf
messageIllegalArgumentException - if the service is already started; or its artifact
is not deployedRuntimeException - if it failed to instantiate the service;
or if the service initialization failedpublic void commitService(ServiceInstanceSpec instanceSpec)
startAddingService(Fork, ServiceInstanceSpec, byte[]). This operation
completes the service instance registration, allowing subsequent operations on it:
transactions, API requests.instanceSpec - a service instance specification; must reference a deployed artifactIllegalArgumentException - if the service is already started; or its artifact
is not deployedpublic void executeTransaction(int serviceId,
String interfaceName,
int txId,
byte[] arguments,
Fork fork,
int callerServiceId,
com.exonum.binding.common.hash.HashCode txMessageHash,
com.exonum.binding.common.crypto.PublicKey authorPublicKey)
throws TransactionExecutionException
serviceId - the numeric identifier of the service instance to which the transaction
belongsinterfaceName - a fully-qualified name of the interface in which the transaction
is defined, or empty string if it is defined in the service directly (implicit interface)txId - the transaction type identifierarguments - the serialized transaction argumentsfork - a native fork objectcallerServiceId - the id of the caller service if transaction is invoked by other
service. Currently only applicable to invocations of Configure interface methodstxMessageHash - the hash of the transaction messageauthorPublicKey - the public key of the transaction authorTransactionExecutionExceptionpublic ServiceRuntimeProtos.ServiceRuntimeStateHashes getStateHashes(Snapshot snapshot)
snapshot - the snapshot of the current database statepublic void beforeCommit(int serviceId,
Fork fork)
serviceId - the id of the service on which to perform the operationfork - a fork allowing the runtime and the service to modify the database state.public void afterCommit(BlockCommittedEvent event)
public void verifyTransaction(int serviceId,
int txId,
byte[] arguments)
serviceId - the id of the servicetxId - the transaction type identifier
within the servicearguments - the serialized transaction
argumentsIllegalArgumentException - if there is no service with such id in this runtime, or if
the transaction is not known to the service, or the arguments are not valid: e.g., cannot
be deserialized, or do not meet the preconditionspublic void shutdown()
throws InterruptedException
InterruptedException - if an interrupt was requestedpublic void close()
throws InterruptedException
close in interface AutoCloseableInterruptedExceptionCopyright © 2019 Exonum. All rights reserved.