Skip to content

Commit 064b7dd

Browse files
Move sample folder under src folder (#24031)
* Change sample folder under src/ * Update checkstyle issues * Update checkstyle issues
1 parent 7497585 commit 064b7dd

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
import com.azure.resourcemanager.eventgrid.models.EventSubscription;
2727
import com.azure.resourcemanager.eventgrid.models.EventSubscriptionFilter;
2828
import com.azure.resourcemanager.eventgrid.models.Topic;
29-
import com.azure.resourcemanager.eventhubs.EventHubsManager;
3029
import com.azure.resourcemanager.eventhubs.models.EventHub;
3130
import com.azure.resourcemanager.eventhubs.models.EventHubNamespace;
3231
import com.azure.resourcemanager.eventhubs.models.EventHubNamespaceSkuType;
3332
import com.azure.resourcemanager.resources.models.ResourceGroup;
34-
import com.azure.resourcemanager.resources.models.Subscription;
3533
import reactor.core.Disposable;
3634
import reactor.core.publisher.Flux;
3735

@@ -48,13 +46,13 @@ public class EventGridPublishAndConsumeExample {
4846

4947
private static final Random RANDOM = new Random();
5048
private static final int NUMBER_OF_EVENTS = 10;
51-
private static final Region region = Region.US_WEST2;
52-
private static final String resourceGroupName = "rg" + randomPadding();
53-
private static final String eventHubName = "eh" + randomPadding();
54-
private static final String eventHubNamespace = "eh-namespace" + randomPadding();
55-
private static final String topicName = "my-topic-name" + randomPadding();
56-
private static final String eventSubscriptionName = "event-subscription" + randomPadding();
57-
private static final String eventHubRuleName = "my-management-rule" + randomPadding();
49+
private static final Region REGION = Region.US_WEST2;
50+
private static final String RESOURCE_GROUP_NAME = "rg" + randomPadding();
51+
private static final String EVENT_HUB_NAME = "eh" + randomPadding();
52+
private static final String EVENT_HUB_NAMESPACE = "ehNamespace" + randomPadding();
53+
private static final String TOPIC_NAME = "myTopicName" + randomPadding();
54+
private static final String EVENT_SUBSCRIPTION_NAME = "eventSubscription" + randomPadding();
55+
private static final String EVENT_HUB_RULE_NAME = "myManagementRule" + randomPadding();
5856

5957
/**
6058
* Main entry point.
@@ -70,7 +68,7 @@ public static void main(String[] args) {
7068
.build();
7169

7270
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
73-
71+
7472
// 2. Create ResourceManager, EventGridManager
7573

7674
// Create one HttpClient to make it shared by two resource managers.
@@ -100,16 +98,16 @@ private static void runSample() {
10098

10199
// 1. Create a resource group.
102100
ResourceGroup resourceGroup =
103-
resourceManager.resourceGroups().define(resourceGroupName).withRegion(region).create();
101+
resourceManager.resourceGroups().define(RESOURCE_GROUP_NAME).withRegion(REGION).create();
104102

105-
System.out.println("Resource group created with name " + resourceGroupName);
103+
System.out.println("Resource group created with name " + RESOURCE_GROUP_NAME);
106104

107105
// 2. Create an event hub.
108106
// 2.1 Create a event hub namespace.
109107
EventHubNamespace namespace = resourceManager.eventHubNamespaces()
110-
.define(eventHubNamespace)
111-
.withRegion(region)
112-
.withExistingResourceGroup(resourceGroupName)
108+
.define(EVENT_HUB_NAMESPACE)
109+
.withRegion(REGION)
110+
.withExistingResourceGroup(RESOURCE_GROUP_NAME)
113111
.withAutoScaling()
114112
.withSku(EventHubNamespaceSkuType.STANDARD)
115113
.create();
@@ -118,26 +116,26 @@ private static void runSample() {
118116

119117
// 2.2 Create event hub.
120118
EventHub eventHub = resourceManager.eventHubs()
121-
.define(eventHubName)
122-
.withExistingNamespace(resourceGroupName, eventHubNamespace)
123-
.withNewManageRule(eventHubRuleName)
119+
.define(EVENT_HUB_NAME)
120+
.withExistingNamespace(RESOURCE_GROUP_NAME, EVENT_HUB_NAMESPACE)
121+
.withNewManageRule(EVENT_HUB_RULE_NAME)
124122
.withPartitionCount(1) // Here we create eventhub with 1 partition, so that when we subscribe, we can make sure all the events come from the same partition, and then subscribe to the first partition. It is for sample purpose. In real use case, one can configure multiple partitions
125123
.create();
126124

127125
System.out.println("EventHub created with name " + eventHub.name());
128126

129127
// 3. Create an event grid topic.
130128
Topic eventGridTopic = eventGridManager.topics()
131-
.define(topicName)
132-
.withRegion(region)
133-
.withExistingResourceGroup(resourceGroupName)
129+
.define(TOPIC_NAME)
130+
.withRegion(REGION)
131+
.withExistingResourceGroup(RESOURCE_GROUP_NAME)
134132
.create();
135133

136134
System.out.println("EventGrid topic created with name " + eventGridTopic.name());
137135

138136
// 4. Create an event grid subscription.
139137
EventSubscription eventSubscription = eventGridManager.eventSubscriptions()
140-
.define(eventSubscriptionName)
138+
.define(EVENT_SUBSCRIPTION_NAME)
141139
.withExistingScope(eventGridTopic.id())
142140
.withDestination(new EventHubEventSubscriptionDestination()
143141
.withResourceId(eventHub.id()))
@@ -150,7 +148,7 @@ private static void runSample() {
150148
System.out.println("EventGrid event subscription created with name " + eventSubscription.name());
151149

152150
// 5. Retrieve the event grid client connection key.
153-
String eventGridClientConnectionKey = eventGridManager.topics().listSharedAccessKeys(resourceGroupName, topicName).key1();
151+
String eventGridClientConnectionKey = eventGridManager.topics().listSharedAccessKeys(RESOURCE_GROUP_NAME, TOPIC_NAME).key1();
154152

155153
System.out.format("Found EventGrid client connection key \"%s\" for endpoint \"%s\"\n", eventGridClientConnectionKey, eventGridTopic.endpoint());
156154

@@ -188,23 +186,21 @@ private static void runSample() {
188186

189187
Disposable subscription = consumer.receiveFromPartition(firstPartition, EventPosition.latest())
190188
.subscribe(partitionEvent -> {
191-
EventData eventData = partitionEvent.getData();
192-
String contents = new String(eventData.getBody(), UTF_8);
193-
countDownLatch.countDown();
189+
EventData eventData = partitionEvent.getData();
190+
String contents = new String(eventData.getBody(), UTF_8);
191+
countDownLatch.countDown();
194192

195-
System.out.printf("Event received. Event sequence number number: %s. Contents: %s%n",
196-
eventData.getSequenceNumber(), contents);
197-
},
198-
error -> {
199-
System.err.println("Error occurred while consuming events: " + error);
193+
System.out.printf("Event received. Event sequence number number: %s. Contents: %s%n", eventData.getSequenceNumber(), contents);
194+
}, error -> {
195+
System.err.println("Error occurred while consuming events: " + error);
200196

201-
// Count down until 0, so the main thread does not keep waiting for events.
202-
while (countDownLatch.getCount() > 0) {
203-
countDownLatch.countDown();
204-
}
205-
}, () -> {
206-
System.out.println("Finished reading events.");
207-
});
197+
// Count down until 0, so the main thread does not keep waiting for events.
198+
while (countDownLatch.getCount() > 0) {
199+
countDownLatch.countDown();
200+
}
201+
}, () -> {
202+
System.out.println("Finished reading events.");
203+
});
208204

209205
// 9. Publish custom events to the EventGrid.
210206
// We create events to send to the service and block until the send has completed.
@@ -231,7 +227,7 @@ private static void runSample() {
231227
e.printStackTrace();
232228
} finally {
233229
// 10. clean up the resources created above
234-
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName);
230+
resourceManager.resourceGroups().beginDeleteByName(RESOURCE_GROUP_NAME);
235231
}
236232
}
237233

0 commit comments

Comments
 (0)