io.aeron.Aeron.connect()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.8k)|赞(0)|评价(0)|浏览(116)

本文整理了Java中io.aeron.Aeron.connect()方法的一些代码示例,展示了Aeron.connect()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Aeron.connect()方法的具体详情如下:
包路径:io.aeron.Aeron
类名称:Aeron
方法名:connect

Aeron.connect介绍

[英]Create an Aeron instance and connect to the media driver with a default Context.

Threads required for interacting with the media driver are created and managed within the Aeron instance.
[中]创建Aeron实例并使用默认上下文连接到媒体驱动程序。
与媒体驱动程序交互所需的线程在Aeron实例中创建和管理。

代码示例

代码示例来源:origin: real-logic/aeron

/**
 * Create an Aeron instance and connect to the media driver with a default {@link Context}.
 * <p>
 * Threads required for interacting with the media driver are created and managed within the Aeron instance.
 *
 * @return the new {@link Aeron} instance connected to the Media Driver.
 */
public static Aeron connect()
{
  return connect(new Context());
}

代码示例来源:origin: real-logic/aeron

public static void main(final String[] args)
  {
    if (args.length != 1)
    {
      System.out.format("Usage: SetControllableIdleStrategy <n>");
      System.exit(0);
    }

    try (Aeron aeron = Aeron.connect())
    {
      final CountersReader countersReader = aeron.countersReader();
      final StatusIndicator statusIndicator = StatusUtil.controllableIdleStrategy(countersReader);

      if (null != statusIndicator)
      {
        final int status = Integer.parseInt(args[0]);
        statusIndicator.setOrdered(status);
        System.out.println("Set ControllableIdleStrategy status to " + status);
      }
      else
      {
        System.out.println("Could not find ControllableIdleStrategy status.");
      }
    }
  }
}

代码示例来源:origin: real-logic/aeron

public static void main(final String[] args) throws Exception
{
  if (args.length != 1)
  {
    System.out.println("Filename to be sent must be supplied as a command line argument");
    System.exit(1);
  }
  try (Aeron aeron = Aeron.connect();
    Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
  {
    while (!publication.isConnected())
    {
      Thread.sleep(1);
    }
    final File file = new File(args[0]);
    final UnsafeBuffer buffer = new UnsafeBuffer(IoUtil.mapExistingFile(file, "sending"));
    final long correlationId = aeron.nextCorrelationId();
    sendFileCreate(publication, correlationId, buffer.capacity(), file.getName());
    streamChunks(publication, correlationId, buffer);
  }
}

代码示例来源:origin: real-logic/aeron

private static Thread startPong(final String embeddedDirName)
{
  return new Thread(() ->
  {
    System.out.println("Subscribing Ping at " + PING_CHANNEL + " on stream Id " + PING_STREAM_ID);
    System.out.println("Publishing Pong at " + PONG_CHANNEL + " on stream Id " + PONG_STREAM_ID);
    final Aeron.Context ctx = new Aeron.Context().aeronDirectoryName(embeddedDirName);
    try (Aeron aeron = Aeron.connect(ctx);
      Subscription pingSubscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID);
      Publication pongPublication = EXCLUSIVE_PUBLICATIONS ?
        aeron.addExclusivePublication(PONG_CHANNEL, PONG_STREAM_ID) :
        aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID))
    {
      final FragmentAssembler dataHandler = new FragmentAssembler(
        (buffer, offset, length, header) -> pingHandler(pongPublication, buffer, offset, length));
      while (RUNNING.get())
      {
        PING_HANDLER_IDLE_STRATEGY.idle(pingSubscription.poll(dataHandler, FRAME_COUNT_LIMIT));
      }
      System.out.println("Shutting down...");
    }
  });
}

代码示例来源:origin: real-logic/aeron

public EmbeddedReplayThroughput()
{
  final String archiveDirName = Archive.Configuration.archiveDirName();
  final File archiveDir = ARCHIVE_DIR_DEFAULT.equals(archiveDirName) ?
    TestUtil.createTempDir() : new File(archiveDirName);
  archivingMediaDriver = ArchivingMediaDriver.launch(
    new MediaDriver.Context()
      .dirDeleteOnStart(true),
    new Archive.Context()
      .archiveDir(archiveDir));
  aeron = Aeron.connect();
  aeronArchive = AeronArchive.connect(
    new AeronArchive.Context()
      .aeron(aeron));
}

代码示例来源:origin: real-logic/aeron

private void launch(final String channel)
{
  context
    .threadingMode(THREADING_MODE)
    .errorHandler(Throwable::printStackTrace)
    .publicationConnectionTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500))
    .timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100));
  driver = MediaDriver.launch(context);
  subscribingClient = Aeron.connect();
  publishingClient = Aeron.connect();
  subscription = subscribingClient.addSubscription(channel, STREAM_ID);
  publication = publishingClient.addPublication(channel, STREAM_ID);
}

代码示例来源:origin: real-logic/aeron

public EmbeddedRecordingThroughput()
{
  final String archiveDirName = Archive.Configuration.archiveDirName();
  final File archiveDir = ARCHIVE_DIR_DEFAULT.equals(archiveDirName) ?
    TestUtil.createTempDir() : new File(archiveDirName);
  archivingMediaDriver = ArchivingMediaDriver.launch(
    new MediaDriver.Context()
      .spiesSimulateConnection(true)
      .dirDeleteOnStart(true),
    new Archive.Context()
      .deleteArchiveOnStart(true)
      .archiveDir(archiveDir));
  aeron = Aeron.connect();
  aeronArchive = AeronArchive.connect(
    new AeronArchive.Context()
      .aeron(aeron));
}

代码示例来源:origin: real-logic/aeron

private void launch()
{
  driverContext.publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .errorHandler(Throwable::printStackTrace)
    .threadingMode(ThreadingMode.SHARED);
  driver = MediaDriver.launch(driverContext);
  client = Aeron.connect();
}

代码示例来源:origin: real-logic/aeron

@Test
  public void shouldThrowWhenReentering()
  {
    final MutableReference<Throwable> expectedException = new MutableReference<>();
    final ErrorHandler errorHandler = expectedException::set;

    try (Aeron aeron = Aeron.connect(new Aeron.Context().errorHandler(errorHandler)))
    {
      final String channel = CommonContext.IPC_CHANNEL;
      final AvailableImageHandler mockHandler = mock(AvailableImageHandler.class);
      doAnswer((invocation) -> aeron.addSubscription(channel, 3))
        .when(mockHandler).onAvailableImage(any(Image.class));

      final Subscription sub = aeron.addSubscription(channel, 1, mockHandler, null);
      final Publication pub = aeron.addPublication(channel, 1);

      verify(mockHandler, timeout(5000)).onAvailableImage(any(Image.class));

      pub.close();
      sub.close();

      assertThat(expectedException.get(), instanceOf(AeronException.class));
    }
  }
}

代码示例来源:origin: real-logic/aeron

@Before
public void before()
{
  driver = MediaDriver.launch(
    new MediaDriver.Context()
      .errorHandler(Throwable::printStackTrace)
      .publicationTermBufferLength(LogBufferDescriptor.TERM_MIN_LENGTH)
      .threadingMode(ThreadingMode.SHARED));
  pingClient = Aeron.connect();
  pongClient = Aeron.connect();
  pingSubscription = pongClient.addSubscription(PING_URI, PING_STREAM_ID);
  pingPublication = pingClient.addPublication(PING_URI, PING_STREAM_ID);
  pongSubscription = pingClient.addSubscription(PONG_URI, PONG_STREAM_ID);
  pongPublication = pongClient.addPublication(PONG_URI, PONG_STREAM_ID);
}

代码示例来源:origin: real-logic/aeron

private void launch()
{
  labelBuffer.putStringWithoutLengthAscii(0, COUNTER_LABEL);
  driver = MediaDriver.launch(
    new MediaDriver.Context()
      .errorHandler(Throwable::printStackTrace)
      .threadingMode(ThreadingMode.SHARED));
  clientA = Aeron.connect(
    new Aeron.Context()
      .availableCounterHandler(availableCounterHandlerClientA)
      .unavailableCounterHandler(unavailableCounterHandlerClientA));
  clientB = Aeron.connect(
    new Aeron.Context()
      .availableCounterHandler(availableCounterHandlerClientB)
      .unavailableCounterHandler(unavailableCounterHandlerClientB));
}

代码示例来源:origin: real-logic/aeron

private void launch(final String channelOne, final int streamOne, final String channelTwo, final int streamTwo)
{
  driverOne = MediaDriver.launchEmbedded(
    new MediaDriver.Context()
      .errorHandler(Throwable::printStackTrace)
      .termBufferSparseFile(true));
  driverTwo = MediaDriver.launchEmbedded(
    new MediaDriver.Context()
      .errorHandler(Throwable::printStackTrace)
      .termBufferSparseFile(true));
  publisherOne = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverOne.aeronDirectoryName()));
  subscriberOne = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverTwo.aeronDirectoryName()));
  publisherTwo = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverOne.aeronDirectoryName()));
  subscriberTwo = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverTwo.aeronDirectoryName()));
  subscriptionOne = subscriberOne.addSubscription(channelOne, streamOne);
  subscriptionTwo = subscriberTwo.addSubscription(channelTwo, streamTwo);
  publicationOne = publisherOne.addPublication(channelOne, streamOne);
  publicationTwo = publisherTwo.addPublication(channelTwo, streamTwo);
}

代码示例来源:origin: real-logic/aeron

private void launch()
{
  final String baseDirA = ROOT_DIR + "A";
  buffer.putInt(0, 1);
  driverContextA
    .errorHandler(Throwable::printStackTrace)
    .publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .aeronDirectoryName(baseDirA)
    .threadingMode(ThreadingMode.SHARED);
  driverA = MediaDriver.launch(driverContextA);
  clientA = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverContextA.aeronDirectoryName()));
}

代码示例来源:origin: real-logic/aeron

public static boolean removeMember(
  final ClusterMarkFile markFile,
  final int memberId,
  final boolean isPassive)
{
  final String aeronDirectoryName = markFile.decoder().aeronDirectory();
  final String archiveChannel = markFile.decoder().archiveChannel();
  final String channel = markFile.decoder().serviceControlChannel();
  final int toServiceStreamId = markFile.decoder().serviceStreamId();
  final int toConsensusModuleStreamId = markFile.decoder().consensusModuleStreamId();
  try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(aeronDirectoryName));
    ConsensusModuleProxy consensusModuleProxy = new ConsensusModuleProxy(
      aeron.addPublication(channel, toConsensusModuleStreamId)))
  {
    if (consensusModuleProxy.removeMember(
      aeron.nextCorrelationId(), memberId, isPassive ? BooleanType.TRUE : BooleanType.FALSE))
    {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: real-logic/aeron

@Theory
  @Test
  public void shouldHaveCorrectTermBufferLength(final String channel)
  {
    final MediaDriver.Context ctx = new MediaDriver.Context()
      .errorHandler(Throwable::printStackTrace)
      .publicationTermBufferLength(TEST_TERM_LENGTH * 2)
      .ipcTermBufferLength(TEST_TERM_LENGTH * 2);

    try (MediaDriver ignore = MediaDriver.launch(ctx);
      Aeron aeron = Aeron.connect();
      Publication publication = aeron.addPublication(channel, STREAM_ID))
    {
      assertThat(publication.termBufferLength(), is(TEST_TERM_LENGTH));
    }
    finally
    {
      ctx.deleteAeronDirectory();
    }
  }
}

代码示例来源:origin: real-logic/aeron

private void launch()
{
  final String baseDirA = ROOT_DIR + "A";
  final String baseDirB = ROOT_DIR + "B";
  buffer.putInt(0, 1);
  driverAContext.publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .aeronDirectoryName(baseDirA)
    .timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100))
    .errorHandler(Throwable::printStackTrace)
    .threadingMode(ThreadingMode.SHARED);
  driverBContext.publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .aeronDirectoryName(baseDirB)
    .timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100))
    .errorHandler(Throwable::printStackTrace)
    .threadingMode(ThreadingMode.SHARED);
  driverA = MediaDriver.launch(driverAContext);
  driverB = MediaDriver.launch(driverBContext);
  clientA = Aeron.connect(
    new Aeron.Context()
      .errorHandler(Throwable::printStackTrace)
      .aeronDirectoryName(driverAContext.aeronDirectoryName()));
  clientB = Aeron.connect(
    new Aeron.Context()
      .errorHandler(Throwable::printStackTrace)
      .aeronDirectoryName(driverBContext.aeronDirectoryName()));
}

代码示例来源:origin: real-logic/aeron

@Test
  public void shouldStartAndStopInstantly()
  {
    final MediaDriver.Context driverCtx = new MediaDriver.Context()
      .errorHandler(Throwable::printStackTrace);

    try (MediaDriver ignore = MediaDriver.launchEmbedded(driverCtx))
    {
      final Aeron.Context clientCtx = new Aeron.Context()
        .aeronDirectoryName(driverCtx.aeronDirectoryName());

      //noinspection EmptyTryBlock
      try (Aeron ignored = Aeron.connect(clientCtx))
      {
        // ignore
      }
    }
    finally
    {
      driverCtx.deleteAeronDirectory();
    }
  }
}

代码示例来源:origin: real-logic/aeron

private void launch()
{
  final String baseDirA = ROOT_DIR + "A";
  final String baseDirB = ROOT_DIR + "B";
  buffer.putInt(0, 1);
  final MediaDriver.Context driverAContext = new MediaDriver.Context()
    .errorHandler(Throwable::printStackTrace)
    .publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .aeronDirectoryName(baseDirA)
    .threadingMode(ThreadingMode.SHARED);
  driverBContext.publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .errorHandler(Throwable::printStackTrace)
    .aeronDirectoryName(baseDirB)
    .threadingMode(ThreadingMode.SHARED);
  driverA = MediaDriver.launch(driverAContext);
  driverB = MediaDriver.launch(driverBContext);
  clientA = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverAContext.aeronDirectoryName()));
  clientB = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverBContext.aeronDirectoryName()));
}

代码示例来源:origin: real-logic/aeron

private void launch()
{
  final String baseDirA = ROOT_DIR + "A";
  final String baseDirB = ROOT_DIR + "B";
  buffer.putInt(0, 1);
  final MediaDriver.Context driverAContext = new MediaDriver.Context()
    .errorHandler(Throwable::printStackTrace)
    .publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .aeronDirectoryName(baseDirA)
    .threadingMode(THREADING_MODE);
  final MediaDriver.Context driverBContext = new MediaDriver.Context()
    .errorHandler(Throwable::printStackTrace)
    .publicationTermBufferLength(TERM_BUFFER_LENGTH)
    .aeronDirectoryName(baseDirB)
    .threadingMode(THREADING_MODE);
  driverA = MediaDriver.launch(driverAContext);
  driverB = MediaDriver.launch(driverBContext);
  clientA = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverAContext.aeronDirectoryName()));
  clientB = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverBContext.aeronDirectoryName()));
}

代码示例来源:origin: real-logic/aeron

.threadingMode(ArchiveThreadingMode.SHARED));
aeron = Aeron.connect(
  new Aeron.Context()
    .aeronDirectoryName(aeronDirectoryName));

相关文章