33import io .aeron .Aeron ;
44import io .aeron .Publication ;
55import io .aeron .Subscription ;
6+ import io .aeron .driver .MediaDriver ;
67import io .aeron .logbuffer .Header ;
78import java .util .ArrayList ;
89import java .util .List ;
@@ -19,7 +20,10 @@ public record AeronConfig(
1920 String aeronDirectory ,
2021 int fragmentLimit ,
2122 int offerMaxAttempts ,
22- IdleStrategy idleStrategy
23+ IdleStrategy idleStrategy ,
24+ boolean embeddedDriver ,
25+ boolean dirDeleteOnStart ,
26+ boolean dirDeleteOnShutdown
2327 ) {
2428 public AeronConfig {
2529 if (fragmentLimit <= 0 ) {
@@ -34,27 +38,46 @@ public record AeronConfig(
3438 }
3539
3640 public static AeronConfig defaults (String channel , int streamId , String aeronDirectory ) {
37- return new AeronConfig (channel , streamId , aeronDirectory , 64 , 10 , new BusySpinIdleStrategy ());
41+ return new AeronConfig (channel , streamId , aeronDirectory , 64 , 10 , new BusySpinIdleStrategy (), false , false , false );
42+ }
43+
44+ public static AeronConfig embeddedDefaults (String channel , int streamId , String aeronDirectory ) {
45+ return new AeronConfig (channel , streamId , aeronDirectory , 64 , 10 , new BusySpinIdleStrategy (), true , true , true );
3846 }
3947 }
4048
4149 private final AeronConfig config ;
4250 private final Aeron aeron ;
4351 private final Publication publication ;
4452 private final Subscription subscription ;
53+ private final MediaDriver mediaDriver ;
4554 private final UnsafeBuffer sendBuffer ;
4655 private final IdleStrategy idleStrategy ;
4756 private final AeronStats stats = new AeronStats ();
4857
4958 public AeronTransport (AeronConfig config ) {
5059 this .config = config ;
60+ MediaDriver driver = null ;
61+ String aeronDir = config .aeronDirectory ();
62+ if (config .embeddedDriver ()) {
63+ MediaDriver .Context driverContext = new MediaDriver .Context ();
64+ if (aeronDir != null && !aeronDir .isBlank ()) {
65+ driverContext .aeronDirectoryName (aeronDir );
66+ }
67+ driverContext .dirDeleteOnStart (config .dirDeleteOnStart ());
68+ driverContext .dirDeleteOnShutdown (config .dirDeleteOnShutdown ());
69+ driver = MediaDriver .launch (driverContext );
70+ aeronDir = driverContext .aeronDirectoryName ();
71+ }
72+
5173 Aeron .Context context = new Aeron .Context ();
52- if (config . aeronDirectory () != null && !config . aeronDirectory () .isBlank ()) {
53- context .aeronDirectoryName (config . aeronDirectory () );
74+ if (aeronDir != null && !aeronDir .isBlank ()) {
75+ context .aeronDirectoryName (aeronDir );
5476 }
5577 this .aeron = Aeron .connect (context );
5678 this .publication = aeron .addPublication (config .channel (), config .streamId ());
5779 this .subscription = aeron .addSubscription (config .channel (), config .streamId ());
80+ this .mediaDriver = driver ;
5881 this .sendBuffer = new UnsafeBuffer (new ExpandableArrayBuffer (AeronMessageCodec .FRAME_LENGTH ));
5982 this .idleStrategy = config .idleStrategy ();
6083 }
@@ -100,6 +123,9 @@ public void close() {
100123 publication .close ();
101124 subscription .close ();
102125 aeron .close ();
126+ if (mediaDriver != null ) {
127+ mediaDriver .close ();
128+ }
103129 }
104130
105131 public AeronConfig config () {
0 commit comments