public final class BasicTwillContext extends Object implements TwillContext
TwillContext
that provides the basic runtime information of a TwillRunnable
.Constructor and Description |
---|
BasicTwillContext(RunId runId,
RunId appRunId,
InetAddress host,
String[] args,
String[] appArgs,
TwillRunnableSpecification spec,
int instanceId,
DiscoveryService discoveryService,
DiscoveryServiceClient discoveryServiceClient,
ZKClient zkClient,
int instanceCount,
int allowedMemoryMB,
int virtualCores) |
Modifier and Type | Method and Description |
---|---|
Cancellable |
announce(String serviceName,
int port)
Registers an endpoint that could be discovered by external party.
|
Cancellable |
announce(String serviceName,
int port,
byte[] payload)
Registers an endpoint that could be discovered by external party with a payload.
|
Lock |
createLock(String name)
Creates a
Lock for performing locking mechanism within the same TwillApplication . |
ServiceDiscovered |
discover(String name)
Discover service with the given name that is announced within the same
TwillApplication . |
Cancellable |
electLeader(String name,
ElectionHandler participantHandler)
Register to participate in a leader election by instances within the same
TwillApplication . |
String[] |
getApplicationArguments()
Returns the runtime arguments that are passed to the
TwillApplication . |
RunId |
getApplicationRunId()
Returns the
RunId of this running application. |
String[] |
getArguments()
Returns the runtime arguments that are passed to the
TwillRunnable . |
InetAddress |
getHost()
Returns the hostname that the runnable is running on.
|
int |
getInstanceCount()
Returns the number of running instances assigned for this
TwillRunnable . |
int |
getInstanceId()
Returns an integer id from 0 to (instanceCount - 1).
|
int |
getMaxMemoryMB()
Returns the amount of memory in MB the runnable is allowed to use.
|
RunId |
getRunId()
Returns the
RunId of this running instance of TwillRunnable . |
TwillRunnableSpecification |
getSpecification()
Returns the
TwillRunnableSpecification that was created by TwillRunnable.configure() . |
int |
getVirtualCores()
Returns the number of virtual cores the runnable is allowed to use.
|
void |
setInstanceCount(int count) |
void |
stop()
Stops and frees any currently allocated resources.
|
public BasicTwillContext(RunId runId, RunId appRunId, InetAddress host, String[] args, String[] appArgs, TwillRunnableSpecification spec, int instanceId, DiscoveryService discoveryService, DiscoveryServiceClient discoveryServiceClient, ZKClient zkClient, int instanceCount, int allowedMemoryMB, int virtualCores)
public RunId getRunId()
TwillContext
RunId
of this running instance of TwillRunnable
.getRunId
in interface TwillContext
public RunId getApplicationRunId()
TwillContext
RunId
of this running application.getApplicationRunId
in interface TwillContext
public int getInstanceCount()
TwillContext
TwillRunnable
.getInstanceCount
in interface TwillContext
public void setInstanceCount(int count)
public InetAddress getHost()
TwillContext
getHost
in interface TwillContext
public String[] getArguments()
TwillContext
TwillRunnable
.getArguments
in interface TwillContext
public String[] getApplicationArguments()
TwillContext
TwillApplication
.getApplicationArguments
in interface TwillContext
public TwillRunnableSpecification getSpecification()
TwillContext
TwillRunnableSpecification
that was created by TwillRunnable.configure()
.getSpecification
in interface TwillContext
public int getInstanceId()
TwillContext
getInstanceId
in interface TwillContext
public int getVirtualCores()
TwillContext
getVirtualCores
in interface TwillContext
public int getMaxMemoryMB()
TwillContext
getMaxMemoryMB
in interface TwillContext
public Cancellable announce(String serviceName, int port)
ServiceAnnouncer
announce
in interface ServiceAnnouncer
serviceName
- Name of the endpointport
- Port of the endpointpublic Cancellable announce(String serviceName, int port, byte[] payload)
ServiceAnnouncer
announce
in interface ServiceAnnouncer
serviceName
- Name of the endpointport
- Port of the endpointpayload
- byte array payloadpublic ServiceDiscovered discover(String name)
TwillContext
TwillApplication
.discover
in interface TwillContext
discover
in interface DiscoveryServiceClient
name
- Name of the serviceServiceDiscovered
object representing the result.public Cancellable electLeader(String name, ElectionHandler participantHandler)
TwillContext
TwillApplication
.electLeader
in interface TwillContext
name
- Unique name for the electionCancellable
object representing this candidate's participation.public Lock createLock(String name)
TwillContext
Lock
for performing locking mechanism within the same TwillApplication
.
The lock created is a reentrant such that if the lock is owned by the current thread, it will return immediately.createLock
in interface TwillContext
name
- Unique name for the lockLock
objectpublic void stop()
Copyright © 2013-2020 The Apache Software Foundation. All rights reserved.