How to Send a Notification to Google Cloud Pub/Sub with Java

import java.io.FileInputStream;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.services.pubsub.Pubsub;
import com.google.api.services.pubsub.PubsubScopes;
import com.google.api.services.pubsub.model.PublishRequest;
import com.google.api.services.pubsub.model.PubsubMessage;
import com.google.common.collect.ImmutableList;

public final class PubsubSample {

  public static void main(final String[] args) throws Exception {
    new Pubsub.Builder(Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), GoogleCredential.fromStream(new FileInputStream("projectid.json")).createScoped(PubsubScopes.all())).build().projects().topics().publish("projects/projectid/topics/SampleTopic", new PublishRequest().setMessages(ImmutableList.of(new PubsubMessage().encodeData("Simples Assim".getBytes())))).execute();
  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s