public final class ServerEnvironment
extends java.lang.Object
implements java.lang.AutoCloseable
Some parts of the ServerEnvironment
can be customized based on the EnvironmentType
. To do so, one of the overloads of the use
method can be called.
Two environment types exist out of the box: Tests
and Production
.
For example:
ServerEnvironment.when(Production.class) .use(productionStorageFactory) .use(memoizingTracerFactory); ServerEnvironment.when(Tests.class) .use(testingStorageFactory);A custom environment type may also be used:
final class StagingEnvironment extends EnvironmentType { ... } ServerEnvironment.when(Staging.class) .use(inMemoryStorageFactory);
If Staging
is enabled
, the specified value is going to be
returned on ServerEnvironment.storageFactory()
.
A ServerEnvironment
instance is a singleton. It means that all Bounded Contexts
which are deployed within the same JVM share the configuration of the server environment.
Namely, under the same environment type, all Bounded Contexts will share the same storage
factory, transport factory, delivery and tracing tools.
If one decides that some of the Bounded Contexts of an application requires custom
settings, they should arrange a separate deployable artifact and distinct configuration
of the respective ServerEnvironment
for those Bounded Contexts. In this way,
the Contexts would reside in their own JVMs and not overlap on interacting with this singleton.
Modifier and Type | Class and Description |
---|---|
static interface |
ServerEnvironment.Fn<R>
A function which accepts a class of
EnvironmentType and returns
a value configured in a ServerEnvironment . |
static class |
ServerEnvironment.TypeConfigurator
Allows to configure values used by the
ServerEnvironment for the given type. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Releases resources associated with this instance.
|
void |
configureDeployment(java.util.function.Supplier<io.spine.server.DeploymentType> supplier)
Makes the
ServerEnvironment.deploymentType() return the values from the provided supplier. |
Delivery |
delivery()
Returns the delivery mechanism specific to this environment.
|
io.spine.server.DeploymentType |
deploymentType()
The type of the environment application is deployed to.
|
static ServerEnvironment |
instance()
Returns a singleton instance.
|
CommandScheduler |
newCommandScheduler()
Obtains command scheduling mechanism used by
CommandBus in this environment. |
io.spine.server.NodeId |
nodeId()
Obtains the identifier of the server node, on which this code is running at the moment.
|
void |
reset()
This is test-only method required for cleaning of the server environment instance in tests.
|
void |
scheduleCommandsUsing(java.util.function.Supplier<CommandScheduler> commandScheduler)
Assigns command scheduling mechanism used at this environment by all
CommandBus instances. |
StorageFactory |
storageFactory()
Obtains the storage factory for the current environment.
|
java.util.Optional<TracerFactory> |
tracing()
Obtains the
TracerFactory associated with the current environment, if it was set. |
TransportFactory |
transportFactory()
Obtains the transport factory for the current environment.
|
java.lang.Class<? extends io.spine.base.EnvironmentType> |
type()
Obtains the type of the current server environment.
|
static ServerEnvironment.TypeConfigurator |
when(java.lang.Class<? extends io.spine.base.EnvironmentType> type)
Starts flowing API chain for configuring
ServerEnvironment for the passed type. |
public static ServerEnvironment instance()
public java.lang.Class<? extends io.spine.base.EnvironmentType> type()
Environment.type()
.public io.spine.server.DeploymentType deploymentType()
public Delivery delivery()
Unless updated manually, returns
a local implementation of Delivery
.
public CommandScheduler newCommandScheduler()
CommandBus
in this environment.public void scheduleCommandsUsing(java.util.function.Supplier<CommandScheduler> commandScheduler)
CommandBus
instances.
If not configured, ExecutorCommandScheduler
is used.
public io.spine.server.NodeId nodeId()
At the moment, the node identifier is always UUID-generated. In future versions of the framework it is expected to become configurable.
public void configureDeployment(java.util.function.Supplier<io.spine.server.DeploymentType> supplier)
ServerEnvironment.deploymentType()
return the values from the provided supplier.
When supplying your own deployment type in tests, remember to reset it during tear down.
public java.util.Optional<TracerFactory> tracing()
TracerFactory
associated with the current environment, if it was set.public StorageFactory storageFactory()
For tests, if the value was not set, defaults to a new InMemoryStorageFactory
.
For other environments, if the value was not set, throws a IllegalStateException
.
StorageFactory
instance for the storage for the current environmentjava.lang.IllegalStateException
- if the value StorageFactory
was not
configured prior to the callpublic TransportFactory transportFactory()
In the testing environment, if the factory was not assigned, assigns
a new InMemoryTransportFactory
and returns it.
For all other environment types, throws an IllegalStateException
.
If the factory is not assigned in the Tests mode, assigns the instance of
InMemoryTransportFactory
and returns it.
public void reset()
public void close() throws java.lang.Exception
close
in interface java.lang.AutoCloseable
java.lang.Exception
public static ServerEnvironment.TypeConfigurator when(java.lang.Class<? extends io.spine.base.EnvironmentType> type)
ServerEnvironment
for the passed type.