Aeron基本的发布/订阅对在同一主机上连接成功,在不同主机间失败。
创始人
2024-07-29 08:30:23
0

要解决"Aeron基本的发布/订阅对在同一主机上连接成功,在不同主机间失败"的问题,可以使用以下代码示例:

首先,需要在发布者端和订阅者端分别创建Aeron实例。在同一主机上,可以使用相同的Aeron目录。

发布者端代码示例:

import io.aeron.Aeron;
import io.aeron.Publication;
import org.agrona.concurrent.BackoffIdleStrategy;
import org.agrona.concurrent.IdleStrategy;
import org.agrona.concurrent.SleepingIdleStrategy;

public class Publisher {
    public static void main(String[] args) {
        String aeronDirectoryName = "/path/to/aeron/directory";
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(aeronDirectoryName));

        String channel = "aeron:udp?endpoint=localhost:40123";
        int streamId = 1;
        Publication publication = aeron.addPublication(channel, streamId);

        IdleStrategy idleStrategy = new BackoffIdleStrategy(1, 10, 100, 1000);
        while (true) {
            long result = publication.offer(messageToSend());
            if (result > 0) {
                // 消息已成功发送
            } else if (result == Publication.CLOSED || result == Publication.NOT_CONNECTED) {
                // 连接已关闭或未连接
                break;
            } else {
                // 暂时无法发送消息,稍后重试
                idleStrategy.idle();
            }
        }

        aeron.close();
    }

    private static String messageToSend() {
        // 构造要发送的消息
        return "Hello, Aeron!";
    }
}

订阅者端代码示例:

import io.aeron.Aeron;
import io.aeron.Subscription;
import io.aeron.logbuffer.FragmentHandler;
import org.agrona.concurrent.BackoffIdleStrategy;
import org.agrona.concurrent.IdleStrategy;
import org.agrona.concurrent.SleepingIdleStrategy;

public class Subscriber {
    public static void main(String[] args) {
        String aeronDirectoryName = "/path/to/aeron/directory";
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(aeronDirectoryName));

        String channel = "aeron:udp?endpoint=localhost:40123";
        int streamId = 1;
        Subscription subscription = aeron.addSubscription(channel, streamId);

        FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
            // 处理收到的消息
            byte[] data = new byte[length];
            buffer.getBytes(offset, data);

            String message = new String(data);
            System.out.println("Received message: " + message);
        };

        IdleStrategy idleStrategy = new BackoffIdleStrategy(1, 10, 100, 1000);
        while (true) {
            int fragmentsRead = subscription.poll(fragmentHandler, 1);
            if (fragmentsRead > 0) {
                // 处理了一些消息
            } else if (fragmentsRead == 0) {
                // 没有收到新消息
                idleStrategy.idle();
            } else if (fragmentsRead == Subscription.CLOSED) {
                // 连接已关闭
                break;
            }
        }

        aeron.close();
    }
}

在代码示例中,需要将/path/to/aeron/directory替换为实际的Aeron目录路径,localhost:40123替换为实际的主机和端口。

发布者端通过publication.offer(messageToSend())发送消息,订阅者端通过subscription.poll(fragmentHandler, 1)接收消息。在发布者端,如果offer()返回正数,表示消息已成功发送;如果返回Publication.CLOSEDPublication.NOT_CONNECTED,表示连接已关闭或未连接;如果返回负数,表示暂时无法发送消息,稍后重试。在订阅者端,poll()返回大于0表示处理了一些消息,返回0表示没有收到新消息,返回Subscription.CLOSED表示连接已关闭。

通过以上示例代码,可以实现Aeron基本的发布/订阅对在同一主机上连接成功,在不同主机间也能成功连接。

相关内容

热门资讯

Android Studio ... 要解决Android Studio 4无法检测到Java代码,无法打开SDK管理器和设置的问题,可以...
安装tensorflow mo... 要安装tensorflow models object-detection软件包和pandas的每个...
安装了Laravelbackp... 检查是否创建了以下自定义文件并进行正确的配置config/backpack/base.phpconf...
安装了centos后会占用多少... 安装了CentOS后会占用多少内存取决于多个因素,例如安装的软件包、系统配置和运行的服务等。通常情况...
按照Laravel方式通过Pr... 在Laravel中,我们可以通过定义关系和使用查询构建器来选择模型。首先,我们需要定义Profile...
按照分类ID显示Django子... 在Django中,可以使用filter函数根据分类ID来筛选子类别。以下是一个示例代码:首先,假设你...
Android Studio ... 要给出包含代码示例的解决方法,我们可以使用Markdown语法来展示代码。下面是一个示例解决方案,其...
Android Retrofi... 问题描述:在使用Android Retrofit进行GET调用时,获取的响应为空,即使服务器返回了正...
Alexa技能在返回响应后出现... 在开发Alexa技能时,如果在返回响应后出现问题,可以按照以下步骤进行排查和解决。检查代码中的错误处...
Airflow Dag文件夹 ... 要忽略Airflow中的笔记本检查点,可以在DAG文件夹中使用以下代码示例:from airflow...