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