public class ChronicleContext
extends net.openhft.chronicle.core.io.SimpleCloseable
The ChronicleContext provides methods to set important parameters such as the context's URL, whether to use buffering and affinity, and the context's name (which can define the relative directory). It also manages the lifecycle of closeable resources and provides the URL, buffering state, and the socket registry.
Example usage:
String url = "tcp://:0";
try (ChronicleContext context = ChronicleContext.newContext(url)
.name("target/server")
.buffered(true)
.useAffinity(true)) {
ChronicleChannel channel = context.newChannelSupplier(new EchoHandler().buffered(false)).connectionTimeoutSecs(1).get();
Says says = channel.methodWriter(Says.class);
says.say("Hello World");
StringBuilder eventType = new StringBuilder();
String text = channel.readOne(eventType, String.class);
assertEquals("say: Hello World", eventType + ": " + text);
| Modifier | Constructor and Description |
|---|---|
protected |
ChronicleContext(String url)
Protected constructor for creating a Chronicle context with the specified URL.
|
protected |
ChronicleContext(String url,
SocketRegistry socketRegistry)
Protected constructor for creating a Chronicle context with the specified URL and socket registry.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addCloseable(net.openhft.chronicle.core.io.Closeable closeable)
Adds a Closeable resource to the context's internal set of resources.
|
net.openhft.affinity.AffinityLock |
affinityLock() |
boolean |
buffered()
Indicates whether buffering should be used in this context.
|
ChronicleContext |
buffered(boolean buffered)
Sets the buffering preference for this context.
|
protected void |
init()
Initializes the context, specifically it creates a new SocketRegistry instance if one is not already set.
|
String |
name() |
ChronicleContext |
name(String name)
Sets the names space for the service.
|
ChronicleChannelSupplier |
newChannelSupplier(ChannelHandler handler)
Constructs a new ChronicleChannelSupplier object using the provided ChannelHandler and the settings from this context.
|
static ChronicleContext |
newContext(String url)
Factory method for a new ChronicleContext
|
protected void |
performClose()
Closes all resources managed by this context in a quiet manner, i.e., without throwing any exceptions.
|
int |
port() |
SocketRegistry |
socketRegistry()
Retrieves the SocketRegistry associated with this context.
|
void |
startNewGateway()
Starts a new instance of ChronicleGatewayMain if one is not already instantiated for this context.
|
SystemContext |
systemContext()
Retrieves the current SystemContext for this process or remote host.
|
void |
systemContext(SystemContext systemContext)
Sets the system context for this context.
|
File |
toFile(String name)
Returns a new File instance for the given name space (sub-directory) within this context.
|
String |
toString() |
URL |
url()
Retrieves the URL of this context.
|
static URL |
urlFor(String spec)
Parses a URL from a string, initializing any custom handlers as necessary.
|
boolean |
useAffinity()
Retrieves the status of affinity usage in this context.
|
ChronicleContext |
useAffinity(boolean useAffinity)
Sets the status of affinity usage in this context.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitreferenceId, referenceName, temporaryprotected ChronicleContext(String url)
url - the URL for this contextprotected ChronicleContext(String url, SocketRegistry socketRegistry)
url - the URL for this contextsocketRegistry - the socket registry for this contextpublic static ChronicleContext newContext(String url)
url - to connect topublic static URL urlFor(String spec) throws net.openhft.chronicle.core.io.IORuntimeException
This method supports "internal:" and "tcp:" URL schemas in addition to the standard schemas. If a URL starts with "internal:", a new Handler object is used as the URLStreamHandler. If a URL starts with "tcp:", a new tcp.Handler object is used as the URLStreamHandler. For other URL schemas, no custom URLStreamHandler is used.
spec - the string to parse as a URL.net.openhft.chronicle.core.io.IORuntimeException - if the string cannot be parsed as a URL.public net.openhft.affinity.AffinityLock affinityLock()
public boolean useAffinity()
public ChronicleContext useAffinity(boolean useAffinity)
useAffinity - a boolean flag indicating whether to enable affinity usage.protected void init()
public ChronicleChannelSupplier newChannelSupplier(ChannelHandler handler) throws net.openhft.chronicle.core.io.InvalidMarshallableException
handler - the ChannelHandler to be used by the new ChronicleChannelSupplier.net.openhft.chronicle.core.io.InvalidMarshallableException - if there is an error during the validation of the header.public void startNewGateway()
throws net.openhft.chronicle.core.io.InvalidMarshallableException
net.openhft.chronicle.core.io.InvalidMarshallableException - if there's an error during the creation or starting of the gateway.public void addCloseable(net.openhft.chronicle.core.io.Closeable closeable)
closeable - the Closeable resource to add.protected void performClose()
performClose in class net.openhft.chronicle.core.io.SimpleCloseablepublic URL url()
public boolean buffered()
public ChronicleContext buffered(boolean buffered)
buffered - a boolean representing the preference for buffering.public SocketRegistry socketRegistry()
SocketRegistry for this context.public void systemContext(SystemContext systemContext) throws net.openhft.chronicle.core.io.InvalidMarshallableException
systemContext - the new system context to be set.net.openhft.chronicle.core.io.InvalidMarshallableException - if there's an error while performing a deep copy of the system context.public SystemContext systemContext()
SystemContext for this process or remote host.public ChronicleContext name(String name)
name - space of this contextpublic String name()
public File toFile(String name)
name - the name for the new Filepublic String toString()
public int port()
Copyright © 2023. All rights reserved.