Skip to content

thaiphamquoc/gcloud-pubsub-sandbox

Repository files navigation

Requirements

  • Maven [3.1.x,)
  • Eclipse Luna 4.4
  • Java SDK [1.7.x,)
  • Google Cloud Setup

Setting up local dev environment

Install Java SDK [1.7.x,)

Install Java SDK 8

Install Eclipse Luna 4.4

Install Eclipse Luna 4.4 used in this tutorial. Other Eclipse versions may not have Google APIs plugin.

Install Google APIs Plugins

Install Maven

Install the latest Maven

Set up Google Cloud PubSub

Follow the instructions on Google Cloud Pubsub page to create

  • Google Account
  • Google Cloud project
  • Enable APIs

Create a PubSub Topic and Subscription

  • Go to Google Developer Console
  • Go to your project created previously
  • Select Pub/Sub
  • Create new topic
  • Create new subscription

Set up Google application default credentials

The simplest way to get a credential for this purpose is to create a service account using the Google Developers Console in the section APIs & Auth, in the sub-section Credentials. Create a service account or choose an existing one and select Generate new JSON key. Set the environment variable to the path of the JSON file downloaded. - Google Application Default Credentials

echo "export GOOGLE_APPLICATION_CREDENTIALS=/path/to/downloaded/authentication/file.json" > ~/.bash_profile
source ~/.bash_profile

Create a PubSub Client

Download a Google PubSub Client sample

  • Download my Google Cloud PubSub sample
  • Prepare a sample log which is used to stream to the Pub/Sub topic
  • Update variable 'file' in com.machinezone.gcloud.pubsub.client.sandbox.MZPublisher.java
  • Update the topic name in com.machinezone.gcloud.pubsub.client.sandbox.MZPublisher.java
  • Update the subscription name in com.machinezone.gcloud.pubsub.client.sandbox.MZSubscriber.java

Run in command-line

cd /your/downloaded/sample/code/dir
mvn clean install
# start the subscriber
java -cp target/pubsub-sandbox-0.0.1-SNAPSHOT.jar com.machinezone.gcloud.pubsub.client.sandbox.MZSubscriber
# start sending messages
java -cp target/pubsub-sandbox-0.0.1-SNAPSHOT.jar com.machinezone.gcloud.pubsub.client.sandbox.MZPublisher

Run in Eclipse

  • Import as an Eclipse project
  • Right click on the project -> Google -> Add Google APIs...
  • Add Google OAuth API
  • Add Google Cloud Pub/Sub API
  • Prepare a log file (file path is in com.machinezone.gcloud.pubsub.client.sandbox.MZPublisher)
  • Start the Publisher by right click on MZPublisher.java -> Run As... -> Java Application
  • Start the Subscriber by right click on MZSubscriber.java -> Run As... -> Java Application

About

Google Cloud PubSub Sandbox

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages