001package com.streamconverter.examples; 002 003import com.streamconverter.CommandResult; 004import com.streamconverter.StreamConverter; 005import com.streamconverter.command.impl.SampleStreamCommand; 006import com.streamconverter.command.impl.csv.CsvNavigateCommand; 007import com.streamconverter.command.rule.PassThroughRule; 008import com.streamconverter.context.ExecutionContext; 009import com.streamconverter.path.CSVPath; 010import java.io.ByteArrayInputStream; 011import java.io.ByteArrayOutputStream; 012import java.io.IOException; 013import java.nio.charset.StandardCharsets; 014import java.util.List; 015import org.slf4j.Logger; 016import org.slf4j.LoggerFactory; 017 018/** 019 * StreamConverterのMDC機能統合デモ 020 * 021 * <p>このデモでは、StreamConverterに統合されたMDC機能により、デフォルトでMDC同期が有効になることを示します。 022 */ 023public class StreamConverterMDCDemo { 024 private static final Logger logger = LoggerFactory.getLogger(StreamConverterMDCDemo.class); 025 026 /** 027 * Main method to demonstrate StreamConverter MDC integration. 028 * 029 * @param args command line arguments (not used) 030 * @throws IOException if file operations fail 031 */ 032 public static void main(String[] args) throws IOException { 033 logger.info("🚀 StreamConverter MDC Integration Demo"); 034 035 demonstrateAutomaticMDC(); 036 demonstrateCustomContext(); 037 demonstrateMultipleCommandsMDC(); 038 039 logger.info("✅ All demonstrations completed successfully!"); 040 } 041 042 /** デモ1: 自動MDC生成 */ 043 private static void demonstrateAutomaticMDC() throws IOException { 044 logger.info("📊 Demo 1: Automatic MDC Generation"); 045 046 // 既存のAPIを使用 - MDCが自動的に有効化される 047 SampleStreamCommand command = new SampleStreamCommand("auto-mdc"); 048 StreamConverter converter = StreamConverter.create(command); 049 050 String testData = "id,name,department\n1,Alice,Engineering\n2,Bob,Marketing\n"; 051 ByteArrayInputStream inputStream = 052 new ByteArrayInputStream(testData.getBytes(StandardCharsets.UTF_8)); 053 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 054 055 logger.info("Executing pipeline with automatic MDC..."); 056 List<CommandResult> results = converter.run(inputStream, outputStream); 057 058 String result = outputStream.toString(StandardCharsets.UTF_8); 059 logger.info( 060 "Demo 1 completed. Results: {} commands, Output length: {} characters\n", 061 results.size(), 062 result.length()); 063 } 064 065 /** デモ2: カスタムコンテキストでのMDC */ 066 private static void demonstrateCustomContext() throws IOException { 067 logger.info("🎯 Demo 2: Custom ExecutionContext with MDC"); 068 069 // カスタムExecutionContextを作成 070 ExecutionContext customContext = 071 ExecutionContext.builder() 072 .globalContext("requestId", "REQ-DEMO-456") 073 .globalContext("userId", "demo-user") 074 .globalContext("sessionId", "session-789") 075 .userContext("businessUnit", "development") 076 .userContext("priority", "high") 077 .build(); 078 079 String testData = "transaction,amount,currency\n1,100.50,USD\n2,75.25,EUR\n"; 080 081 // カスタムコンテキストでコンバーター作成 082 SampleStreamCommand enrichmentCommand = new SampleStreamCommand("enrichment"); 083 SampleStreamCommand auditCommand = new SampleStreamCommand("audit"); 084 StreamConverter converter = 085 StreamConverter.createWithContext(customContext, enrichmentCommand, auditCommand); 086 087 ByteArrayInputStream inputStream = 088 new ByteArrayInputStream(testData.getBytes(StandardCharsets.UTF_8)); 089 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 090 091 logger.info("Executing pipeline with custom context..."); 092 List<CommandResult> results = converter.run(inputStream, outputStream); 093 094 String result = outputStream.toString(StandardCharsets.UTF_8); 095 logger.info( 096 "Demo 2 completed. ExecutionId: {}, Results: {} commands", 097 customContext.getExecutionId(), 098 results.size()); 099 logger.info("Output length: {} characters\n", result.length()); 100 } 101 102 /** デモ3: 複数コマンドでのMDC同期 */ 103 private static void demonstrateMultipleCommandsMDC() throws IOException { 104 logger.info("🔀 Demo 3: Multiple Commands with MDC Synchronization"); 105 106 String testData = "product,price,category\nLaptop,999.99,Electronics\nBook,19.99,Education\n"; 107 108 // 複数のコマンドでパイプライン構築 109 SampleStreamCommand validator = new SampleStreamCommand("validator"); 110 CsvNavigateCommand extractor = 111 CsvNavigateCommand.create(new CSVPath("product"), new PassThroughRule()); 112 SampleStreamCommand formatter = new SampleStreamCommand("formatter"); 113 114 StreamConverter converter = StreamConverter.create(validator, extractor, formatter); 115 116 ByteArrayInputStream inputStream = 117 new ByteArrayInputStream(testData.getBytes(StandardCharsets.UTF_8)); 118 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 119 120 logger.info("Executing multi-command pipeline with MDC synchronization..."); 121 List<CommandResult> results = converter.run(inputStream, outputStream); 122 123 String result = outputStream.toString(StandardCharsets.UTF_8); 124 logger.info("Demo 3 completed. Results: {} commands executed", results.size()); 125 logger.info("Final output: {}", result.trim()); 126 logger.info("All commands completed with MDC synchronization\n"); 127 } 128}