|
36 | 36 |
|
37 | 37 | public class MutableCredentialsExample { |
38 | 38 |
|
39 | | - static void createClientWithMutableCredentials(String credentialsPath) throws IOException { |
40 | | - Path path = Paths.get(credentialsPath); |
41 | | - // Use an AtomicReference to hold the mutable lastModifiedTime so it can be accessed in the lambda |
42 | | - final AtomicReference<FileTime> lastModifiedTime = |
43 | | - new AtomicReference<>(Files.getLastModifiedTime(path)); |
| 39 | + static void createClientWithMutableCredentials(String credentialsPath) throws IOException { |
| 40 | + Path path = Paths.get(credentialsPath); |
| 41 | + // Use an AtomicReference to hold the mutable lastModifiedTime so it can be accessed in the |
| 42 | + // lambda |
| 43 | + final AtomicReference<FileTime> lastModifiedTime = |
| 44 | + new AtomicReference<>(Files.getLastModifiedTime(path)); |
44 | 45 |
|
45 | | - // 1 - create service account credentials |
46 | | - ServiceAccountCredentials serviceAccountCredentials; |
47 | | - try (FileInputStream is = new FileInputStream(credentialsPath)) { |
48 | | - serviceAccountCredentials = ServiceAccountCredentials.fromStream(is); |
49 | | - } |
| 46 | + // 1 - create service account credentials |
| 47 | + ServiceAccountCredentials serviceAccountCredentials; |
| 48 | + try (FileInputStream is = new FileInputStream(credentialsPath)) { |
| 49 | + serviceAccountCredentials = ServiceAccountCredentials.fromStream(is); |
| 50 | + } |
50 | 51 |
|
51 | | - // 2 - wrap credentials from step 1 in a MutableCredentials instance |
52 | | - MutableCredentials mutableCredentials = new MutableCredentials(serviceAccountCredentials); |
| 52 | + // 2 - wrap credentials from step 1 in a MutableCredentials instance |
| 53 | + MutableCredentials mutableCredentials = new MutableCredentials(serviceAccountCredentials); |
53 | 54 |
|
54 | | - // 3 - set credentials on your SpannerOptions builder to your mutableCredentials |
55 | | - SpannerOptions options = SpannerOptions.newBuilder().setCredentials(mutableCredentials).build(); |
| 55 | + // 3 - set credentials on your SpannerOptions builder to your mutableCredentials |
| 56 | + SpannerOptions options = SpannerOptions.newBuilder().setCredentials(mutableCredentials).build(); |
56 | 57 |
|
57 | | - // 4 - include logic for when/how to update your mutableCredentials |
58 | | - // In this example we'll use a SchedulerExecutorService to periodically check for updates |
59 | | - ThreadFactory daemonThreadFactory = |
60 | | - runnable -> { |
61 | | - Thread thread = new Thread(runnable, "spanner-mutable-credentials-rotator"); |
62 | | - thread.setDaemon(true); |
63 | | - return thread; |
64 | | - }; |
65 | | - ScheduledExecutorService executorService = |
66 | | - Executors.newSingleThreadScheduledExecutor(daemonThreadFactory); |
67 | | - executorService.scheduleAtFixedRate( |
68 | | - () -> { |
69 | | - try { |
70 | | - FileTime currentModifiedTime = Files.getLastModifiedTime(path); |
71 | | - if (currentModifiedTime.compareTo(lastModifiedTime.get()) > 0) { |
72 | | - lastModifiedTime.set(currentModifiedTime); |
73 | | - try (FileInputStream is = new FileInputStream(credentialsPath)) { |
74 | | - ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(is); |
75 | | - mutableCredentials.updateCredentials(credentials); |
76 | | - } |
77 | | - } |
78 | | - } catch (IOException e) { |
79 | | - System.err.println("Failed to check or update credentials: " + e.getMessage()); |
80 | | - } |
81 | | - }, |
82 | | - 15, |
83 | | - 15, |
84 | | - TimeUnit.MINUTES); |
| 58 | + // 4 - include logic for when/how to update your mutableCredentials |
| 59 | + // In this example we'll use a SchedulerExecutorService to periodically check for updates |
| 60 | + ThreadFactory daemonThreadFactory = |
| 61 | + runnable -> { |
| 62 | + Thread thread = new Thread(runnable, "spanner-mutable-credentials-rotator"); |
| 63 | + thread.setDaemon(true); |
| 64 | + return thread; |
| 65 | + }; |
| 66 | + ScheduledExecutorService executorService = |
| 67 | + Executors.newSingleThreadScheduledExecutor(daemonThreadFactory); |
| 68 | + executorService.scheduleAtFixedRate( |
| 69 | + () -> { |
| 70 | + try { |
| 71 | + FileTime currentModifiedTime = Files.getLastModifiedTime(path); |
| 72 | + if (currentModifiedTime.compareTo(lastModifiedTime.get()) > 0) { |
| 73 | + lastModifiedTime.set(currentModifiedTime); |
| 74 | + try (FileInputStream is = new FileInputStream(credentialsPath)) { |
| 75 | + ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(is); |
| 76 | + mutableCredentials.updateCredentials(credentials); |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (IOException e) { |
| 80 | + System.err.println("Failed to check or update credentials: " + e.getMessage()); |
| 81 | + } |
| 82 | + }, |
| 83 | + 15, |
| 84 | + 15, |
| 85 | + TimeUnit.MINUTES); |
85 | 86 |
|
86 | | - // 5. Use the client |
87 | | - try (Spanner spanner = options.getService(); |
88 | | - DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { |
89 | | - // Perform operations... |
90 | | - // long running client operations will always use the latest credentials wrapped in |
91 | | - // mutableCredentials |
92 | | - } finally { |
93 | | - // Ensure the executor is shut down when the application exits or the client is closed |
94 | | - executorService.shutdown(); |
95 | | - } |
| 87 | + // 5. Use the client |
| 88 | + try (Spanner spanner = options.getService(); |
| 89 | + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { |
| 90 | + // Perform operations... |
| 91 | + // long running client operations will always use the latest credentials wrapped in |
| 92 | + // mutableCredentials |
| 93 | + } finally { |
| 94 | + // Ensure the executor is shut down when the application exits or the client is closed |
| 95 | + executorService.shutdown(); |
96 | 96 | } |
| 97 | + } |
97 | 98 | } |
98 | 99 | // [END spanner_mutable_credentials] |
0 commit comments