26
26
import com .azure .resourcemanager .eventgrid .models .EventSubscription ;
27
27
import com .azure .resourcemanager .eventgrid .models .EventSubscriptionFilter ;
28
28
import com .azure .resourcemanager .eventgrid .models .Topic ;
29
- import com .azure .resourcemanager .eventhubs .EventHubsManager ;
30
29
import com .azure .resourcemanager .eventhubs .models .EventHub ;
31
30
import com .azure .resourcemanager .eventhubs .models .EventHubNamespace ;
32
31
import com .azure .resourcemanager .eventhubs .models .EventHubNamespaceSkuType ;
33
32
import com .azure .resourcemanager .resources .models .ResourceGroup ;
34
- import com .azure .resourcemanager .resources .models .Subscription ;
35
33
import reactor .core .Disposable ;
36
34
import reactor .core .publisher .Flux ;
37
35
@@ -48,13 +46,13 @@ public class EventGridPublishAndConsumeExample {
48
46
49
47
private static final Random RANDOM = new Random ();
50
48
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 ();
58
56
59
57
/**
60
58
* Main entry point.
@@ -70,7 +68,7 @@ public static void main(String[] args) {
70
68
.build ();
71
69
72
70
AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
73
-
71
+
74
72
// 2. Create ResourceManager, EventGridManager
75
73
76
74
// Create one HttpClient to make it shared by two resource managers.
@@ -100,16 +98,16 @@ private static void runSample() {
100
98
101
99
// 1. Create a resource group.
102
100
ResourceGroup resourceGroup =
103
- resourceManager .resourceGroups ().define (resourceGroupName ).withRegion (region ).create ();
101
+ resourceManager .resourceGroups ().define (RESOURCE_GROUP_NAME ).withRegion (REGION ).create ();
104
102
105
- System .out .println ("Resource group created with name " + resourceGroupName );
103
+ System .out .println ("Resource group created with name " + RESOURCE_GROUP_NAME );
106
104
107
105
// 2. Create an event hub.
108
106
// 2.1 Create a event hub namespace.
109
107
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 )
113
111
.withAutoScaling ()
114
112
.withSku (EventHubNamespaceSkuType .STANDARD )
115
113
.create ();
@@ -118,26 +116,26 @@ private static void runSample() {
118
116
119
117
// 2.2 Create event hub.
120
118
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 )
124
122
.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
125
123
.create ();
126
124
127
125
System .out .println ("EventHub created with name " + eventHub .name ());
128
126
129
127
// 3. Create an event grid topic.
130
128
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 )
134
132
.create ();
135
133
136
134
System .out .println ("EventGrid topic created with name " + eventGridTopic .name ());
137
135
138
136
// 4. Create an event grid subscription.
139
137
EventSubscription eventSubscription = eventGridManager .eventSubscriptions ()
140
- .define (eventSubscriptionName )
138
+ .define (EVENT_SUBSCRIPTION_NAME )
141
139
.withExistingScope (eventGridTopic .id ())
142
140
.withDestination (new EventHubEventSubscriptionDestination ()
143
141
.withResourceId (eventHub .id ()))
@@ -150,7 +148,7 @@ private static void runSample() {
150
148
System .out .println ("EventGrid event subscription created with name " + eventSubscription .name ());
151
149
152
150
// 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 ();
154
152
155
153
System .out .format ("Found EventGrid client connection key \" %s\" for endpoint \" %s\" \n " , eventGridClientConnectionKey , eventGridTopic .endpoint ());
156
154
@@ -188,23 +186,21 @@ private static void runSample() {
188
186
189
187
Disposable subscription = consumer .receiveFromPartition (firstPartition , EventPosition .latest ())
190
188
.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 ();
194
192
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 );
200
196
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
+ });
208
204
209
205
// 9. Publish custom events to the EventGrid.
210
206
// We create events to send to the service and block until the send has completed.
@@ -231,7 +227,7 @@ private static void runSample() {
231
227
e .printStackTrace ();
232
228
} finally {
233
229
// 10. clean up the resources created above
234
- resourceManager .resourceGroups ().beginDeleteByName (resourceGroupName );
230
+ resourceManager .resourceGroups ().beginDeleteByName (RESOURCE_GROUP_NAME );
235
231
}
236
232
}
237
233
0 commit comments