|
21 | 21 | import org.eclipse.microprofile.config.inject.ConfigProperty; |
22 | 22 |
|
23 | 23 | /** |
24 | | - * Aeron Ingress Singleton Bean Launches an embedded MediaDriver and subscribes to market data |
25 | | - * stream. Uses SBE decoders for zero-copy message processing. Runs in a dedicated thread to |
26 | | - * continuously poll for messages. IMPORTANT: This must initialize BEFORE MarketDataPublisher |
| 24 | + * Aeron Ingress Singleton Bean Launches an embedded MediaDriver and subscribes to market data stream. Uses SBE decoders for zero-copy message processing. Runs |
| 25 | + * in a dedicated thread to continuously poll for messages. IMPORTANT: This must initialize BEFORE MarketDataPublisher |
27 | 26 | */ |
28 | 27 | @ApplicationScoped |
29 | 28 | public class AeronSubscriberBean { |
30 | 29 |
|
31 | | - private static final Logger LOGGER = Logger.getLogger(AeronSubscriberBean.class.getName()); |
| 30 | + private static final Logger LOGGER = Logger.getLogger(AeronSubscriberBean.class.getName()); |
32 | 31 |
|
33 | | - private static final String CHANNEL = "aeron:ipc"; |
34 | | - private static final int STREAM_ID = 1001; |
35 | | - private static final int FRAGMENT_LIMIT = 10; |
| 32 | + private static final String CHANNEL = "aeron:ipc"; |
| 33 | + private static final int STREAM_ID = 1001; |
| 34 | + private static final int FRAGMENT_LIMIT = 10; |
36 | 35 |
|
37 | | - private MediaDriver mediaDriver; |
38 | | - private Aeron aeron; |
39 | | - private Subscription subscription; |
40 | | - private volatile boolean running = false; |
41 | | - private Future<?> pollingFuture; |
| 36 | + private MediaDriver mediaDriver; |
| 37 | + private Aeron aeron; |
| 38 | + private Subscription subscription; |
| 39 | + private volatile boolean running = false; |
| 40 | + private Future<?> pollingFuture; |
42 | 41 |
|
43 | | - @Inject private MarketDataFragmentHandler fragmentHandler; |
| 42 | + @Inject |
| 43 | + private MarketDataFragmentHandler fragmentHandler; |
44 | 44 |
|
45 | | - @Inject @VirtualThreadExecutor private ManagedExecutorService managedExecutorService; |
| 45 | + @Inject |
| 46 | + @VirtualThreadExecutor |
| 47 | + private ManagedExecutorService managedExecutorService; |
46 | 48 |
|
47 | | - @Inject |
48 | | - @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") |
49 | | - private String ingestionMode; |
| 49 | + @Inject |
| 50 | + @ConfigProperty(name = "TRADER_INGESTION_MODE", defaultValue = "AERON") |
| 51 | + private String ingestionMode; |
50 | 52 |
|
51 | | - void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { |
52 | | - managedExecutorService.submit(this::init); |
53 | | - } |
54 | | - |
55 | | - public void init() { |
56 | | - if ("DIRECT".equalsIgnoreCase(ingestionMode)) { |
57 | | - LOGGER.info("Running in DIRECT mode - Skipping Aeron/MediaDriver initialization."); |
58 | | - return; |
| 53 | + void contextInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) { |
| 54 | + managedExecutorService.submit(this::init); |
59 | 55 | } |
60 | 56 |
|
61 | | - LOGGER.info("Initializing Aeron Subscriber Bean..."); |
62 | | - |
63 | | - try { |
64 | | - LOGGER.info("Launching embedded MediaDriver..."); |
65 | | - mediaDriver = |
66 | | - MediaDriver.launchEmbedded( |
67 | | - new MediaDriver.Context() |
68 | | - .threadingMode(ThreadingMode.SHARED) |
69 | | - .dirDeleteOnStart(true) |
70 | | - .dirDeleteOnShutdown(true)); |
71 | | - |
72 | | - LOGGER.info("MediaDriver launched at: " + mediaDriver.aeronDirectoryName()); |
73 | | - LOGGER.info("Connecting Aeron client..."); |
74 | | - aeron = |
75 | | - Aeron.connect( |
76 | | - new Aeron.Context() |
77 | | - .aeronDirectoryName(mediaDriver.aeronDirectoryName()) |
78 | | - .errorHandler(this::onError) |
79 | | - .availableImageHandler( |
80 | | - image -> LOGGER.info("Available image: " + image.sourceIdentity())) |
81 | | - .unavailableImageHandler( |
82 | | - image -> LOGGER.info("Unavailable image: " + image.sourceIdentity()))); |
83 | | - LOGGER.info("Adding subscription on channel: " + CHANNEL + ", stream: " + STREAM_ID); |
84 | | - subscription = aeron.addSubscription(CHANNEL, STREAM_ID); |
85 | | - startPolling(); |
86 | | - LOGGER.info("Aeron Subscriber Bean initialized successfully"); |
87 | | - |
88 | | - } catch (Exception e) { |
89 | | - LOGGER.log(Level.SEVERE, "Failed to initialize Aeron Subscriber Bean", e); |
90 | | - cleanup(); |
91 | | - throw new RuntimeException("Failed to initialize Aeron", e); |
| 57 | + public void init() { |
| 58 | + if ("DIRECT".equalsIgnoreCase(ingestionMode)) { |
| 59 | + LOGGER.info("Running in DIRECT mode - Skipping Aeron/MediaDriver initialization."); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + LOGGER.info("Initializing Aeron Subscriber Bean..."); |
| 64 | + |
| 65 | + try { |
| 66 | + LOGGER.info("Launching embedded MediaDriver..."); |
| 67 | + mediaDriver = MediaDriver |
| 68 | + .launchEmbedded(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).dirDeleteOnShutdown(true)); |
| 69 | + |
| 70 | + LOGGER.info("MediaDriver launched at: " + mediaDriver.aeronDirectoryName()); |
| 71 | + LOGGER.info("Connecting Aeron client..."); |
| 72 | + aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()) |
| 73 | + .errorHandler(this::onError) |
| 74 | + .availableImageHandler(image -> LOGGER.info("Available image: " + image.sourceIdentity())) |
| 75 | + .unavailableImageHandler(image -> LOGGER.info("Unavailable image: " + image.sourceIdentity()))); |
| 76 | + LOGGER.info("Adding subscription on channel: " + CHANNEL + ", stream: " + STREAM_ID); |
| 77 | + subscription = aeron.addSubscription(CHANNEL, STREAM_ID); |
| 78 | + startPolling(); |
| 79 | + LOGGER.info("Aeron Subscriber Bean initialized successfully"); |
| 80 | + |
| 81 | + } catch (Exception e) { |
| 82 | + LOGGER.log(Level.SEVERE, "Failed to initialize Aeron Subscriber Bean", e); |
| 83 | + cleanup(); |
| 84 | + throw new RuntimeException("Failed to initialize Aeron", e); |
| 85 | + } |
92 | 86 | } |
93 | | - } |
94 | | - |
95 | | - /** Start background task to continuously poll for messages */ |
96 | | - private void startPolling() { |
97 | | - running = true; |
98 | | - pollingFuture = |
99 | | - managedExecutorService.submit( |
100 | | - () -> { |
101 | | - LOGGER.info("Aeron polling task started"); |
102 | | - |
103 | | - final IdleStrategy idleStrategy = |
104 | | - new BackoffIdleStrategy( |
105 | | - 100, |
106 | | - 10, |
107 | | - TimeUnit.MICROSECONDS.toNanos(1), |
108 | | - TimeUnit.MICROSECONDS.toNanos(100)); |
109 | | - |
110 | | - while (running && !Thread.currentThread().isInterrupted()) { |
| 87 | + |
| 88 | + /** Start background task to continuously poll for messages */ |
| 89 | + private void startPolling() { |
| 90 | + running = true; |
| 91 | + pollingFuture = managedExecutorService.submit(() -> { |
| 92 | + LOGGER.info("Aeron polling task started"); |
| 93 | + |
| 94 | + final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100)); |
| 95 | + |
| 96 | + while (running && !Thread.currentThread().isInterrupted()) { |
111 | 97 | try { |
112 | | - final int fragmentsRead = subscription.poll(fragmentHandler, FRAGMENT_LIMIT); |
| 98 | + final int fragmentsRead = subscription.poll(fragmentHandler, FRAGMENT_LIMIT); |
113 | 99 |
|
114 | | - idleStrategy.idle(fragmentsRead); |
| 100 | + idleStrategy.idle(fragmentsRead); |
115 | 101 |
|
116 | 102 | } catch (Exception e) { |
117 | | - LOGGER.log(Level.SEVERE, "Error polling subscription", e); |
| 103 | + LOGGER.log(Level.SEVERE, "Error polling subscription", e); |
118 | 104 | } |
119 | | - } |
| 105 | + } |
| 106 | + |
| 107 | + LOGGER.info("Aeron polling task stopped"); |
| 108 | + }); |
| 109 | + } |
120 | 110 |
|
121 | | - LOGGER.info("Aeron polling task stopped"); |
122 | | - }); |
123 | | - } |
| 111 | + /** Error handler for Aeron */ |
| 112 | + private void onError(Throwable throwable) { |
| 113 | + LOGGER.log(Level.SEVERE, "Aeron error occurred", throwable); |
| 114 | + } |
124 | 115 |
|
125 | | - /** Error handler for Aeron */ |
126 | | - private void onError(Throwable throwable) { |
127 | | - LOGGER.log(Level.SEVERE, "Aeron error occurred", throwable); |
128 | | - } |
| 116 | + @PreDestroy |
| 117 | + public void shutdown() { |
| 118 | + LOGGER.info("Shutting down Aeron Subscriber Bean..."); |
| 119 | + running = false; |
129 | 120 |
|
130 | | - @PreDestroy |
131 | | - public void shutdown() { |
132 | | - LOGGER.info("Shutting down Aeron Subscriber Bean..."); |
133 | | - running = false; |
| 121 | + if (pollingFuture != null) { |
| 122 | + pollingFuture.cancel(true); |
| 123 | + } |
134 | 124 |
|
135 | | - if (pollingFuture != null) { |
136 | | - pollingFuture.cancel(true); |
| 125 | + cleanup(); |
| 126 | + LOGGER.info("Aeron Subscriber Bean shut down"); |
137 | 127 | } |
138 | 128 |
|
139 | | - cleanup(); |
140 | | - LOGGER.info("Aeron Subscriber Bean shut down"); |
141 | | - } |
142 | | - |
143 | | - /** Clean up all Aeron resources */ |
144 | | - private void cleanup() { |
145 | | - CloseHelper.quietClose(subscription); |
146 | | - CloseHelper.quietClose(aeron); |
147 | | - CloseHelper.quietClose(mediaDriver); |
148 | | - } |
149 | | - |
150 | | - /** Get subscription statistics */ |
151 | | - public String getStatus() { |
152 | | - if (subscription != null) { |
153 | | - return String.format( |
154 | | - "Channel: %s, Stream: %d, Images: %d, Running: %b", |
155 | | - subscription.channel(), subscription.streamId(), subscription.imageCount(), running); |
| 129 | + /** Clean up all Aeron resources */ |
| 130 | + private void cleanup() { |
| 131 | + CloseHelper.quietClose(subscription); |
| 132 | + CloseHelper.quietClose(aeron); |
| 133 | + CloseHelper.quietClose(mediaDriver); |
156 | 134 | } |
157 | | - return "Not initialized"; |
158 | | - } |
159 | 135 |
|
160 | | - /** Get the Aeron directory name for connecting publishers */ |
161 | | - public String getAeronDirectoryName() { |
162 | | - if (mediaDriver != null) { |
163 | | - return mediaDriver.aeronDirectoryName(); |
| 136 | + /** Get subscription statistics */ |
| 137 | + public String getStatus() { |
| 138 | + if (subscription != null) { |
| 139 | + return String.format("Channel: %s, Stream: %d, Images: %d, Running: %b", subscription.channel(), subscription.streamId(), subscription.imageCount(), |
| 140 | + running); |
| 141 | + } |
| 142 | + return "Not initialized"; |
164 | 143 | } |
165 | | - return null; |
166 | | - } |
167 | 144 |
|
168 | | - /** Check if the MediaDriver is ready */ |
169 | | - public boolean isReady() { |
170 | | - return mediaDriver != null && aeron != null && subscription != null && running; |
171 | | - } |
| 145 | + /** Get the Aeron directory name for connecting publishers */ |
| 146 | + public String getAeronDirectoryName() { |
| 147 | + if (mediaDriver != null) { |
| 148 | + return mediaDriver.aeronDirectoryName(); |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + /** Check if the MediaDriver is ready */ |
| 154 | + public boolean isReady() { |
| 155 | + return mediaDriver != null && aeron != null && subscription != null && running; |
| 156 | + } |
172 | 157 | } |
0 commit comments