# 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.

```apex
public class FailureCallback implements EventBus.EventPublishFailureCallback {
      
   public void onFailure(EventBus.FailureResult result) {
       // Your implementation
       // Get event UUIDs from the result
       List<String> eventUuids = result.getEventUuids();
       // ...
   }        
}

```

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.

```apex
public class SuccessCallback implements EventBus.EventPublishSuccessCallback {
      
   public void onSuccess(EventBus.SuccessResult result) {
       // Your implementation
       // Get event UUIDs from the result
       List<String> eventUuids = result.getEventUuids();
       // …
   }        
}

```

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.

:::note
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.
:::

```apex
public class FailureAndSuccessCallback implements EventBus.EventPublishFailureCallback,
    EventBus.EventPublishSuccessCallback {

   public void onFailure(EventBus.FailureResult result) {
       // Your implementation
       // Get event UUIDs from the result
       List<String> eventUuids = result.getEventUuids();
       // …
   } 
      
   public void onSuccess(EventBus.SuccessResult result) {
       // Your implementation
       // Get event UUIDs from the result
       List<String> eventUuids = result.getEventUuids();
       // …
   }        
}

```

**See Also**

- [Identify and Match Event Messages with the EventUuid Field](/docs/platform/platform-events/guide/platform-events-event-uuid.md)
- [*Apex Reference Guide*: System.Quiddity Enum: PLATFORM_EVENT_PUBLISH_CALLBACK](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_System_Quiddity.htm)
