Skip to content

Instantly share code, notes, and snippets.

View fmachado091's full-sized avatar
🍓
Strawberry pies are supposed to have strawberries in them, dear.

Fernando Machado fmachado091

🍓
Strawberry pies are supposed to have strawberries in them, dear.
View GitHub Profile
@fmachado091
fmachado091 / spring-cloud-gcp-pubsub-example-setup-gcp.sh
Created December 2, 2018 23:32
How to create and setup a GCP project with Cloud Pub/Sub topics and subscriptions.
# 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
@fmachado091
fmachado091 / spring-cloud-gcp-pubsub-example-create-service-account.sh
Last active July 21, 2020 05:29
How to create a service account using Google Cloud SDK.
# 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
@fmachado091
fmachado091 / spring-cloud-gcp-pubsub-example-pubsubtemplate.java
Created December 19, 2018 00:56
PubSubTemplate methods for publishing and consuming messages.
/**
* 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);
public abstract class PubSubConsumer {
/**
* Name of an existing Pub/Sub subscription.
*/
public abstract String subscription();
/**
* The actual consumer logic.
*
@Component
public class HelloPubSubConsumer extends PubSubConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloPubSubConsumer.class);
@Override
public String subscription() {
return "hello-pubsub-subscription";
}
@Configuration
public class HelloPubSubSubscriberConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloPubSubSubscriberConfig.class);
private final PubSubTemplate pubSubTemplate;
private final HelloPubSubConsumer helloPubSubConsumer;
@Autowired
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
# 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"
public abstract class PubSubPublisher {
private static final Logger LOGGER = LoggerFactory.getLogger(PubSubPublisher.class);
private final PubSubTemplate pubSubTemplate;
protected PubSubPublisher(PubSubTemplate pubSubTemplate) {
this.pubSubTemplate = pubSubTemplate;
}
@Component
public class HelloPubSubPublisher extends PubSubPublisher {
@Autowired
public HelloPubSubPublisher(PubSubTemplate pubSubTemplate) {
super(pubSubTemplate);
}
@Override
protected String topic() {