GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — hedera-docs/.github/examples/java/CreateTopicDemo.java at main · hashgraph/hedera-docs

Expand file tree

/

CreateTopicDemo.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
77 lines (60 loc) · 3.08 KB
/

CreateTopicDemo.java

Copy path

File metadata and controls

77 lines (60 loc) · 3.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import com.hedera.hashgraph.sdk.*;
import java.net.http.*;
import java.net.URI;
import com.google.gson.*;
public class CreateTopicDemo {
public static void main(String[] args ) throws Exception {
// .env-provided
AccountId operatorId = AccountId.fromString(System.getenv("OPERATOR_ID"));
PrivateKey operatorKey = PrivateKey.fromStringECDSA(System.getenv("OPERATOR_KEY"));
String network = System.getenv().getOrDefault("HEDERA_NETWORK", "local");
String mirrorNode = System.getenv().getOrDefault(
"MIRROR_NODE_URL",
"http://localhost:8080/api/v1"
);
if (operatorId == null || operatorKey == null) {
throw new IllegalStateException("OPERATOR_ID / OPERATOR_KEY not set");
}
Client client =
"local".equalsIgnoreCase(network)
? Client.forNetwork(java.util.Map.of("127.0.0.1:50211", new AccountId(3)))
: Client.forTestnet();
client.setOperator(operatorId, operatorKey);
// build & execute the topic creation transaction
TopicCreateTransaction transaction = new TopicCreateTransaction()
.setTopicMemo("My first HCS topic");
TransactionResponse txResponse = transaction.execute(client);
TransactionReceipt receipt = txResponse.getReceipt(client);
TopicId topicId = receipt.topicId;
System.out.println("\nTopic created: " + topicId);
// build & execute the message submission transaction
String message = "Hello, Hedera!";
TopicMessageSubmitTransaction messageTransaction = new TopicMessageSubmitTransaction()
.setTopicId(topicId)
.setMessage(message);
messageTransaction.execute(client);
System.out.println("\nMessage submitted: " + message);
// wait for Mirror Node to populate data
System.out.println("\nWaiting for Mirror Node to update...");
Thread.sleep(10000);
// query messages using Mirror Node
String mirrorNodeUrl = mirrorNode + "/topics/" + topicId + "/messages";
HttpClient httpClient = HttpClient.newHttpClient( );
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(mirrorNodeUrl))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString( ));
Gson gson = new Gson();
JsonObject data = gson.fromJson(response.body(), JsonObject.class);
if (data.has("messages") && data.getAsJsonArray("messages").size() > 0) {
JsonArray messages = data.getAsJsonArray("messages");
JsonObject latestMessage = messages.get(messages.size() - 1).getAsJsonObject();
String encodedMessage = latestMessage.get("message").getAsString();
String messageContent = new String(java.util.Base64.getDecoder().decode(encodedMessage)).trim();
System.out.println("\nLatest message: " + messageContent + "\n");
} else {
System.out.println("No messages found yet in Mirror Node");
}
client.close();
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.