-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPubSub.java
More file actions
198 lines (158 loc) · 8.07 KB
/
GPubSub.java
File metadata and controls
198 lines (158 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.grpc.InstantiatingChannelProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.pubsub.spi.v1.*;
import com.google.pubsub.v1.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
public class GPubSub {
private static final String FORMAT_OF_TOPIC_NAME = "projects/%s/topics/%s";
private static final String FORMAT_OF_SUBSCRIPTION_NAME = "projects/%s/subscriptions/%s";
public static final int TOPIC_ADMIN_CLIENT = 0;
public static final int SUBSCRIPTION_ADMIN_CLIENT = 1;
public static final int ACK_DEADLINE_SECONDS = 50;
private String projectID;
private ProjectName projectName;
private GoogleCredentials googleCredentials;
private CredentialsProvider credentialsProviderForTopic;
private InstantiatingChannelProvider channelProviderForTopic;
private TopicAdminSettings topicAdminSettings;
private TopicAdminClient topicAdminClient;
private CredentialsProvider credentialsProviderForSub;
private InstantiatingChannelProvider channelProviderForSub;
private SubscriptionAdminSettings subscriptionAdminSettings;
private SubscriptionAdminClient subscriptionAdminClient;
public GPubSub(String projectID, String credentialFile) throws IOException {
this.projectID = projectID;
this.projectName = ProjectName.create(projectID);
this.googleCredentials = ServiceAccountCredentials.fromStream(new FileInputStream(credentialFile));
this.initializeTopicAdminClient(googleCredentials);
this.initializeSubscriptionAdminClient(googleCredentials);
}
private void initializeTopicAdminClient(GoogleCredentials googleCredentials) throws IOException{
this.credentialsProviderForTopic = createCredentialsProvider(googleCredentials, TOPIC_ADMIN_CLIENT);
this.channelProviderForTopic = createChannelProvider(credentialsProviderForTopic, TOPIC_ADMIN_CLIENT);
this.topicAdminSettings = createTopicAdminSettings(channelProviderForTopic);
this.topicAdminClient = TopicAdminClient.create(topicAdminSettings);
}
private void initializeSubscriptionAdminClient(GoogleCredentials googleCredentials) throws IOException{
this.credentialsProviderForSub = createCredentialsProvider(googleCredentials, SUBSCRIPTION_ADMIN_CLIENT);
this.channelProviderForSub = createChannelProvider(credentialsProviderForSub, SUBSCRIPTION_ADMIN_CLIENT);
this.subscriptionAdminSettings = createSubscriptionAdminSettings(channelProviderForSub);
this.subscriptionAdminClient = SubscriptionAdminClient.create(subscriptionAdminSettings);
}
public TopicAdminClient getTopicAdminClient() {
return this.topicAdminClient;
}
public SubscriptionAdminClient getSubscriptionAdminClient() {
return this.subscriptionAdminClient;
}
public Subscription createSubscriptionIfNotExist(String subscriptionName, TopicName topicName) {
String fullSubName = String.format(FORMAT_OF_SUBSCRIPTION_NAME, projectID, subscriptionName);
Iterator<Subscription> iterator = subscriptionAdminClient
.listSubscriptions(projectName)
.iterateAllElements()
.iterator();
while(iterator.hasNext()) {
Subscription subscription = iterator.next();
System.out.printf("Subscription: %s\n", subscription.getName());
if(subscription.getName().equals(fullSubName)) {
return subscription;
}
}
SubscriptionName requestSubscriptionName = SubscriptionName.create(projectID, subscriptionName);
return this.subscriptionAdminClient.createSubscription(requestSubscriptionName, topicName, PushConfig.getDefaultInstance(), ACK_DEADLINE_SECONDS);
}
public Topic createTopicIfNotExist(String topicName) {
String fullTopicName = String.format(FORMAT_OF_TOPIC_NAME, projectID, topicName);
Iterator<Topic> iterator = topicAdminClient
.listTopics(projectName)
.iterateAllElements()
.iterator();
while (iterator.hasNext()) {
Topic topic = iterator.next();
System.out.printf("Topic: %s\n", topic.getName());
if (topic.getName().equals(fullTopicName)) {
System.out.printf("%s exists\n", topicName);
return topic;
}
}
TopicName requestTopicName = TopicName.create(this.projectID, topicName);
return this.topicAdminClient.createTopic(requestTopicName);
}
public Publisher createPublisher(Topic topic) throws IOException {
Publisher publisher = Publisher
.defaultBuilder(topic.getNameAsTopicName())
.setChannelProvider(this.channelProviderForTopic)
.build();
return publisher;
}
public Publisher createPublisher(String topicName) throws IOException {
Topic topic = this.createTopicIfNotExist(topicName);
Publisher publisher = Publisher
.defaultBuilder(topic.getNameAsTopicName())
.setChannelProvider(this.channelProviderForTopic)
.build();
return publisher;
}
private CredentialsProvider createCredentialsProvider(GoogleCredentials googleCredentials, int clientType) {
CredentialsProvider credentialsProvider = null;
switch (clientType) {
case TOPIC_ADMIN_CLIENT:
credentialsProvider = new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
return googleCredentials.createScoped(TopicAdminSettings.getDefaultServiceScopes());
}
};
break;
case SUBSCRIPTION_ADMIN_CLIENT:
credentialsProvider = new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
return googleCredentials.createScoped(SubscriptionAdminSettings.getDefaultServiceScopes());
}
};
break;
}
return credentialsProvider;
}
//http://googlecloudplatform.github.io/google-cloud-java/0.12.0/apidocs/
private InstantiatingChannelProvider createChannelProvider(CredentialsProvider credentialsProvider, int clientType) {
InstantiatingChannelProvider channelProvider = null;
switch (clientType) {
case TOPIC_ADMIN_CLIENT:
channelProvider = TopicAdminSettings
.defaultChannelProviderBuilder()
.setCredentialsProvider(credentialsProvider)
// .setEndpoint("localhost:8042")
// .setCredentialsProvider(FixedCredentialsProvider.create(NoCredentials.getInstance()))
.build();
break;
case SUBSCRIPTION_ADMIN_CLIENT:
channelProvider = SubscriptionAdminSettings
.defaultChannelProviderBuilder()
.setCredentialsProvider(credentialsProvider)
.build();
break;
}
return channelProvider;
}
private TopicAdminSettings createTopicAdminSettings(InstantiatingChannelProvider channelProvider) throws IOException {
TopicAdminSettings topicAdminSettings = TopicAdminSettings
.defaultBuilder()
.setChannelProvider(channelProvider)
.build();
return topicAdminSettings;
}
private SubscriptionAdminSettings createSubscriptionAdminSettings(InstantiatingChannelProvider channelProvider) throws IOException {
SubscriptionAdminSettings subscriptionAdminSettings = SubscriptionAdminSettings
.defaultBuilder()
.setChannelProvider(channelProvider)
.build();
return subscriptionAdminSettings;
}
}