|
| 1 | +package io.temporal.samples.envconfig; |
| 2 | + |
| 3 | +import io.temporal.client.WorkflowClient; |
| 4 | +import io.temporal.client.WorkflowClientOptions; |
| 5 | +import io.temporal.envconfig.ClientConfigProfile; |
| 6 | +import io.temporal.envconfig.LoadClientConfigProfileOptions; |
| 7 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 8 | +import io.temporal.serviceclient.WorkflowServiceStubsOptions; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +/** |
| 14 | + * This sample demonstrates loading a specific profile from a TOML configuration file with |
| 15 | + * programmatic overrides. |
| 16 | + */ |
| 17 | +public class LoadProfile { |
| 18 | + |
| 19 | + private static final Logger logger = LoggerFactory.getLogger(LoadProfile.class); |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + String profileName = "staging"; |
| 23 | + |
| 24 | + try { |
| 25 | + // For this sample to be self-contained, we explicitly provide the path to |
| 26 | + // the config.toml file included in this directory. |
| 27 | + String configFilePath = |
| 28 | + Paths.get(LoadProfile.class.getResource("/config.toml").toURI()).toString(); |
| 29 | + |
| 30 | + logger.info("--- Loading '{}' profile from {} ---", profileName, configFilePath); |
| 31 | + |
| 32 | + // Load specific profile from file with environment variable overrides |
| 33 | + ClientConfigProfile profile = |
| 34 | + ClientConfigProfile.load( |
| 35 | + LoadClientConfigProfileOptions.newBuilder() |
| 36 | + .setConfigFilePath(configFilePath) |
| 37 | + .setConfigFileProfile(profileName) |
| 38 | + .build()); |
| 39 | + |
| 40 | + // Demonstrate programmatic override - fix the incorrect address from staging profile |
| 41 | + logger.info("\n--- Applying programmatic override ---"); |
| 42 | + ClientConfigProfile.Builder profileBuilder = profile.toBuilder(); |
| 43 | + profileBuilder.setAddress("localhost:7233"); // Override the incorrect address |
| 44 | + profile = profileBuilder.build(); |
| 45 | + logger.info(" Overridden address to: {}", profile.getAddress()); |
| 46 | + |
| 47 | + // Convert profile to client options (equivalent to Python's load_client_connect_config) |
| 48 | + WorkflowServiceStubsOptions serviceStubsOptions = profile.toWorkflowServiceStubsOptions(); |
| 49 | + WorkflowClientOptions clientOptions = profile.toWorkflowClientOptions(); |
| 50 | + |
| 51 | + logger.info("Loaded '{}' profile from {}", profileName, configFilePath); |
| 52 | + logger.info(" Address: {}", serviceStubsOptions.getTarget()); |
| 53 | + logger.info(" Namespace: {}", clientOptions.getNamespace()); |
| 54 | + if (serviceStubsOptions.getHeaders() != null |
| 55 | + && !serviceStubsOptions.getHeaders().keys().isEmpty()) { |
| 56 | + logger.info(" gRPC Metadata keys: {}", serviceStubsOptions.getHeaders().keys()); |
| 57 | + } |
| 58 | + |
| 59 | + logger.info("\nAttempting to connect to client..."); |
| 60 | + |
| 61 | + try { |
| 62 | + // Create the workflow client using the loaded configuration |
| 63 | + WorkflowClient client = |
| 64 | + WorkflowClient.newInstance( |
| 65 | + WorkflowServiceStubs.newServiceStubs(serviceStubsOptions), clientOptions); |
| 66 | + |
| 67 | + // Test the connection by getting system info |
| 68 | + var systemInfo = |
| 69 | + client |
| 70 | + .getWorkflowServiceStubs() |
| 71 | + .blockingStub() |
| 72 | + .getSystemInfo( |
| 73 | + io.temporal.api.workflowservice.v1.GetSystemInfoRequest.getDefaultInstance()); |
| 74 | + |
| 75 | + logger.info("✅ Client connected successfully!"); |
| 76 | + logger.info(" Server version: {}", systemInfo.getServerVersion()); |
| 77 | + |
| 78 | + } catch (Exception e) { |
| 79 | + logger.error("❌ Failed to connect: {}", e.getMessage()); |
| 80 | + } |
| 81 | + |
| 82 | + } catch (Exception e) { |
| 83 | + logger.error("Failed to load configuration: {}", e.getMessage(), e); |
| 84 | + System.exit(1); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments