Пример #1
0
 public Exchange createExchange(IoSession session, Object payload) {
   Exchange exchange = createExchange();
   exchange.getIn().setHeader(Mina2Constants.MINA_IOSESSION, session);
   exchange.getIn().setHeader(Mina2Constants.MINA_LOCAL_ADDRESS, session.getLocalAddress());
   exchange.getIn().setHeader(Mina2Constants.MINA_REMOTE_ADDRESS, session.getRemoteAddress());
   Mina2PayloadHelper.setIn(exchange, payload);
   return exchange;
 }
Пример #2
0
  @SuppressWarnings("deprecation")
  protected void doProcess(Exchange exchange) throws Exception {
    if (session == null && !lazySessionCreation) {
      throw new IllegalStateException("Not started yet!");
    }
    if (session == null || !session.isConnected()) {
      openConnection();
    }

    // set the exchange encoding property
    if (getEndpoint().getConfiguration().getCharsetName() != null) {
      exchange.setProperty(
          Exchange.CHARSET_NAME,
          IOConverter.normalizeCharset(getEndpoint().getConfiguration().getCharsetName()));
    }

    Object body = Mina2PayloadHelper.getIn(getEndpoint(), exchange);
    if (body == null) {
      noReplyLogger.log("No payload to send for exchange: " + exchange);
      return; // exit early since nothing to write
    }

    // if textline enabled then covert to a String which must be used for textline
    if (getEndpoint().getConfiguration().isTextline()) {
      body =
          getEndpoint()
              .getCamelContext()
              .getTypeConverter()
              .mandatoryConvertTo(String.class, exchange, body);
    }

    // if sync is true then we should also wait for a response (synchronous mode)
    if (sync) {
      // only initialize latch if we should get a response
      latch = new CountDownLatch(1);
      // reset handler if we expect a response
      ResponseHandler handler = (ResponseHandler) session.getHandler();
      handler.reset();
    }

    // log what we are writing
    if (LOG.isDebugEnabled()) {
      Object out = body;
      if (body instanceof byte[]) {
        // byte arrays is not readable so convert to string
        out = exchange.getContext().getTypeConverter().convertTo(String.class, body);
      }
      LOG.debug("Writing body: {}", out);
    }
    // write the body
    Mina2Helper.writeBody(session, body, exchange);

    if (sync) {
      // wait for response, consider timeout
      LOG.debug("Waiting for response using timeout {} millis.", timeout);
      boolean done = latch.await(timeout, TimeUnit.MILLISECONDS);
      if (!done) {
        throw new ExchangeTimedOutException(exchange, timeout);
      }

      // did we get a response
      ResponseHandler handler = (ResponseHandler) session.getHandler();
      if (handler.getCause() != null) {
        throw new CamelExchangeException(
            "Error occurred in ResponseHandler", exchange, handler.getCause());
      } else if (!handler.isMessageReceived()) {
        // no message received
        throw new ExchangeTimedOutException(exchange, timeout);
      } else {
        // set the result on either IN or OUT on the original exchange depending on its pattern
        if (ExchangeHelper.isOutCapable(exchange)) {
          Mina2PayloadHelper.setOut(exchange, handler.getMessage());
        } else {
          Mina2PayloadHelper.setIn(exchange, handler.getMessage());
        }
      }
    }
  }