Cloud Pub/Sub v1 API - Class PublisherClient (3.4.0)

public abstract class PublisherClient : IAsyncDisposable

Reference documentation and code samples for the Cloud Pub/Sub v1 API class PublisherClient.

A PubSub publisher that is associated with a specific TopicName.

Inheritance

object > PublisherClient

Implements

IAsyncDisposable

Derived Types

Namespace

GoogleGoogle.CloudGoogle.Cloud.PubSubV1

Assembly

Google.Cloud.PubSub.V1.dll

Remarks

This class implements the IAsyncDisposable interface. However, it is recommended to create a single PublisherClient instance and use it throughout the lifetime of the application. If the PublisherClient is registered in a dependency injection container, its DisposeAsync method will be called automatically.

Properties

ApiMaxBatchingSettings

public static BatchingSettings ApiMaxBatchingSettings { get; }

The absolute maximum BatchingSettings supported by the service. Maximum values are: ElementCountThreshold = 1,000; ByteCountThreshold = 10,000,000;

Property Value
TypeDescription
BatchingSettings

DefaultBatchingSettings

public static BatchingSettings DefaultBatchingSettings { get; }

Default BatchingSettings for PublisherClient. Default values are: ElementCountThreshold = 100; ByteCountThreshold = 1,000,000; DelayThreshold = 10 milliseconds;

Property Value
TypeDescription
BatchingSettings

DefaultDisposeTimeout

public static TimeSpan DefaultDisposeTimeout { get; }

The default DisposeTimeout of 5 seconds for the PublisherClient.

Property Value
TypeDescription
TimeSpan

TopicName

public virtual TopicName TopicName { get; }

The associated TopicName.

Property Value
TypeDescription
TopicName

Methods

Create(TopicName)

public static PublisherClient Create(TopicName topicName)

Create a PublisherClient instance associated with the specified TopicName, using default settings.

Parameter
NameDescription
topicNameTopicName

The TopicName to publish messages to. Must not be null.

Returns
TypeDescription
PublisherClient

A PublisherClient instance associated with the specified TopicName.

Create(TopicName, ClientCreationSettings, Settings)

[Obsolete("Use PublisherClient.Create(TopicName) to use the default settings, or PublisherClientBuilder for customization.")]
public static PublisherClient Create(TopicName topicName, PublisherClient.ClientCreationSettings clientCreationSettings = null, PublisherClient.Settings settings = null)

Create a PublisherClient instance associated with the specified TopicName. The default settings and clientCreationSettings are suitable for machines with high network bandwidth (e.g. Google Compute Engine instances). If running with more limited network bandwidth, some settings may need changing; especially PublisherServiceApiSettings.PublishSettings.Retry. By default this method generates a gRPC channel per CPU core; if using a high-core-count machine and using many clients concurrently then this may need reducing; use the setting ClientCount.

Parameters
NameDescription
topicNameTopicName

The TopicName to publish messages to. Must not be null.

clientCreationSettingsPublisherClientClientCreationSettings

Optional. PublisherClient.ClientCreationSettings specifying how to create PublisherServiceApiClients.

settingsPublisherClientSettings

Optional. PublisherClient.Settings for creating a PublisherClient.

Returns
TypeDescription
PublisherClient

A PublisherClient instance associated with the specified TopicName.

CreateAsync(TopicName)

public static Task<PublisherClient> CreateAsync(TopicName topicName)

Creates a PublisherClient instance associated with the specified TopicName, using default settings.

Parameter
NameDescription
topicNameTopicName

The TopicName to publish messages to. Must not be null.

Returns
TypeDescription
TaskPublisherClient

A PublisherClient instance associated with the specified TopicName.

CreateAsync(TopicName, ClientCreationSettings, Settings)

[Obsolete("Use PublisherClient.CreateAsync(TopicName) to use the default settings, or PublisherClientBuilder for customization.")]
public static Task<PublisherClient> CreateAsync(TopicName topicName, PublisherClient.ClientCreationSettings clientCreationSettings = null, PublisherClient.Settings settings = null)

Create a PublisherClient instance associated with the specified TopicName. The default settings and clientCreationSettings are suitable for machines with high network bandwidth (e.g. Google Compute Engine instances). If running with more limited network bandwidth, some settings may need changing; especially PublisherServiceApiSettings.PublishSettings.Retry. By default this method generates a gRPC channel per CPU core; if using a high-core-count machine and using many clients concurrently then this may need reducing; use the setting ClientCount.

Parameters
NameDescription
topicNameTopicName

The TopicName to publish messages to. Must not be null.

clientCreationSettingsPublisherClientClientCreationSettings

Optional. PublisherClient.ClientCreationSettings specifying how to create PublisherServiceApiClients.

settingsPublisherClientSettings

Optional. PublisherClient.Settings for creating a PublisherClient.

Returns
TypeDescription
TaskPublisherClient

A PublisherClient instance associated with the specified TopicName.

DisposeAsync()

public virtual ValueTask DisposeAsync()

Disposes this PublisherClient asynchronously.

Returns
TypeDescription
ValueTask
Remarks

This method asynchronously waits for the time interval as specified in the DisposeTimeout for the PublisherClient to send any pending messages. If the clean shutdown is not complete after this time, it is aborted; this may leave some locally queued messages unsent. The time interval can be customized by setting the DisposeTimeout.

PublishAsync(PubsubMessage)

public virtual Task<string> PublishAsync(PubsubMessage message)

Publish a message to the topic specified in TopicName.

Parameter
NameDescription
messagePubsubMessage

The PubsubMessage to publish.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(ByteString)

public virtual Task<string> PublishAsync(ByteString message)

Publish a message to the topic specified in TopicName.

Parameter
NameDescription
messageByteString

The ByteString to publish.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(IMessage)

public virtual Task<string> PublishAsync(IMessage message)

Publish a message to the topic specified in TopicName.

Parameter
NameDescription
messageIMessage

The message to publish.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(byte[])

public virtual Task<string> PublishAsync(byte[] message)

Publish a message to the topic specified in TopicName.

Parameter
NameDescription
messagebyte

The message to publish.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(string, ByteString)

public virtual Task<string> PublishAsync(string orderingKey, ByteString message)

Publish a message to the topic specified in TopicName.

Parameters
NameDescription
orderingKeystring

The ordering key to use for this message.

messageByteString

The ByteString to publish.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(string, IMessage)

public virtual Task<string> PublishAsync(string orderingKey, IMessage message)

Publish a message to the topic specified in TopicName.

Parameters
NameDescription
orderingKeystring

The ordering key to use for this message.

messageIMessage

The message to publish.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(string, byte[])

public virtual Task<string> PublishAsync(string orderingKey, byte[] message)

Publish a message to the topic specified in TopicName.

Parameters
NameDescription
orderingKeystring

The ordering key to use for this message.

messagebyte

The message to publish.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(string, string, Encoding)

public virtual Task<string> PublishAsync(string orderingKey, string message, Encoding encoding = null)

Publish a message to the topic specified in TopicName.

Parameters
NameDescription
orderingKeystring

The ordering key to use for this message.

messagestring

The string to publish.

encodingEncoding

The encoding for string to byte conversion. If null, defaults to UTF8.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

PublishAsync(string, Encoding)

public virtual Task<string> PublishAsync(string message, Encoding encoding = null)

Publish a message to the topic specified in TopicName.

Parameters
NameDescription
messagestring

The string to publish.

encodingEncoding

The encoding for string to byte conversion. If null, defaults to UTF8.

Returns
TypeDescription
Taskstring

A task which completes once the message has been published. The task Result contains the message ID.

ResumePublish(string)

public virtual void ResumePublish(string orderingKey)

Re-enable publishing the the given ordering key, after an error has caused the ordering key to be disabled.

Parameter
NameDescription
orderingKeystring

The ordering key to re-enable.

ShutdownAsync(CancellationToken)

public virtual Task ShutdownAsync(CancellationToken hardStopToken)

Shutdown this PublisherClient. Cancelling hardStopToken aborts the clean shutdown process, and may leave some locally queued messages unsent. The returned Task completes when all queued messages have been published. The returned Task cancels if hardStopToken is cancelled.

Parameter
NameDescription
hardStopTokenCancellationToken

Cancel this CancellationToken to abort publishing queued messages.

Returns
TypeDescription
Task

A Task that completes when all queued messages have been published; or cancels if hardStopToken is cancelled.

ShutdownAsync(TimeSpan)

public virtual Task ShutdownAsync(TimeSpan timeout)

Shutdown this PublisherClient. If timeout expires, the clean shutdown process will abort; leaving some locally queued messages unsent. The returned Task completes when all queued messages have been published. The returned Task cancels if the timeout expires before all messages are published.

Parameter
NameDescription
timeoutTimeSpan

After this period, abort publishing queued messages.

Returns
TypeDescription
Task

A Task that completes when all queued messages have been published; or cancels if timeout expires.