共用方式為


StatefulServiceBase Class

  • java.lang.Object
    • microsoft.servicefabric.services.runtime.StatefulServiceBase

public class StatefulServiceBase

Represents base class for Microsoft Service Fabric based stateful reliable service.

Constructor Summary

Constructor Description
StatefulServiceBase(StatefulServiceContext serviceContext, StateProviderReplica stateProviderReplica)

Creates a new StatefulService.

Method Summary

Modifier and Type Method and Description
CompletableFuture<?> backupAsync(BackupDescription backupDescription)

Performs a backup of all reliable state managed by this StatefulServiceBase.

CompletableFuture<?> backupAsync(BackupDescription backupDescription, Duration timeout, CancellationToken cancellationToken)

Performs a backup of all reliable state managed by this StatefulServiceBase.

List<ServiceReplicaListener> createServiceReplicaListeners()

Override this method to supply the communication listeners for the service replica. The endpoints returned by the communication listener's are stored as a JSON string of ListenerName, Endpoint string pairs like {"Endpoints":{"Listener1":"Endpoint1","Listener2":"Endpoint2" ...}}

Map<String, String> getAddresses()

Gets list of all the addresses for this service replica as (ListenerName, Endpoint) key-value pair.

StatefulServicePartition getPartition()

Service partition to which current service replica belongs.

StatefulServiceContext getServiceContext()

Gets the service context that this stateful service is operating under. It provides information like ReplicaId, PartitionId, ServiceName etc.

StateProviderReplica getStateProviderReplica()
void onAbort()

Notification that the service is being aborted. RunAsync MAY be running concurrently with the execution of this method, as cancellation is not awaited on the abort path.

CompletableFuture<?> onChangeRoleAsync(ReplicaRole newRole, CancellationToken cancellationToken)

This method is called as the final step before completing changeRoleAsync(ReplicaRole newRole, CancellationToken cancellationToken) . Override this method to be notified that ChangeRole has completed for this replica's internal components.

CompletableFuture<?> onCloseAsync(CancellationToken cancellationToken)

This method is called as the final step of closing the service. Override this method to be notified that Close has completed for this replica's internal components.

CompletableFuture<Boolean> onDataLossAsync(RestoreContext context, CancellationToken cancellationToken)

This method is called during suspected data loss. You can override this method to restore the service in case of data loss.

CompletableFuture<?> onOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)

This method is called as the final step of opening the service. Override this method to be notified that Open has completed for this replica's internal components.

CompletableFuture<?> runAsync(CancellationToken cancellationToken)

Services that want to implement a processing loop which runs when it is primary and has write status, just override this method with their logic.

void setAddresses(Map<String, String> addresses)

Sets list of all the addresses for this service replica as (ListenerName, Endpoint) key-value pair.

void setPartition(StatefulServicePartition partition)

Service partition to which current service replica belongs.

Constructor Details

StatefulServiceBase

protected StatefulServiceBase(StatefulServiceContext serviceContext, StateProviderReplica stateProviderReplica)

Creates a new StatefulService.

Parameters:

serviceContext - A StatefulServiceContext that describes the service context.
stateProviderReplica - A StateProviderReplica that represents a reliable state provider replica.

Method Details

backupAsync

public CompletableFuture backupAsync(BackupDescription backupDescription)

Performs a backup of all reliable state managed by this StatefulServiceBase.

Parameters:

backupDescription - A BackupDescription describing the backup request.

Returns:

Task that represents the asynchronous backup operation.

backupAsync

public CompletableFuture backupAsync(BackupDescription backupDescription, Duration timeout, CancellationToken cancellationToken)

Performs a backup of all reliable state managed by this StatefulServiceBase.

Parameters:

backupDescription - A BackupDescription describing the backup request.
timeout - The timeout for this operation.
cancellationToken - The token to monitor for cancellation requests.

Returns:

Task that represents the asynchronous backup operation.

createServiceReplicaListeners

protected List createServiceReplicaListeners()

Override this method to supply the communication listeners for the service replica. The endpoints returned by the communication listener's are stored as a JSON string of ListenerName, Endpoint string pairs like {"Endpoints":{"Listener1":"Endpoint1","Listener2":"Endpoint2" ...}}

Returns:

List of ServiceReplicaListeners

getAddresses

protected Map getAddresses()

Gets list of all the addresses for this service replica as (ListenerName, Endpoint) key-value pair.

Returns:

An Map containing list of addresses as (ListenerName, Endpoint) key-value pair.

getPartition

protected StatefulServicePartition getPartition()

Service partition to which current service replica belongs.

Returns:

An StatefulServicePartition that represents the partition to which this service replica belongs.

getServiceContext

public StatefulServiceContext getServiceContext()

Gets the service context that this stateful service is operating under. It provides information like ReplicaId, PartitionId, ServiceName etc.

Returns:

A StatefulServiceContext that describes the service context.

getStateProviderReplica

protected StateProviderReplica getStateProviderReplica()

onAbort

protected void onAbort()

Notification that the service is being aborted. RunAsync MAY be running concurrently with the execution of this method, as cancellation is not awaited on the abort path.

onChangeRoleAsync

protected CompletableFuture onChangeRoleAsync(ReplicaRole newRole, CancellationToken cancellationToken)

This method is called as the final step before completing changeRoleAsync(ReplicaRole newRole, CancellationToken cancellationToken) . Override this method to be notified that ChangeRole has completed for this replica's internal components.

Parameters:

newRole - for this service replica.
cancellationToken -

CancellationToken object to indicate the cancellation status of the operation.

Returns:

A CompletableFuture that represents outstanding operation.

onCloseAsync

protected CompletableFuture onCloseAsync(CancellationToken cancellationToken)

This method is called as the final step of closing the service. Override this method to be notified that Close has completed for this replica's internal components.

Parameters:

cancellationToken -

CancellationToken object to indicate the cancellation status of the operation.

Returns:

A CompletableFuture that represents outstanding operation.

onDataLossAsync

protected CompletableFuture onDataLossAsync(RestoreContext context, CancellationToken cancellationToken)

This method is called during suspected data loss. You can override this method to restore the service in case of data loss.

Parameters:

context - A RestoreContext to be used to restore the service.
cancellationToken -

CancellationToken object to indicate the cancellation status of the operation.

Returns:

A Task that represents the asynchronous restore operation. True indicates that the state has been restored. False indicates that the replica's state has not been modified.

onOpenAsync

protected CompletableFuture onOpenAsync(ReplicaOpenMode openMode, CancellationToken cancellationToken)

This method is called as the final step of opening the service. Override this method to be notified that Open has completed for this replica's internal components.

Parameters:

openMode - for this service replica.
cancellationToken -

CancellationToken object to indicate the cancellation status of the operation.

Returns:

A CompletableFuture that represents outstanding operation.

runAsync

protected CompletableFuture runAsync(CancellationToken cancellationToken)

Services that want to implement a processing loop which runs when it is primary and has write status, just override this method with their logic.

Parameters:

cancellationToken -

CancellationToken object to indicate the cancellation status of the operation.

Returns:

A CompletableFuture that represents outstanding operation.

setAddresses

protected void setAddresses(Map addresses)

Sets list of all the addresses for this service replica as (ListenerName, Endpoint) key-value pair.

Parameters:

addresses - An Map containing list of addresses as (ListenerName, Endpoint) key-value pair.

setPartition

protected void setPartition(StatefulServicePartition partition)

Service partition to which current service replica belongs.

Parameters:

partition - An StatefulServicePartition that represents the partition to which this service replica belongs.

Applies to