Skip to content

Commit 9606ce3

Browse files
authored
chore: update common templates, start generating README (#157)
* chore: update common templates, start generating README * chore: fix link * chore: fix libraries-bom install section
1 parent 77884f8 commit 9606ce3

File tree

15 files changed

+593
-84
lines changed

15 files changed

+593
-84
lines changed

.kokoro/common.sh

+17-2
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,28 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
# set -eo pipefail
17-
1816
function retry_with_backoff {
1917
attempts_left=$1
2018
sleep_seconds=$2
2119
shift 2
2220
command=$@
2321

22+
23+
# store current flag state
24+
flags=$-
25+
26+
# allow a failures to continue
27+
set +e
2428
echo "${command}"
2529
${command}
2630
exit_code=$?
2731

32+
# restore "e" flag
33+
if [[ ${flags} =~ e ]]
34+
then set -e
35+
else set +e
36+
fi
37+
2838
if [[ $exit_code == 0 ]]
2939
then
3040
return 0
@@ -42,3 +52,8 @@ function retry_with_backoff {
4252

4353
return $exit_code
4454
}
55+
56+
## Helper functionss
57+
function now() { date +"%Y-%m-%d %H:%M:%S" | tr -d '\n'; }
58+
function msg() { println "$*" >&2; }
59+
function println() { printf '%s\n' "$(now) $*"; }

.kokoro/dependencies.sh

+48
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,51 @@ retry_with_backoff 3 10 \
3636
-Dclirr.skip=true
3737

3838
mvn -B dependency:analyze -DfailOnWarning=true
39+
40+
echo "****************** DEPENDENCY LIST COMPLETENESS CHECK *******************"
41+
## Run dependency list completeness check
42+
function completenessCheck() {
43+
# Output dep list with compile scope generated using the original pom
44+
msg "Generating dependency list using original pom..."
45+
mvn dependency:list -f pom.xml -Dsort=true | grep '\[INFO] .*:.*:.*:.*:.*' | grep -v ':test$' >.org-list.txt
46+
47+
# Output dep list generated using the flattened pom (test scope deps are ommitted)
48+
msg "Generating dependency list using flattened pom..."
49+
mvn dependency:list -f .flattened-pom.xml -Dsort=true | grep '\[INFO] .*:.*:.*:.*:.*' >.new-list.txt
50+
51+
# Compare two dependency lists
52+
msg "Comparing dependency lists..."
53+
diff .org-list.txt .new-list.txt >.diff.txt
54+
if [[ $? == 0 ]]
55+
then
56+
msg "Success. No diff!"
57+
else
58+
msg "Diff found. See below: "
59+
msg "You can also check .diff.txt file located in $1."
60+
cat .diff.txt
61+
return 1
62+
fi
63+
}
64+
65+
# Allow failures to continue running the script
66+
set +e
67+
68+
error_count=0
69+
for path in $(find -name ".flattened-pom.xml")
70+
do
71+
# Check flattened pom in each dir that contains it for completeness
72+
dir=$(dirname "$path")
73+
pushd "$dir"
74+
completenessCheck "$dir"
75+
error_count=$(($error_count + $?))
76+
popd
77+
done
78+
79+
if [[ $error_count == 0 ]]
80+
then
81+
msg "All checks passed."
82+
exit 0
83+
else
84+
msg "Errors found. See log statements above."
85+
exit 1
86+
fi

.kokoro/nightly/integration.cfg

+20
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,31 @@ env_vars: {
66
value: "gcr.io/cloud-devrel-kokoro-resources/java8"
77
}
88

9+
env_vars: {
10+
key: "JOB_TYPE"
11+
value: "integration"
12+
}
13+
# TODO: remove this after we've migrated all tests and scripts
14+
env_vars: {
15+
key: "GCLOUD_PROJECT"
16+
value: "gcloud-devel"
17+
}
18+
19+
env_vars: {
20+
key: "GOOGLE_CLOUD_PROJECT"
21+
value: "gcloud-devel"
22+
}
23+
924
env_vars: {
1025
key: "ENABLE_BUILD_COP"
1126
value: "true"
1227
}
1328

29+
env_vars: {
30+
key: "GOOGLE_APPLICATION_CREDENTIALS"
31+
value: "keystore/73713_java_it_service_account"
32+
}
33+
1434
before_action {
1535
fetch_keystore {
1636
keystore_resource {

.kokoro/nightly/samples.cfg

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ env_vars: {
1111
value: "samples"
1212
}
1313

14+
# TODO: remove this after we've migrated all tests and scripts
1415
env_vars: {
1516
key: "GCLOUD_PROJECT"
16-
value: "gcloud-devel"
17+
value: "java-docs-samples-testing"
18+
}
19+
20+
env_vars: {
21+
key: "GOOGLE_CLOUD_PROJECT"
22+
value: "java-docs-samples-testing"
1723
}
1824

1925
env_vars: {

.kokoro/presubmit/integration.cfg

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ env_vars: {
1111
value: "integration"
1212
}
1313

14+
# TODO: remove this after we've migrated all tests and scripts
1415
env_vars: {
15-
key: "GCLOUD_PROJECT"
16-
value: "gcloud-devel"
16+
key: "GCLOUD_PROJECT"
17+
value: "gcloud-devel"
1718
}
1819

1920
env_vars: {
20-
key: "GOOGLE_APPLICATION_CREDENTIALS"
21-
value: "keystore/73713_java_it_service_account"
21+
key: "GOOGLE_CLOUD_PROJECT"
22+
value: "gcloud-devel"
23+
}
24+
25+
env_vars: {
26+
key: "GOOGLE_APPLICATION_CREDENTIALS"
27+
value: "keystore/73713_java_it_service_account"
2228
}
2329

2430
before_action {

.kokoro/presubmit/samples.cfg

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ env_vars: {
1111
value: "samples"
1212
}
1313

14+
# TODO: remove this after we've migrated all tests and scripts
1415
env_vars: {
15-
key: "GCLOUD_PROJECT"
16-
value: "gcloud-devel"
16+
key: "GCLOUD_PROJECT"
17+
value: "java-docs-samples-testing"
1718
}
1819

1920
env_vars: {
20-
key: "GOOGLE_APPLICATION_CREDENTIALS"
21-
value: "keystore/73713_java_it_service_account"
21+
key: "GOOGLE_CLOUD_PROJECT"
22+
value: "java-docs-samples-testing"
23+
}
24+
25+
env_vars: {
26+
key: "GOOGLE_APPLICATION_CREDENTIALS"
27+
value: "keystore/73713_java_it_service_account"
2228
}
2329

2430
before_action {

.readme-partials.yaml

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
custom_content: |
2+
#### Creating a topic
3+
4+
With Pub/Sub you can create topics. A topic is a named resource to which messages are sent by
5+
publishers. Add the following imports at the top of your file:
6+
7+
```java
8+
import com.google.cloud.pubsub.v1.TopicAdminClient;
9+
import com.google.pubsub.v1.ProjectTopicName;
10+
```
11+
Then, to create the topic, use the following code:
12+
13+
```java
14+
ProjectTopicName topic = ProjectTopicName.of("test-project", "test-topic");
15+
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
16+
topicAdminClient.createTopic(topic);
17+
}
18+
```
19+
20+
#### Publishing messages
21+
22+
With Pub/Sub you can publish messages to a topic. Add the following import at the top of your file:
23+
24+
```java
25+
import com.google.api.core.ApiFuture;
26+
import com.google.api.core.ApiFutureCallback;
27+
import com.google.api.core.ApiFutures;
28+
import com.google.cloud.pubsub.v1.Publisher;
29+
import com.google.common.util.concurrent.MoreExecutors;
30+
import com.google.protobuf.ByteString;
31+
import com.google.pubsub.v1.PubsubMessage;
32+
```
33+
Then, to publish messages asynchronously, use the following code:
34+
35+
```java
36+
Publisher publisher = null;
37+
try {
38+
publisher = Publisher.newBuilder(topic).build();
39+
ByteString data = ByteString.copyFromUtf8("my-message");
40+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
41+
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
42+
ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() {
43+
public void onSuccess(String messageId) {
44+
System.out.println("published with message id: " + messageId);
45+
}
46+
47+
public void onFailure(Throwable t) {
48+
System.out.println("failed to publish: " + t);
49+
}
50+
}, MoreExecutors.directExecutor());
51+
//...
52+
} finally {
53+
if (publisher != null) {
54+
publisher.shutdown();
55+
publisher.awaitTermination(1, TimeUnit.MINUTES);
56+
}
57+
}
58+
```
59+
60+
#### Creating a subscription
61+
62+
With Pub/Sub you can create subscriptions. A subscription represents the stream of messages from a
63+
single, specific topic. Add the following imports at the top of your file:
64+
65+
```java
66+
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
67+
import com.google.pubsub.v1.PushConfig;
68+
import com.google.pubsub.v1.ProjectSubscriptionName;
69+
import com.google.pubsub.v1.ProjectTopicName;
70+
```
71+
Then, to create the subscription, use the following code:
72+
73+
```java
74+
ProjectTopicName topic = ProjectTopicName.of("test-project", "test-topic");
75+
ProjectSubscriptionName subscription = ProjectSubscriptionName.of("test-project", "test-subscription");
76+
77+
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
78+
subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
79+
}
80+
```
81+
82+
#### Pulling messages
83+
84+
With Pub/Sub you can pull messages from a subscription. Add the following imports at the top of your
85+
file:
86+
87+
```java
88+
import com.google.cloud.pubsub.v1.AckReplyConsumer;
89+
import com.google.cloud.pubsub.v1.MessageReceiver;
90+
import com.google.cloud.pubsub.v1.Subscriber;
91+
import com.google.common.util.concurrent.MoreExecutors;
92+
import com.google.pubsub.v1.PubsubMessage;
93+
import com.google.pubsub.v1.ProjectSubscriptionName;
94+
import com.google.pubsub.v1.ProjectTopicName;
95+
```
96+
Then, to pull messages asynchronously, use the following code:
97+
98+
```java
99+
ProjectSubscriptionName subscription = ProjectSubscriptionName.of("test-project", "test-subscription");
100+
101+
MessageReceiver receiver =
102+
new MessageReceiver() {
103+
@Override
104+
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
105+
System.out.println("got message: " + message.getData().toStringUtf8());
106+
consumer.ack();
107+
}
108+
};
109+
110+
Subscriber subscriber = null;
111+
try {
112+
subscriber = Subscriber.newBuilder(subscription, receiver).build();
113+
subscriber.addListener(
114+
new Subscriber.Listener() {
115+
@Override
116+
public void failed(Subscriber.State from, Throwable failure) {
117+
// Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
118+
System.err.println(failure);
119+
}
120+
},
121+
MoreExecutors.directExecutor());
122+
subscriber.startAsync().awaitRunning();
123+
//...
124+
} finally {
125+
if (subscriber != null) {
126+
subscriber.stopAsync();
127+
}
128+
}
129+
```
130+
#### Complete source code
131+
132+
In [CreateTopicAndPublishMessages.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java) and [CreateSubscriptionAndConsumeMessages.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateSubscriptionAndConsumeMessages.java) we put together all the code shown above into two programs. The programs assume that you are running on Compute Engine, App Engine Flexible or from your own desktop.

.repo-metadata.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "pubsub",
3-
"name_pretty": "Google Cloud Pub/Sub",
3+
"name_pretty": "Cloud Pub/Sub",
44
"api_reference": "https://cloud.google.com/pubsub/",
55
"product_documentation": "https://cloud.google.com/pubsub/docs/",
6-
"client_documentation": "https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/pubsub/v1/package-summary.html",
6+
"client_documentation": "https://googleapis.dev/java/google-cloud-pubusb/latest/index.html",
7+
"api_description": "is designed to provide reliable, many-to-many, asynchronous messaging between applications. Publisher applications can send messages to a topic and other applications can subscribe to that topic to receive the messages. By decoupling senders and receivers, Google Cloud Pub/Sub allows developers to communicate between independently written applications.",
78
"issue_tracker": "https://issuetracker.google.com/savedsearches/559741",
89
"release_level": "ga",
910
"language": "java",
1011
"repo": "googleapis/java-pubsub",
1112
"repo_short": "java-pubsub",
1213
"distribution_name": "com.google.cloud:google-cloud-pubsub",
13-
"api_id": "pubsub.googleapis.com"
14+
"api_id": "pubsub.googleapis.com",
15+
"requires_billing": true
1416
}

0 commit comments

Comments
 (0)