Aeron:基础示例

本文详细介绍了如何使用Aeron库在Java中通过IPC实现消息发送和接收,包括创建MediaDriver、Aeron实例、Subscription和Publication,以及关键代码段的解读。
摘要由CSDN通过智能技术生成

下面是一个通过 IPC 发送消息的完整方法。为了尽可能简单,publishersubscriber 在同一线程上,但这在实际应用中并不合理。尽管如此,它还是非常有用的,因为它展示了一个最小 Aeron 应用程序的完整设置。代码可在 aeron-ipc 项目的 SimplestCase.java 文件中找到。

作为示例的一部分,我们将构建以下4项:

  • the Aeron API;
  • the Media Driver;
  • a Subscription;
  • a Publication.

一、完整代码

public static void main(String[] args)
{
 final String channel = "aeron:ipc";
 final String message = "my message";
 final IdleStrategy idle = new SleepingIdleStrategy();
 final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(ByteBuffer.allocate(256));
 try (MediaDriver driver = MediaDriver.launch();
     Aeron aeron = Aeron.connect();
     Subscription sub = aeron.addSubscription(channel, 10);
     Publication pub = aeron.addPublication(channel, 10))
 {
     while (!pub.isConnected())
     {
         idle.idle();
     }
     unsafeBuffer.putStringAscii(0, message);
     System.out.println("sending:" + message);
     while (pub.offer(unsafeBuffer) < 0)
     {
         idle.idle();
     }
     FragmentHandler handler = (buffer, offset, length, header) ->
         System.out.println("received:" + buffer.getStringAscii(offset));
     while (sub.poll(handler, 1) <= 0)
     {
         idle.idle();
     }
 }
}

 执行结果

sending:my message
received:my message

二、剖析代码

构建基础支持对象定义

final String channel = "aeron:ipc";
final String message = "my message";
final IdleStrategy idle = new SleepingIdleStrategy();
final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(ByteBuffer.allocate(256));
  • 第 1 行指定 Aeron Publisher Subscriber进行通信的channel,基于 Aeron URL 方式定义,本例中的channel为 IPC channel,另请参阅 Aeron Wiki on channel configuration 的内容。
  • 第 2 行指定要通过publication发送的信息。
  • 第 3 行指定了要使用的空闲策略,在本例中,空闲轮询时,SleepingIdleStrategy 会将线程停等 1 us,默认值。
  • 第 4 行提供了一个堆外的不安全缓冲区,用于保存要发送的信息,为简单起见,分配了 256 字节,当然也可以减少。

    参考:Agents & Idle Strategies

 构建 Aeron 对象

try (MediaDriver driver = MediaDriver.launch();
 Aeron aeron = Aeron.connect();
 Subscription sub = aeron.addSubscription(channel, 10);
 Publication pub = aeron.addPublication(channel, 10))

接下来的四行代码将创建用于发送和接收数据的关键 Aeron 对象:

  • 第 1 行创建Media DriverMedia Driver负责所有 IPC网络活动,稍后将详细讨论;
  • 第 2 行创建 Aeron 对象。这是应用程序与 Aeron 交互的主要 API;
  • 第 3 行创建SubscriptionSubscription将被轮询以接收信息;
  • 第 4 行创建用于发送消息的 Publication

    参考:Media Driver

 发送消息

while (!pub.isConnected())
{
 idle.idle();
}
unsafeBuffer.putStringAscii(0, message);
System.out.println("sending:" + message);
while (pub.offer(unsafeBuffer) < 0)
{
 idle.idle();
}

这些代码负责发送消息;首先,应用程序等待publication连接完成;然后,将消息写入unsafeBuffer,最后将buffer提供给publication,关键代码描述如下:

  • 只有当publication连接后,第 1 行才返回 true。这是在一个 while 循环中进行轮询,每次轮询停等1us,直到连接成功;
  • 第 7 行向publication程序提供buffer,当返回的值小于零时,说明有什么东西阻止了publication接受缓冲区,空闲策略再次进行轮询,直到它被接受为止。

接收消息

FragmentHandler handler = (buffer, offset, length, header) ->
 System.out.println("received:" + buffer.getStringAscii(offset));

while (sub.poll(handler, 1) <= 0)
{
 idle.idle();
}

代码的最后一部分负责接受来自subscription的消息。首先,由 FragmentHandler 接收消息,然后对subscription进行轮询,直到消息由 Aeron 接收。关键行如下:

  • 第 1-2 行构建 FragmentHandler,在本例中,我们知道这是一条简单的小消息,因此可以使用最基本的 FragmentHandler 来接受消息,FragmentAssembler 应该用于重新组装较大的报文;
  • 第 4 行轮询subscription数据,与 publication 一样,数值等于或小于零具有特定含义;然后,代码轮询subscription,轮询空闲时间设置为1us。
  • 50
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值