java - 在 JeroMQ 中将文件作为消息发送

标签 java zeromq jeromq

在 JeroMQ 中如何使用单个消息发送具有文件内容类型和其他属性的文件。

在客户端:

构建文件消息并发送到服务器

DataInputStream inStrm = file.getContent();
ZMsg msg = ZMsg.load(inStrm);
msg.send(sender);

有什么方法可以为消息设置属性吗?喜欢:

msg.setProperties("Content-Type", "application/xml");
msg.setProperties("fileName", "abc.pdf");

在服务器中,接收文件:

Poller items = new ZMQ.Poller (2);
items.register(receiver, ZMQ.Poller.POLLIN);
while (true) {
    try{
        items.poll();       
        if (items.pollin(0)) {
            ZMsg msg = ZMsg.recvMsg(receiver);
            //save file to disk
        }
    }catch(Exception e){
        LOG.error("Error while receive file: ", e);
    }
}

最佳答案

还有一个办法。 ZeroMq 有 Multipart-Messages

在我看来这非常有用。 在 jeromq/jzmq 库中,你可以这样使用它:

将文件中的数据存储在字节数组中。 制作一个多部分的 ZMsg,将您需要的所有标题和数据放入其中:

ZMsg outMsg = new ZMsg();
outMsg.add(new ZFrame("application/xml"));
outMsg.add(new ZFrame("abc.pdf"));
outMsg.add(new ZFrame(bytes)); // here is the data from file
outMsg.send(outSocket);

从另一个套接字接收 ZMsg 并从中获取所有数据:

ZMsg inMsg = ZMsg.recvMsg(inSocket);
String contentType = inMsg.pop().toString();
String fileName = inMsg.pop().toString();
byte[] fileData = inMsg.pop().getData();

或者您可以通过任何其他方便的方式来完成,通过在一个字节数组中序列化所有必要的 header 并仅使用两个帧等。

关于java - 在 JeroMQ 中将文件作为消息发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41249955/

相关文章:

java - gb mb kb 和 X 字节中的字节数,java

java - 为什么一个方法可以在另一个方法上调用?

java - 碧 Jade 报告 : Store in variable value of parameter bean property

c# - 失去网络连接时 ZMQ 发布-订阅程序失败

java - 子服务器套接字关闭时如何制作ZMQ pub客户端套接字缓冲区消息

java - 在 Jax-rs Web 服务中注入(inject) HttpResponse 时出现空指针异常

python - 在 zeromq/python 中使用 pyobj 子函数时设置主题

c++ - ZMQ C++ Req 到路由器问题

java - Android Java 上的 ZeroMQ ( Android Studio )

messaging - ZeroMq 路由器静默丢弃消息