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
b.c.q.s.c.HelloPubSubController : received a POST at /hello/publish with message=[Hello, Pub/Sub]
b.c.q.s.publishers.PubSubPublisher : publishing to topic [hello-pubsub], message: [Hello, Pub/Sub]
b.c.q.s.s.consumers.HelloPubSubConsumer : message received: Hello, Pub/Sub
@RestController
@RequestMapping("/hello")
public class HelloPubSubController {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloPubSubController.class);
private final HelloPubSubPublisher publisher;
@Autowired
public HelloPubSubController(HelloPubSubPublisher publisher) {
@Component
public class HelloPubSubPublisher extends PubSubPublisher {
@Autowired
public HelloPubSubPublisher(PubSubTemplate pubSubTemplate) {
super(pubSubTemplate);
}
@Override
protected String topic() {
public abstract class PubSubPublisher {
private static final Logger LOGGER = LoggerFactory.getLogger(PubSubPublisher.class);
private final PubSubTemplate pubSubTemplate;
protected PubSubPublisher(PubSubTemplate pubSubTemplate) {
this.pubSubTemplate = pubSubTemplate;
}
# 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"
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
@Configuration
public class HelloPubSubSubscriberConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloPubSubSubscriberConfig.class);
private final PubSubTemplate pubSubTemplate;
private final HelloPubSubConsumer helloPubSubConsumer;
@Autowired
@Component
public class HelloPubSubConsumer extends PubSubConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloPubSubConsumer.class);
@Override
public String subscription() {
return "hello-pubsub-subscription";
}
public abstract class PubSubConsumer {
/**
* Name of an existing Pub/Sub subscription.
*/
public abstract String subscription();
/**
* The actual consumer logic.
*
@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);