diff --git a/docs_src/microservices/application/AdvancedTopics.md b/docs_src/microservices/application/AdvancedTopics.md index 703722aa0b..723d92d6f5 100644 --- a/docs_src/microservices/application/AdvancedTopics.md +++ b/docs_src/microservices/application/AdvancedTopics.md @@ -424,6 +424,61 @@ If in secure mode, the secrets are retrieved from the secret store based on the If running in insecure mode, the secrets are retrieved from the *Writable.InsecureSecrets* configuration. +### Background Publishing + +Application Services using the MessageBus trigger can request a background publisher using the AddBackgroundPublisher API in the SDK. This method takes an int representing the background channel's capacity as the only parameter and returns a reference to a BackgroundPublisher. This reference can then be used by background processes to publish to the configured MessageBus output. Example usage is as follows: + +```go + +func runJob (pub appsdk.BackgroundPublisher, done chan struct{}){ + ticker := time.NewTicker(1 * time.Minute) + go func() { + for { + select { + case <-ticker.C: + msg := myDataService.GetMessage() + payload, err := json.Marshal(message) + + if err != nil { + continue + } + pub.Publish(payload, uuid.New().String(), clients.ContentTypeJSON) + case <-j.done: + ticker.Stop() + return + } + } + }() +} + +func main() { + edgexSdk := &appsdk.AppFunctionsSDK{ServiceKey: serviceKey} + if err := edgexSdk.Initialize(); err != nil { + edgexSdk.LoggingClient.Error(fmt.Sprintf("SDK initialization failed: %v", err)) + os.Exit(-1) + } + + //initialize background publisher with a channel capacity of 10 + pub := sdk.AddBackgroundPublisher(10) + + done := make(chan struct{}) + defer close(done) + + //pass publisher to your background job + runJob(pub, done) + + edgexSdk.SetFunctionsPipeline( + All, + My, + Functions, + ) + + edgexSdk.MakeItRun() + + os.Exit(0) +} +``` + ### Registry Client **After initialization**, the configured registry client used by the SDK can be retrieved from the sdk instance at .RegistryClient. It is important to note that sdk.RegistryClient may be nil - either if the SDK is not yet initialized, or if the registry option (-r/--registry) is not specified on start. Once retrieved the client can be used to look up host information for other services, or perform other operations supported by the registry.Client type in [go-mod-registry](https://github.com/edgexfoundry/go-mod-registry). For example, to retrieve the URL for a given service: @@ -445,4 +500,4 @@ func(sdk *appsdk.AppFunctionsSDK, serviceKey string) (string, error) { ``` !!! note Known Service Keys Service keys for known EdgeX services can be found under clients in [go-mod-core-contracts](https://github.com/edgexfoundry/go-mod-core-contracts/blob/master/clients/constants.go#L58-L72) - + \ No newline at end of file