forked from hashgraph/hedera-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsensusPubSubExample.java
76 lines (60 loc) · 3 KB
/
ConsensusPubSubExample.java
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
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.PrecheckStatusException;
import com.hedera.hashgraph.sdk.ReceiptStatusException;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.TopicCreateTransaction;
import com.hedera.hashgraph.sdk.TopicId;
import com.hedera.hashgraph.sdk.TopicMessageQuery;
import com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction;
import com.hedera.hashgraph.sdk.TransactionReceipt;
import com.hedera.hashgraph.sdk.TransactionResponse;
import io.github.cdimascio.dotenv.Dotenv;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
class ConsensusPubSubExample {
private static final AccountId OPERATOR_ID = AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
private static final PrivateKey OPERATOR_KEY = PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK");
private static final String CONFIG_FILE = Dotenv.load().get("CONFIG_FILE");
private ConsensusPubSubExample() {
}
public static void main(String[] args) throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException {
Client client;
if (HEDERA_NETWORK != null && HEDERA_NETWORK.equals("previewnet")) {
client = Client.forPreviewnet();
} else {
try {
client = Client.fromConfigFile(CONFIG_FILE != null ? CONFIG_FILE : "");
} catch (Exception e) {
client = Client.forTestnet();
}
}
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
TransactionResponse transactionResponse = new TopicCreateTransaction()
.execute(client);
TransactionReceipt transactionReceipt = transactionResponse.getReceipt(client);
TopicId topicId = Objects.requireNonNull(transactionReceipt.topicId);
System.out.println("New topic created: " + topicId);
Thread.sleep(5000);
new TopicMessageQuery()
.setTopicId(topicId)
.subscribe(client, resp -> {
String messageAsString = new String(resp.contents, StandardCharsets.UTF_8);
System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);
});
// keep the main thread from exiting because the listeners run on daemon threads
// noinspection InfiniteLoopStatement
for (int i = 0; ; i++) {
new TopicMessageSubmitTransaction()
.setTopicId(topicId)
.setMessage("hello, HCS! " + i)
.execute(client)
.getReceipt(client);
Thread.sleep(2500);
}
}
}