实习笔记 11:ZeroMQ研究 win7 X64+visual studio 2010+java server+python client

连续搞了两天晚上,最后差点转投jeroMQ,对于C++,Java项目编译各种尝试。

配置:

1. 下载ZeroMQ,编译,里面直接有v10的编译版本,生成 libzmq_d.dll

2. 下载JZMQ,注意里面是32位版本,转64的位话注意有个直接从32位中copy配置的。

3. JZMQ中加入jni路径等,参照 http://www.distream.org/?p=436http://zeromq.org/bindings:java

 

使用:

Java Server

public class wuserver {

    public static void main (String[] args) throws Exception {
        //  Prepare our context and publisher
        ZMQ.Context context = ZMQ.context(1);

        ZMQ.Socket publisher = context.socket(ZMQ.PUB);
        publisher.bind("tcp://*:5556");
//        publisher.bind("ipc://weather");

        //  Initialize random number generator
        Random srandom = new Random(System.currentTimeMillis());
        while (!Thread.currentThread ().isInterrupted ()) {
            //  Get values that will fool the boss
            int zipcode, temperature, relhumidity;
            zipcode = 10000 + srandom.nextInt(10000) ;
            temperature = srandom.nextInt(215) - 80 + 1;
            relhumidity = srandom.nextInt(50) + 10 + 1;

            //  Send message to all subscribers
            String update = String.format("%05d %d %d", zipcode, temperature, relhumidity);
            publisher.send(update, 0);
        }

        publisher.close ();
        context.term ();
    }
}

注意此处去掉 ipc,win7玩不了。

Python:

import sys
import zmq

#  Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

socket.connect ("tcp://localhost:5556")

# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"
socket.setsockopt(zmq.SUBSCRIBE, zip_filter)

# Process 5 updates
total_temp = 0
for update_nbr in range (5):
    string = socket.recv()
    zipcode, temperature, relhumidity = string.split()
    total_temp += int(temperature)

print "Average temperature for zipcode '%s' was %dF" % (
      zip_filter, total_temp / update_nbr)

 

注意wuserver运行的时候的配置,一定加入jar和dll的地址配置,以及path环境变量加入zero_mq编译的那个dll,有依赖,伤不起。

OK!

Marketdata(Publisher)+Stats(Subscriber)+Strategy(Subscriber) 搭建完成!

posted on 2013-09-08 16:36  surghost  阅读(589)  评论(0编辑  收藏  举报

导航