Skip to content

Commit 2a1cc20

Browse files
Merge pull request #304078 from fanruisun/changeOrderSendmail
change order of aync and sync
2 parents d44e8c0 + ff3e446 commit 2a1cc20

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

articles/communication-services/quickstarts/email/includes/send-email-java.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,48 @@ Make these replacements in the code:
210210

211211
To send the email message, call the `beginSend` function from the `EmailClient`.
212212

213+
## [Async Client](#tab/async-client)
214+
215+
Calling `beginSend` on the async client returns a `PollerFlux` object to which you can subscribe. The callbacks defined in the subscribe method are triggered once the email sending operation is complete. **Note that the initial request to send an email will not be sent until a subscriber is set up.**
216+
217+
```java
218+
Duration MAIN_THREAD_WAIT_TIME = Duration.ofSeconds(30);
219+
220+
// ExecutorService to run the polling in a separate thread
221+
ExecutorService executorService = Executors.newSingleThreadExecutor();
222+
223+
PollerFlux<EmailSendResult, EmailSendResult> poller = emailAsyncClient.beginSend(emailMessage);
224+
225+
executorService.submit(() -> {
226+
// The initial request is sent out as soon as we subscribe the to PollerFlux object
227+
poller.subscribe(
228+
response -> {
229+
if (response.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
230+
System.out.printf("Successfully sent the email (operation id: %s)\n", response.getValue().getId());
231+
}
232+
else {
233+
// The operation ID can be retrieved as soon as the first response is received from the PollerFlux.
234+
System.out.println("Email send status: " + response.getStatus() + ", operation id: " + response.getValue().getId());
235+
}
236+
},
237+
error -> {
238+
System.out.println("Error occurred while sending email: " + error.getMessage());
239+
}
240+
);
241+
});
242+
243+
// In a real application, you might have a mechanism to keep the main thread alive.
244+
// For this sample we will keep the main thread alive for 30 seconds to make sure the child thread has time to receive the SUCCESSFULLY_COMPLETED status.
245+
try {
246+
Thread.sleep(MAIN_THREAD_WAIT_TIME.toMillis());
247+
} catch (InterruptedException e) {
248+
e.printStackTrace();
249+
}
250+
251+
executorService.shutdown();
252+
System.out.println("Main thread ends.");
253+
```
254+
213255
## [Sync Client](#tab/sync-client)
214256

215257
Calling `beginSend` on the sync client returns a `SyncPoller` object, which can be used to check on the status of the operation and retrieve the result once it finishes. The initial request to send an email starts as soon as the `beginSend` method is called. **Sending an email is a long running operation. Its important to note that the `getFinalResult()` method on the poller is a blocking operation until a terminal state (`SUCCESSFULLY_COMPLETED` or `FAILED`) is reached.** We recommend that you do manual polling at an interval that's appropriate for your application needs as demonstrated in the following sample.
@@ -257,48 +299,6 @@ catch (Exception exception)
257299
}
258300
```
259301

260-
## [Async Client](#tab/async-client)
261-
262-
Calling `beginSend` on the async client returns a `PollerFlux` object to which you can subscribe. The callbacks defined in the subscribe method are triggered once the email sending operation is complete. **Note that the initial request to send an email will not be sent until a subscriber is set up.**
263-
264-
```java
265-
Duration MAIN_THREAD_WAIT_TIME = Duration.ofSeconds(30);
266-
267-
// ExecutorService to run the polling in a separate thread
268-
ExecutorService executorService = Executors.newSingleThreadExecutor();
269-
270-
PollerFlux<EmailSendResult, EmailSendResult> poller = emailAsyncClient.beginSend(emailMessage);
271-
272-
executorService.submit(() -> {
273-
// The initial request is sent out as soon as we subscribe the to PollerFlux object
274-
poller.subscribe(
275-
response -> {
276-
if (response.getStatus() == LongRunningOperationStatus.SUCCESSFULLY_COMPLETED) {
277-
System.out.printf("Successfully sent the email (operation id: %s)\n", response.getValue().getId());
278-
}
279-
else {
280-
// The operation ID can be retrieved as soon as the first response is received from the PollerFlux.
281-
System.out.println("Email send status: " + response.getStatus() + ", operation id: " + response.getValue().getId());
282-
}
283-
},
284-
error -> {
285-
System.out.println("Error occurred while sending email: " + error.getMessage());
286-
}
287-
);
288-
});
289-
290-
// In a real application, you might have a mechanism to keep the main thread alive.
291-
// For this sample we will keep the main thread alive for 30 seconds to make sure the child thread has time to receive the SUCCESSFULLY_COMPLETED status.
292-
try {
293-
Thread.sleep(MAIN_THREAD_WAIT_TIME.toMillis());
294-
} catch (InterruptedException e) {
295-
e.printStackTrace();
296-
}
297-
298-
executorService.shutdown();
299-
System.out.println("Main thread ends.");
300-
```
301-
302302
---
303303

304304
### Run the code

0 commit comments

Comments
 (0)