Example #1
0
  /**
   * Make a specific request to the underlying transport
   *
   * @param endpoint the endpoint to use when connecting to the resource
   * @param timeout the maximum time the operation should block before returning. The call should
   *     return immediately if there is data available. If no data becomes available before the
   *     timeout elapses, null will be returned
   * @return the result of the request wrapped in a UMOMessage object. Null will be returned if no
   *     data was avaialable
   * @throws Exception if the call to the underlying protocal cuases an exception
   */
  protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception {

    Session session = null;
    Destination dest = null;
    MessageConsumer consumer = null;
    try {
      boolean topic = false;
      String resourceInfo = endpoint.getEndpointURI().getResourceInfo();
      topic = (resourceInfo != null && JmsConstants.TOPIC_PROPERTY.equalsIgnoreCase(resourceInfo));

      session = connector.getSession(false, topic);
      dest =
          connector
              .getJmsSupport()
              .createDestination(session, endpoint.getEndpointURI().getAddress(), topic);
      consumer = connector.getJmsSupport().createConsumer(session, dest, topic);

      try {
        Message message = null;
        if (timeout == RECEIVE_NO_WAIT) {
          message = consumer.receiveNoWait();
        } else if (timeout == RECEIVE_WAIT_INDEFINITELY) {
          message = consumer.receive();
        } else {
          message = consumer.receive(timeout);
        }
        if (message == null) {
          return null;
        }

        message = connector.preProcessMessage(message, session);

        return new MuleMessage(connector.getMessageAdapter(message));
      } catch (Exception e) {
        connector.handleException(e);
        return null;
      }
    } finally {
      connector.closeQuietly(consumer);
      connector.closeQuietly(session);
    }
  }