0

我希望该方法等到侦听器的事件发生。我应该如何实现这一目标?在我的程序中,我想循环直到事件的结果满足某些条件,否则,我将无休止地发送请求

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的新手,谁能帮帮我?谢谢你!

4

1 回答 1

0

创建一个设置为 1 的 CountDownLatch 并在主应用程序中等待它。然后,您可以在满足条件时让 onMessage 方法对闩锁进行倒计时。这还允许您在等待调用上设置超时值,以便您的应用程序可以唤醒并检查其是否需要关闭或执行其他工作等。

于 2013-04-14T10:14:26.193 回答