zeromq/jzmq 基于信封-内容的pub/sub发布订阅java代码

pub:

package com.zeromq.test.psen_pub_sub;

import org.zeromq.ZMQ;

/**
 * @author 吕桂强
 * @email [email protected]
 * @version 创建时间:2012-4-27 下午5:56:05
 */
public class psenvpub {

	public static void main(String[] args) throws InterruptedException {
		ZMQ.Context context = ZMQ.context(1);
		ZMQ.Socket publisher = context.socket(ZMQ.PUB);
		publisher.bind("tcp://*:5563");
		while (true) {
			// 以信封-内容的形式写入2条消息
			publisher.send("A".getBytes(), ZMQ.SNDMORE);
			publisher.send("ok!".getBytes(), 0);
			Thread.sleep(1000);
		}

	}
}

sub:

package com.zeromq.test.psen_pub_sub;

import org.zeromq.ZMQ;

/**
 * @author 吕桂强
 * @email [email protected]
 * @version 创建时间:2012-4-27 下午6:02:30
 */
public class psenvsub {

	public static void main(String[] args) {

		ZMQ.Context context = ZMQ.context(1);
		ZMQ.Socket subscriber = context.socket(ZMQ.SUB);

		subscriber.connect("tcp://localhost:5563");
		subscriber.subscribe("We would like to see this".getBytes());
		while (true) {
			// 读取信封内容
			// 读取内容
			String address = new String(subscriber.recv(0));
			String contents = new String(subscriber.recv(0));
			System.out.println(address + " : " + contents);
		}
	}
}



你可能感兴趣的:(java,String,tcp,Class,email)