Create message bus (v1)

Create a new Eventarc message bus.

Code sample

Java

To authenticate to Eventarc, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import com.google.api.gax.rpc.AlreadyExistsException;
import com.google.api.gax.rpc.ResourceExhaustedException;
import com.google.cloud.eventarc.v1.EventarcClient;
import com.google.cloud.eventarc.v1.LocationName;
import com.google.cloud.eventarc.v1.MessageBus;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class CreateMessageBus {

  public static void main(String[] args) throws Exception {
    String messageBusId = "example-message-bus";
    String projectId = "example-project-id";
    String locationId = "us-central1";
    createMessageBus(messageBusId, projectId, locationId);
  }

  public static void createMessageBus(String messageBusId, String projectId, String locationId)
      throws Exception {
    try (EventarcClient client = EventarcClient.create()) {
      String parent = LocationName.of(projectId, locationId).toString();
      MessageBus messageBus = MessageBus.newBuilder().build();

      try {
        MessageBus response = client.createMessageBusAsync(parent, messageBus, messageBusId).get();
        System.out.println("Created message bus: " + response.getName());
      } catch (ExecutionException e) {
        if (e.getCause() instanceof ResourceExhaustedException) {
          System.out.println("Quota limit reached for message buses in region: " + locationId);
        } else if (e.getCause() instanceof AlreadyExistsException) {
          System.out.println("Message bus already exists: " + messageBusId);
        } else {
          throw e;
        }
      }
    }
  }
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.