Example: Publish Callback Class That Creates Follow-Up Tasks for Failed Publishes

This publish callback class creates a task when event publishing fails in the onFailure method. The inserted task includes the number of failed events and the event UUIDs.
public class FailureCallback implements EventBus.EventPublishFailureCallback {
    
    public void onFailure(EventBus.FailureResult result) {
        List<String> eventUuids = result.getEventUuids();
        System.debug(eventUuids.size() + ' events failed to publish.');      
        System.debug('FailureCallback eventUuids to match with event objects: ' +
            eventUuids);

        // Create a follow-up task
        insertTask(eventUuids, false);
    }
    
    private void insertTask(List<String> eventUuids, Boolean isSuccess) {
        String eventIdString = '';
        for (String evtId : eventUuids) {
            eventIdString += evtId + ' ';
        }
        Task t = new Task();
        if (isSuccess == false) {
            t.Subject = 'Follow up on event publishing failures.';
            t.Description = eventUuids.size() + 
                ' events failed to publish. Event UUIDs: '
            + eventIdString; 
        }

        // Set the due date
        t.ActivityDate = Date.today().addDays(3);
        // Set owner ID explicitly. 
        // Otherwise, the task assignee is the Automated Process User.
        // Change the user ID to a valid user ID in your org.
        t.OwnerId = '005RM000002QhQ1YAK';
        // Insert task
        Database.SaveResult sr = Database.insert(t);
        if (!sr.isSuccess()) {
            for(Database.Error err : sr.getErrors()) {
                System.debug('Error returned: ' +
                             err.getStatusCode() +
                             ' - ' +
                             err.getMessage());
            }
        }
    }
}