1

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);
    }
}
4

1 に答える 1

3

別の方法があります。ZeroMq にはマルチパート メッセージがあります

私の意見では、それは非常に便利です。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();

または、必要なすべてのヘッダーを 1 つのバイト配列にシリアル化し、2 つのフレームのみを使用するなど、他の便利な方法で行うこともできます。

于 2016-12-21T16:53:26.647 に答える