纯java ZeroMQ 客户端?

发布于 2024-09-14 02:01:18 字数 144 浏览 15 评论 0原文

我正在尝试使用 ZeroMQ 的 pub sub 消息传递,但客户端要求代码全部为 Java。我知道 ZeroMQ 有一个 Java 绑定,但仍然依赖于 ac 库,因此我无法使用它。是否有我可以用来连接到服务器的 ZeroMQ 客户端,或者实现是否足够简单,我可以自己完成?

I am trying to use ZeroMQ's pub sub messaging, but the client side requires the code to be all Java. I understand that ZeroMQ has a Java binding but that still relies on a c library, therefore I am unable to use it. Is there a ZeroMQ client I can use to connect to the server, or is the implementation simple enough for me to do myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

┈┾☆殇 2024-09-21 02:01:18

我正在研究纯java ZeroMQ。

https://github.com/miniway/jeromq

即使它还处于早期阶段,可能不太适合现在的生产使用情况。

但我希望它能有所帮助,因为我是满怀热情地做的。

I'm working on pure java ZeroMQ.

https://github.com/miniway/jeromq

Even it is very early stage and might not quite fit for production usage right now.

But I hope it would help, as I made it with a passion.

屌丝范 2024-09-21 02:01:18

Zeromq 网站在以下页面上声称支持 Android:

http://www.zeromq.org/distro:android

其中包括为 android 预构建的 Zeromq 压缩包,使用 NDK(本机开发工具包)将 Java 应用程序与标准 C 库桥接起来。

The zeromq web site claims support for android on the follow page:

http://www.zeromq.org/distro:android

which includes a tarball of zeromq pre-built for android, using the NDK (Native Development Kit) which bridges Java apps to standard c libraries.

舟遥客 2024-09-21 02:01:18

截至今天,我认为可以肯定地假设您的问题的答案是

纯 Java 客户端需要使用与本机 C++ 实现相同的有线协议;该协议的文档尚未完成,我上次检查时已在 0MQ 开发人员 TODO 列表中。

As of today, I think it is safe to assume that the answer to your question is no.

A pure Java client would need to use the same wire protocol as the native C++ implementation; documenting this protocol has not been done yet and was on the 0MQ developers TODO list last time I checked.

我的黑色迷你裙 2024-09-21 02:01:18

注意:我假设您已成功安装 ZeroMQ 和 google protoc

我有一个示例,其中我使用 google protocol buffer 从 ZeroMQ 发布者向订阅者发送一些消息。

test.proto 文件

option java_package = "com.example.tutorial"; 
option java_outer_classname = "TestProtos";

message Test {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

现在首先编译它与 protoc 编译器一样

$protoc -I=. --java_out=. test.proto

所以这将在当前目录中生成 TestProtos.java 文件
/com/example/tutorial 文件夹

--------------------------------协议结束缓冲步骤--------------------------

发布者代码是
文件名:Publisher.java

import org.zeromq.ZMQ;
import com.example.tutorial.TestProtos.Test;

public class Publisher {
    public static void main (String[] args) {
        ZMQ.Context context = ZMQ.context(1);
        ZMQ.Socket publisher = context.socket(ZMQ.PUB);

        // Subscriber tells us when it's ready here
        ZMQ.Socket sync = context.socket(ZMQ.PULL);

        sync.bind("tcp://*:5561");

        // We send updates via this socket
        publisher.bind("tcp://*:5562");

        System.out.println("Publisher Running");

        // Wait for synchronization request
        sync.recv(0);

        Test testzmq =
              Test.newBuilder()
                .setId(1234)
                .setName("Pritam Kharat")
                .setEmail("[email protected]")
                .build();

        long start = System.currentTimeMillis ();
        int request_nbr;
        for (request_nbr = 0; request_nbr != 10; request_nbr++) {
            //System.out.println(request_nbr);
            publisher.send(testzmq.toByteArray(), 0); //serialization
        }
        long end = System.currentTimeMillis ();
        long diff = (end - start);
        System.out.println("time taken to send messages "+ request_nbr +" is :" +diff);
    }
}

订阅者代码是
文件名:Subscriber.java

import org.zeromq.ZMQ;
import com.example.tutorial.TestProtos.Test;


public class Subscriber {
    public static void main (String[] args) {
        ZMQ.Context context = ZMQ.context(1);

        // Connect our subscriber socket
        ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
        subscriber.setIdentity("hello".getBytes());

        // Synchronize with the publisher
        ZMQ.Socket sync = context.socket(ZMQ.PUSH);

        subscriber.subscribe("".getBytes());
        subscriber.connect("tcp://localhost:5562");
        sync.connect("tcp://localhost:5561");

        sync.send("".getBytes(), 0);

        long start = System.currentTimeMillis ();
        int request_nbr;
        for (request_nbr = 0; request_nbr != 10; request_nbr++) {
            byte[] rawBytes = subscriber.recv(0);
            try{
                Test data = Test.parseFrom(rawBytes); //deserialization
             //   System.out.println(data);
            }
            catch ( Exception e ) {
            }
        }
        long end = System.currentTimeMillis ();
        long diff = (end - start);
        System.out.println("time taken to receive messages "+ request_nbr +" is :" +diff);
    }
}

这就是全部..您已经完成了代码..
现在只需运行这些发布者和订阅者代码..

Note : I am assuming that you have installed ZeroMQ and google protoc successfully

I have one example where I am sending some messages from ZeroMQ Publisher to Subscriber using google protocol buffer..

test.proto file is

option java_package = "com.example.tutorial"; 
option java_outer_classname = "TestProtos";

message Test {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

now first compile it with protoc compiler as

$protoc -I=. --java_out=. test.proto

So this will generate TestProtos.java file in your current directories
/com/example/tutorial folder

---------------------------------end of protocol buffer steps--------------------------

Publisher code is
file name : Publisher.java

import org.zeromq.ZMQ;
import com.example.tutorial.TestProtos.Test;

public class Publisher {
    public static void main (String[] args) {
        ZMQ.Context context = ZMQ.context(1);
        ZMQ.Socket publisher = context.socket(ZMQ.PUB);

        // Subscriber tells us when it's ready here
        ZMQ.Socket sync = context.socket(ZMQ.PULL);

        sync.bind("tcp://*:5561");

        // We send updates via this socket
        publisher.bind("tcp://*:5562");

        System.out.println("Publisher Running");

        // Wait for synchronization request
        sync.recv(0);

        Test testzmq =
              Test.newBuilder()
                .setId(1234)
                .setName("Pritam Kharat")
                .setEmail("[email protected]")
                .build();

        long start = System.currentTimeMillis ();
        int request_nbr;
        for (request_nbr = 0; request_nbr != 10; request_nbr++) {
            //System.out.println(request_nbr);
            publisher.send(testzmq.toByteArray(), 0); //serialization
        }
        long end = System.currentTimeMillis ();
        long diff = (end - start);
        System.out.println("time taken to send messages "+ request_nbr +" is :" +diff);
    }
}

Subscriber Code is
file name : Subscriber.java

import org.zeromq.ZMQ;
import com.example.tutorial.TestProtos.Test;


public class Subscriber {
    public static void main (String[] args) {
        ZMQ.Context context = ZMQ.context(1);

        // Connect our subscriber socket
        ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
        subscriber.setIdentity("hello".getBytes());

        // Synchronize with the publisher
        ZMQ.Socket sync = context.socket(ZMQ.PUSH);

        subscriber.subscribe("".getBytes());
        subscriber.connect("tcp://localhost:5562");
        sync.connect("tcp://localhost:5561");

        sync.send("".getBytes(), 0);

        long start = System.currentTimeMillis ();
        int request_nbr;
        for (request_nbr = 0; request_nbr != 10; request_nbr++) {
            byte[] rawBytes = subscriber.recv(0);
            try{
                Test data = Test.parseFrom(rawBytes); //deserialization
             //   System.out.println(data);
            }
            catch ( Exception e ) {
            }
        }
        long end = System.currentTimeMillis ();
        long diff = (end - start);
        System.out.println("time taken to receive messages "+ request_nbr +" is :" +diff);
    }
}

That is all..you are done with your code..
Now just run these Publisher and subscriber codes..

岁月染过的梦 2024-09-21 02:01:18

目前还没有 0mq 的纯 Java 实现。正如你所说,有一个 Java 语言绑定,但它需要一个本机库才能运行。实现该库的纯 Java 替代品并非易事。

There is currently no pure Java implementation of 0mq. As you say, there is a Java language binding but it requires a native library to function. A pure Java replacement for the library would not be trivial to implement.

在风中等你 2024-09-21 02:01:18

http://rfc.zeromq.org/spec 上有一个关于 ZeroMQ 使用的线路协议的简短规范: 13..这样您至少应该能够编写代码的协议部分。您仍然需要编写处理 ZeroMQ 套接字之类的部分。

There is a short spec on the wire protocol used by ZeroMQ at http://rfc.zeromq.org/spec:13. With that you should at least be able to write the protocol portions of the code. You would still need to write the parts that deal with ZeroMQ sockets and whatnot.

风月客 2024-09-21 02:01:18

javazmq 项目于 2011 年 8 月启动,声称要用纯 Java 实现 ZeroMQ 的子集。目前不确定其完整性。

The javazmq project started in Aug of 2011 claims to implement a subset of ZeroMQ in pure Java. Not sure of the completeness of it at this point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文