我希望该方法等到侦听器的事件发生。我应该如何实现这一目标?在我的程序中,我想循环直到事件的结果满足某些条件,否则,我将无休止地发送请求
public void run(double r, double sigma, double strike, double s0, String t, int d) throws JMSException, InterruptedException {
double actureError = 0.0;
double epsilon = 0.00001;
// iterate until the relative error rate is less than requested (converged)
while ((actureError < epsilon) || (actureError > ESTIMATION_ERROR)) {
sendRequest(r, sigma, strike, s0, t, d);
Topic topic = session.createTopic("topic");
MessageConsumer consumer = session.createConsumer(topic);
OptionMessageListener listener = new OptionMessageListener(task);
consumer.setMessageListener(listener);
double absoluteError = y * task.getSigma()
/ Math.sqrt(task.getCount());
actureError = absoluteError / (task.getAvg() + epsilon);
}
return task.getAvg() * Math.exp(-r * 252);
}
public void sendRequest(double r, double sigma, double strike, double s0, String t, int d) throws JMSException {
// send request to the client
MessageProducer producer = session.createProducer(queue);
OptionRequest request = new OptionRequest(r, sigma, strike, s0, t, d, null);
ObjectMessage requestMessage = session.createObjectMessage(request);
producer.send(requestMessage);
}
public class OptionMessageListener implements MessageListener {
private Task task;
public OptionMessageListener(Task task) {
this.task = task;
}
@Override
public void onMessage(Message message) {
if (message instanceof ObjectMessage) {
ObjectMessage objectMessage = (ObjectMessage) message;
try {
Result result = (Result) objectMessage.getObject();
double payout = result.getPayout();
task.addPayout(payout);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
我是activeMQ的新手,谁能帮帮我?谢谢你!