消息过滤不起作用 - 无法理解代码有什么问题。这根本不会检索任何消息。但是,如果我使用(“。”。getBytes()),它工作正常并返回发布的所有msgs。我正在使用JZMQ2.1.3.jar。请建议。谢谢!
public class OMQSubFilter {
public static void main (String[] args) {
// Prepare our context and subscriber
Context context = ZMQ.context(1);
Socket subscriber = context.socket(ZMQ.SUB);
// System.out.print("Subscriber started");
subscriber.connect("tcp://localhost:5556");
// Subscribe to keywords, default is All
String twtFilter = (args.length > 0) ? args[0] : "";
subscriber.subscribe(twtFilter.trim().getBytes());
System.out.print("Filter: "+ twtFilter.trim());
while (!Thread.currentThread ().isInterrupted ()) {
byte[] tweet = subscriber.recv(0);
String str = new String(tweet);
//String str = subscriber.recvStr(0).trim();
System.out.print(str);
}
subscriber.close ();
context.term ();
}
}
以下是发布商代码:
public class OMQPub
{
public static void main (String[] args)
{
Context zcontext = ZMQ.context(1);
Socket pub = zcontext.socket(ZMQ.PUB);
pub.bind("tcp://*:5556");
java.util.List<TweetStatus> tweets = ct.getTweets();
Iterator<TweetStatus> iter = tweets.iterator();
String tweetStr = "";
try {
Thread.sleep (1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (iter.hasNext()) {
TweetStatus tweet = iter.next();
tweetStr = Utility.jsonFormat(tweet, ct.getKeywords());
System.out.println(" TweetStr : "+tweetStr);
pub.send(tweetStr.getBytes(), ZMQ.NOBLOCK);
}
//pub.close();
//zcontext.term(); //zcontext.term() hangs even all the sockets created in the context are closed.
}
}