This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# login with your google credentials | |
# you will be asked to either use an existing project or create a new one | |
# for this article, I created the project hello-spring-cloud-gcp | |
$ gcloud init | |
# enable the Pub/Sub API | |
$ gcloud services enable pubsub.googleapis.com | |
# create a new Pub/Sub topic | |
$ gcloud pubsub topics create hello-pubsub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a new service account with a descriptive name | |
# I chose 'hello-spring-cloud-gcp-app' for mine, but it can be any name you like | |
# just know that a service account name must be between 6 and 30 characters (inclusive), must begin with | |
# a lowercase letter, and consist of lowercase alphanumeric characters that can be separated by hyphens. | |
$ gcloud iam service-accounts create hello-spring-cloud-gcp-app | |
# add the appropriate roles to your service account | |
# for more info on roles, check https://cloud.google.com/iam/docs/understanding-roles | |
# for our app, we only need the Pub/Sub Publisher and Subscriber roles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Send a message to Pub/Sub. | |
*/ | |
public <T> ListenableFuture<String> publish(String topic, T payload); | |
/** | |
* Add a callback method to an existing subscription. | |
*/ | |
public Subscriber subscribe(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class PubSubConsumer { | |
/** | |
* Name of an existing Pub/Sub subscription. | |
*/ | |
public abstract String subscription(); | |
/** | |
* The actual consumer logic. | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class HelloPubSubConsumer extends PubSubConsumer { | |
private static final Logger LOGGER = LoggerFactory.getLogger(HelloPubSubConsumer.class); | |
@Override | |
public String subscription() { | |
return "hello-pubsub-subscription"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class HelloPubSubSubscriberConfig { | |
private static final Logger LOGGER = LoggerFactory.getLogger(HelloPubSubSubscriberConfig.class); | |
private final PubSubTemplate pubSubTemplate; | |
private final HelloPubSubConsumer helloPubSubConsumer; | |
@Autowired |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s.SpringCloudGcpPubSubExampleApplication : Started SpringCloudGcpPubSubExampleApplication in 2.824 seconds (JVM running for 3.432) | |
b.c.q.s.c.HelloPubSubSubscriberConfig : Subscribing HelloPubSubConsumer to hello-pubsub-subscription |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list pub/sub topics (in case you don't remeber the your topic's name) | |
$ gcloud pubsub topics list | |
# setting up topic (just for readability purposes) | |
$ PUBSUB_TOPIC=projects/hello-spring-cloud-gcp/topics/hello-pubsub | |
# publish to topic | |
$ gcloud pubsub topics publish ${PUBSUB_TOPIC}\ | |
--message "Hello, Pub/Sub" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class PubSubPublisher { | |
private static final Logger LOGGER = LoggerFactory.getLogger(PubSubPublisher.class); | |
private final PubSubTemplate pubSubTemplate; | |
protected PubSubPublisher(PubSubTemplate pubSubTemplate) { | |
this.pubSubTemplate = pubSubTemplate; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class HelloPubSubPublisher extends PubSubPublisher { | |
@Autowired | |
public HelloPubSubPublisher(PubSubTemplate pubSubTemplate) { | |
super(pubSubTemplate); | |
} | |
@Override | |
protected String topic() { |
OlderNewer