- Maven [3.1.x,)
- Eclipse Luna 4.4
- Java SDK [1.7.x,)
- Google Cloud Setup
Install Java SDK 8
Install Eclipse Luna 4.4 used in this tutorial. Other Eclipse versions may not have Google APIs plugin.
- Open Eclipse
- Help -> Install New Software...
- Add 'https://dl.google.com/eclipse/plugin/4.4'
- Click Next -> Next -> Finish
- Restart Eclipse
Install the latest Maven
Follow the instructions on Google Cloud Pubsub page to create
- Google Account
- Google Cloud project
- Enable APIs
- Go to Google Developer Console
- Go to your project created previously
- Select Pub/Sub
- Create new topic
- Create new subscription
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
- 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
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
- 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