コード例 #1
0
  /**
   * @param channel name to subscribe to
   * @param listener callback for messages received
   * @param timetoken the time since the last read from the server. This value is usually sent with
   *     a result from the subscribe RESt method on the server.
   */
  private void doSubscribe(String channel, MessageListener listener, String timetoken) {
    while (true) {
      try {
        // Build URL
        List<String> url =
            java.util.Arrays.asList("subscribe", this.subscribeKey, channel, "0", timetoken);

        // Wait for Message
        JsonNode response = doRequest(url);

        JsonNode messages = response.get(0);

        // Update TimeToken
        if (response.get(1).getTextValue().length() > 0) {
          timetoken = response.get(1).getTextValue();
        }

        // Run user Callback and Reconnect if user permits. If
        // there's a timeout then messages.length() == 0.
        for (int i = 0; messages.size() > i; i++) {
          JsonNode message = messages.get(i);
          if (!listener.onMessage(message)) {
            return;
          }
        }
      } catch (Exception e) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException ignored) {
        }
      }
    }
  }
コード例 #2
0
 private boolean notifyOnMessage(
     MessageListener listener, ServerSession from, ServerMessage message) {
   try {
     return listener.onMessage(this, from, message);
   } catch (Throwable x) {
     _logger.info("Exception while invoking listener " + listener, x);
     return true;
   }
 }
コード例 #3
0
ファイル: Session.java プロジェクト: huten10/prax
 @SuppressWarnings("unchecked")
 private Message onMessage(Message message) {
   Map<String, MessageListener> listeners =
       AppContext.getApplicationContext().getBeansOfType(MessageListener.class);
   for (MessageListener listener : listeners.values()) {
     if (listener.support(message)) {
       return listener.onMessage(message);
     }
   }
   return null;
 }
コード例 #4
0
  /**
   * Performs an atomic {@link #receiveNoWait()} and dispatch to the listener within the STOP_GUARD
   * which ensures that the consumer cannot be stopped until the listener has completed processing
   * of the message.
   *
   * @param listener the listener to which to dispatch
   * @return true if a message was dispatched, false otherwise
   * @throws JMSException
   */
  protected boolean receiveAndDispatch(BlockingQueue<byte[]> queue, MessageListener listener)
      throws JMSException {
    synchronized (STOP_GUARD) {
      Message msg = receive(queue, 0);

      if (msg != null) {
        listener.onMessage(msg);
        return true;
      } else {
        return false;
      }
    }
  }
コード例 #5
0
 public void notify(Message message) {
   log(message);
   if (!message.isTransient()) {
     // put the newest messages up front; old messages are at the end
     this.messages.addFirst(message);
     if (messages.size() > maxMessages) {
       messages
           .removeLast(); // we should only have 1 extra, so removeLast should remove all extras
     }
   }
   for (MessageListener listener : listeners) {
     listener.onMessage(message);
   }
 }
コード例 #6
0
ファイル: MessageHandler.java プロジェクト: ozsofts/ozsofts
  public String handleMessage(WXAccount wxaccount, String reqdata) {
    Map<String, String> msgData = parseRequestXml(reqdata);

    String msgType = msgData.get("MsgType");

    try {
      Class<?> clazz = receiveMessageMap.get(msgType);
      if (clazz == null) {
        log.error("[{}] 处理消息时对应的类型[{}]没有解析器!", wxaccount.getSystemId(), msgType);
        return "";
      }

      ReceiveMessage receiveMessage = (ReceiveMessage) clazz.newInstance();
      // 对上行的消息进行解析
      receiveMessage.parse(msgData);

      // 将上行的消息调用每一个侦听器对象
      String messageType = receiveMessage.getMsgType();
      List<MessageListener> messageListenerList =
          new LinkedList<MessageListener>(this.commonMessageListenerList);
      if (this.messageListenerMap.containsKey(messageType)) {
        messageListenerList.addAll(this.messageListenerMap.get(messageType));
      }

      ResponseMessage respMessage = null;
      for (MessageListener listener : messageListenerList) {
        respMessage = listener.onMessage(wxaccount, receiveMessage);
        // 注意,如果有多个侦听器关注一个类型的消息话,以第一个有返回的为主,后续的侦听器将不再起作用
        if (respMessage != null) {

          // 保存下行消息
          WXMessage.dao.saveResponseMessage(wxaccount.getId(), respMessage);
          // 返回下行消息
          return respMessage.toXML();
        }
      }
    } catch (Exception e) {
      log.error("[{}] 处理消息时未正确初始化[{}]", wxaccount.getSystemId(), msgType);
      return "";
    }

    // 回复此字符串表示服务器已经处理,微信不需要再重发相同的消息
    return "success";
  }