use of org.apache.camel.CamelExchangeException in project camel by apache.
the class NettyProducer method process.
public boolean process(final Exchange exchange, AsyncCallback callback) {
if (!isRunAllowed()) {
if (exchange.getException() == null) {
exchange.setException(new RejectedExecutionException());
}
callback.done(true);
return true;
}
Object body;
try {
body = getRequestBody(exchange);
if (body == null) {
noReplyLogger.log("No payload to send for exchange: " + exchange);
callback.done(true);
return true;
}
} catch (Exception e) {
exchange.setException(e);
callback.done(true);
return true;
}
// set the exchange encoding property
if (getConfiguration().getCharsetName() != null) {
exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(getConfiguration().getCharsetName()));
}
if (LOG.isTraceEnabled()) {
LOG.trace("Pool[active={}, idle={}]", pool.getNumActive(), pool.getNumIdle());
}
// get a channel from the pool
Channel existing;
try {
existing = pool.borrowObject();
if (existing != null) {
LOG.trace("Got channel from pool {}", existing);
}
} catch (Exception e) {
exchange.setException(e);
callback.done(true);
return true;
}
// we must have a channel
if (existing == null) {
exchange.setException(new CamelExchangeException("Cannot get channel from pool", exchange));
callback.done(true);
return true;
}
if (exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT) != null) {
long timeoutInMs = exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT, Long.class);
ChannelHandler oldHandler = existing.getPipeline().get("timeout");
ReadTimeoutHandler newHandler = new ReadTimeoutHandler(getEndpoint().getTimer(), timeoutInMs, TimeUnit.MILLISECONDS);
if (oldHandler == null) {
existing.getPipeline().addBefore("handler", "timeout", newHandler);
} else {
existing.getPipeline().replace(oldHandler, "timeout", newHandler);
}
}
// need to declare as final
final Channel channel = existing;
final AsyncCallback producerCallback = new NettyProducerCallback(channel, callback);
// setup state as attachment on the channel, so we can access the state later when needed
channel.setAttachment(new NettyCamelState(producerCallback, exchange));
InetSocketAddress remoteAddress = null;
if (!isTcp()) {
// Need to specify the remoteAddress for udp connection
remoteAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort());
}
// write body
NettyHelper.writeBodyAsync(LOG, channel, remoteAddress, body, exchange, new ChannelFutureListener() {
public void operationComplete(ChannelFuture channelFuture) throws Exception {
LOG.trace("Operation complete {}", channelFuture);
if (!channelFuture.isSuccess()) {
// no success then exit, (any exception has been handled by ClientChannelHandler#exceptionCaught)
return;
}
// if we do not expect any reply then signal callback to continue routing
if (!configuration.isSync()) {
try {
// should channel be closed after complete?
Boolean close;
if (ExchangeHelper.isOutCapable(exchange)) {
close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
} else {
close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class);
}
// should we disconnect, the header can override the configuration
boolean disconnect = getConfiguration().isDisconnect();
if (close != null) {
disconnect = close;
}
if (disconnect) {
if (LOG.isTraceEnabled()) {
LOG.trace("Closing channel when complete at address: {}", getEndpoint().getConfiguration().getAddress());
}
NettyHelper.close(channel);
}
} finally {
// signal callback to continue routing
producerCallback.done(false);
}
}
}
});
// continue routing asynchronously
return false;
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class ClientChannelHandler method channelClosed.
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
if (LOG.isTraceEnabled()) {
LOG.trace("Channel closed: {}", ctx.getChannel());
}
Exchange exchange = getExchange(ctx);
AsyncCallback callback = getAsyncCallback(ctx);
// remove state
producer.removeState(ctx.getChannel());
// to keep track of open sockets
producer.getAllChannels().remove(ctx.getChannel());
// this channel is maybe closing graceful and the exchange is already done
// and if so we should not trigger an exception
boolean doneUoW = exchange.getUnitOfWork() == null;
if (producer.getConfiguration().isSync() && !doneUoW && !messageReceived && !exceptionHandled) {
// To avoid call the callback.done twice
exceptionHandled = true;
// and could not return a response. We should count down to stop waiting for a response
if (LOG.isDebugEnabled()) {
LOG.debug("Channel closed but no message received from address: {}", producer.getConfiguration().getAddress());
}
exchange.setException(new CamelExchangeException("No response received from remote server: " + producer.getConfiguration().getAddress(), exchange));
// signal callback
callback.done(false);
}
// make sure the event can be processed by other handlers
super.channelClosed(ctx, e);
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class OpenShiftProducer method doStart.
protected void doStart(Exchange exchange, IDomain domain) throws CamelExchangeException {
String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
if (name == null) {
throw new CamelExchangeException("Application not specified", exchange);
}
IApplication app = domain.getApplicationByName(name);
if (app == null) {
throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
} else {
app.start();
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class OpenShiftProducer method doRemoveEnvironmentVariable.
protected void doRemoveEnvironmentVariable(Exchange exchange, IDomain domain) throws CamelExchangeException {
String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
if (name == null) {
throw new CamelExchangeException("Application not specified", exchange);
}
IApplication app = domain.getApplicationByName(name);
if (app == null) {
throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
} else {
String variableName = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_NAME, getEndpoint().getApplication(), String.class);
if (!app.canGetEnvironmentVariables()) {
throw new CamelExchangeException("The application with id " + name + " can't get Environment Variables", exchange);
}
if (ObjectHelper.isNotEmpty(variableName)) {
app.removeEnvironmentVariable(variableName);
exchange.getIn().setBody(variableName);
} else {
throw new CamelExchangeException("Environment variable name not specified", exchange);
}
}
}
use of org.apache.camel.CamelExchangeException in project camel by apache.
the class OpenShiftProducer method doGetDeploymentType.
protected void doGetDeploymentType(Exchange exchange, IDomain domain) throws CamelExchangeException {
String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
if (name == null) {
throw new CamelExchangeException("Application not specified", exchange);
}
IApplication app = domain.getApplicationByName(name);
if (app == null) {
throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
} else {
String deploymentType = app.getDeploymentType();
exchange.getIn().setBody(deploymentType);
}
}
Aggregations