Newer Version Available

This content describes an older version of this product. View Latest

Apex Publish Callback Class

An Apex publish callback contains the result of an asynchronous publish operation in Apex. After the publish operation completes and the final result is ready, the system returns a callback. You can implement one of these two interfaces: EventBus.EventPublishFailureCallback for failed publishes and EventBus.EventPublishSuccessCallback for successful publishes.

Track Event Publish Failures

To track the final failed result of asynchronous publish operations, implement the EventBus.EventPublishFailureCallback interface in an Apex class. In your implementation, you can decide what action to take for publish failures. For example, you can log the failures or you can attempt to republish the events.

1public class FailureCallback implements EventBus.EventPublishFailureCallback {
2      
3   public void onFailure(EventBus.FailureResult result) {
4       // Your implementation.
5       // Get event UUIDs from the result
6       List<String> eventUuids = result.getEventUuids();
7       // ...
8   }        
9}
10

If the asynchronous publish operation fails, the onFailure method is invoked. In the implemented onFailure method, you can write logic to act in response to the final result of the publishing operation. The onFailure method takes a parameter that contains the result of the publish operation: EventBus.FailureResult result. The result contains the EventUuid field values for each failed event but doesn’t contain the data for the event. Use the getEventUuids method to get the universally unique identifiers (UUIDs) of the events. Each event UUID is a UUID that identifies an event message.

Track Event Publish Successes

To track the final successful result of asynchronous publish operations, implement the EventBus.EventPublishSuccessCallback interface in an Apex class. Because most publish calls typically succeed, processing successful event publishes likely isn’t a concern. Also, a large volume of events can be published successfully, so be mindful about the performance and Apex limit impacts when processing the results.

1public class SuccessCallback implements EventBus.EventPublishSuccessCallback {
2      
3   public void onSuccess(EventBus.SuccessResult result) {
4       // Your implementation.
5       // Get event UUIDs from the result
6       List<String> eventUuids = result.getEventUuids();
7       // …
8   }        
9}
10

If the asynchronous publish operation succeeds, the onSuccess method is invoked. In the implemented onSuccess method, you can write logic to act in response to the final result of the publishing operation. The onSuccess method takes a parameter that contains the result of the publish operation: EventBus.SuccessResult result. The result contains the EventUuid field values for each successfully published event but doesn’t contain the data for the event. Use the getEventUuids method to get the UUIDs of the events. Each event UUID is a UUID that identifies an event message.

Track Event Publish Failures and Successes in One Callback

Alternatively, you can process failed and successful publish results in one Apex class. Implement the EventBus.EventPublishFailureCallback and EventBus.EventPublishSuccessCallback interfaces in the same Apex class. The interface includes the onFailure and onSuccess methods.

Implement both failure and success callbacks only if you have valid use cases for processing both. Because most publish calls typically succeed, processing successful event publishes likely isn’t a concern. Also, a large volume of events can be published successfully, so be mindful about the performance and Apex limit impacts when processing the results in the onSuccess method.

Note

1public class FailureAndSuccessCallback implements EventBus.EventPublishFailureCallback,
2    EventBus.EventPublishSuccessCallback {
3
4   public void onFailure(EventBus.FailureResult result) {
5       // Your implementation.
6       // Get event UUIDs from the result
7       List<String> eventUuids = result.getEventUuids();
8       // …
9   } 
10      
11   public void onSuccess(EventBus.SuccessResult result) {
12       // Your implementation.
13       // Get event UUIDs from the result
14       List<String> eventUuids = result.getEventUuids();
15       // …
16   }        
17}
18