pub struct ComponentContext<CD>where
CD: ComponentTraits,{ /* private fields */ }Expand description
The contextual object for a Kompact component
Gives access compact internal features like timers, logging, confguration, an the self reference.
Implementations§
Source§impl<CD> ComponentContext<CD>where
CD: ComponentTraits + ComponentLifecycle,
impl<CD> ComponentContext<CD>where
CD: ComponentTraits + ComponentLifecycle,
Sourcepub fn uninitialised() -> ComponentContext<CD>
pub fn uninitialised() -> ComponentContext<CD>
Create a new, uninitialised component context
§Note
Nothing in this context may be used before the parent component is actually initialised!
Sourcepub fn initialise(&mut self, c: Arc<Component<CD>>)
pub fn initialise(&mut self, c: Arc<Component<CD>>)
Initialise the component context with the actual component instance
This must be invoked from setup.
Sourcepub fn log(&self) -> &Logger<Arc<Fuse<Async>>>
pub fn log(&self) -> &Logger<Arc<Fuse<Async>>>
The components logger instance
This instance will already be preloaded with component specific information in the MDC.
§Example
use kompact::prelude::*;
#[derive(ComponentDefinition, Actor)]
struct HelloLogging {
ctx: ComponentContext<Self>,
}
impl HelloLogging {
fn new() -> HelloLogging {
HelloLogging {
ctx: ComponentContext::uninitialised(),
}
}
}
impl ComponentLifecycle for HelloLogging {
fn on_start(&mut self) -> HandlerResult {
info!(self.ctx().log(), "Hello Start event");
self.ctx().system().shutdown_async();
Handled::OK
}
}
let system = KompactConfig::default().build().wait().expect("system");
let c = system.create(HelloLogging::new);
system.start(&c);
system.terminated().wait();Sourcepub fn config(&self) -> &Config
pub fn config(&self) -> &Config
Get a reference to the system configuration
Use load_config_str or or load_config_file to load values into the config object.
§Example
use kompact::prelude::*;
#[derive(ComponentDefinition, Actor)]
struct ConfigComponent {
ctx: ComponentContext<Self>,
}
impl ConfigComponent {
fn new() -> ConfigComponent {
ConfigComponent {
ctx: ComponentContext::uninitialised(),
}
}
}
impl ComponentLifecycle for ConfigComponent {
fn on_start(&mut self) -> HandlerResult {
assert_eq!(Some(7i64), self.ctx().config()["a"].as_i64());
self.ctx().system().shutdown_async();
Handled::OK
}
}
let default_values = r#"a = 7"#;
let mut conf = KompactConfig::default();
conf.load_config_str(default_values);
let system = conf.build().wait().expect("system");
let c = system.create(ConfigComponent::new);
system.start(&c);
system.terminated().wait();Sourcepub fn set_blocking(&mut self, future: BlockingFuture)
pub fn set_blocking(&mut self, future: BlockingFuture)
Sets the component to block on the provided blocking future
This should only be used when implementing custom execute logic! Otherwise the correct way to block is to return the Handled::BlockOn variant from a handler.
If this is used for custom execute logic, then the next call
should be a return ExecuteResult::new(true, count, skip), as continuing to execute handlers violates
blocking semantics.
Sourcepub fn is_blocking(&self) -> bool
pub fn is_blocking(&self) -> bool
Return true if the component is set up for blocking
If this does return true, port handling must be immediately
aborted and the returned result must have the form
ExecuteResult::new(true, count, skip).
Sourcepub fn typed_component(&self) -> Arc<Component<CD>>
pub fn typed_component(&self) -> Arc<Component<CD>>
Returns the component instance wrapping this component definition
This is mostly meant to be passed along for scheduling or registrations. Don’t try to lock anything on the thread already executing the component!
Sourcepub fn component(&self) -> Arc<dyn CoreContainer>
pub fn component(&self) -> Arc<dyn CoreContainer>
Returns the component instance wrapping this component definition
This is mostly meant to be passed along for scheduling and the like. Don’t try to lock anything on the thread already executing the component!
Sourcepub fn system(&self) -> ContextSystemHandle
pub fn system(&self) -> ContextSystemHandle
Returns a handle to the Kompact system this component is a part of
Sourcepub fn dispatcher_ref(&self) -> ActorRefStrong<DispatchEnvelope>
pub fn dispatcher_ref(&self) -> ActorRefStrong<DispatchEnvelope>
Returns a reference to the system dispatcher
Sourcepub fn deadletter_ref(&self) -> ActorRef<!>
pub fn deadletter_ref(&self) -> ActorRef<!>
Returns a reference to the system’s deadletter box
Sourcepub fn suicide(&self)
pub fn suicide(&self)
Destroys this component lazily
This simply sends a Kill event to itself,
which means that other events may still be handled
before the component is actually killed.
For a more immediate alternative see Handled::Shutdown.
Sourcepub fn preserialise<B>(&self, content: &B) -> Result<ChunkRef, SerError>where
B: Serialisable,
pub fn preserialise<B>(&self, content: &B) -> Result<ChunkRef, SerError>where
B: Serialisable,
Attempts to create a ChunkRef of the data using the local EncodeBuffer, to be used with tell_preserialised
Sourcepub fn init_buffers(
&self,
buffer_config: Option<BufferConfig>,
custom_allocator: Option<Arc<dyn ChunkAllocator>>,
)
pub fn init_buffers( &self, buffer_config: Option<BufferConfig>, custom_allocator: Option<Arc<dyn ChunkAllocator>>, )
May be used for manual initialization of a components local EncodeBuffer. If this method is never called explicitly the actor will implicitly call it when it tries to serialise its first message.
If buffers have already been initialized, explicitly or implicitly, the method does nothing.
A custom BufferConfig may be specified, if None is given the
actor will first try to parse the configuration from the system wide configuration,
if that fails, the default config will be used.
A custom ChunkAllocator may also be specified.
if None is given the actor will use the default allocator.
Note: The BufferConfig within the systems NetworkConfig never
affects the Actors Buffers (i.e. this method).
Sourcepub fn set_recovery_function<F>(&self, f: F)
pub fn set_recovery_function<F>(&self, f: F)
Set the recovery function for this component
See RecoveryHandler for more information.
You can perform action repeatedly in order to store state to use while recovering from a failure. Do note, however, that this call causes an additional mutex lock which is not necessarily cheap and can theoretically deadlock, if you call this from more than one place.
Trait Implementations§
Source§impl<CD> ActorPathFactory for ComponentContext<CD>where
CD: ComponentTraits + ComponentLifecycle,
Available on crate feature distributed only.
impl<CD> ActorPathFactory for ComponentContext<CD>where
CD: ComponentTraits + ComponentLifecycle,
distributed only.