The proper solution to your problem is to use pub.getNumSubscribers() and wait until that is > 0. Then publish.