*
* @param timeout time to wait when reading from data queue. A value of -1
* indicates a blocking read.
*/
public Exchange receive(long timeout) {
DataQueue queue = endpoint.getDataQueue();
try {
DataQueueEntry entry;
if (timeout >= 0) {
int seconds = (int)timeout / 1000;
if (log.isTraceEnabled()) {
log.trace("Reading from data queue: " + queue.getName() + " with " + seconds + " seconds timeout");
}
entry = queue.read(seconds);
} else {
if (log.isTraceEnabled()) {
log.trace("Reading from data queue: " + queue.getName() + " with no timeout");
}
entry = queue.read(-1);
}
Exchange exchange = new DefaultExchange(endpoint.getCamelContext());
if (entry != null) {
if (endpoint.getFormat() == Format.binary) {
exchange.getIn().setBody(entry.getData());
} else {
exchange.getIn().setBody(entry.getString());
}
return exchange;
}
} catch (AS400SecurityException e) {
throw new RuntimeCamelException("Unable to read from data queue: " + queue.getName(), e);
} catch (ErrorCompletingRequestException e) {
throw new RuntimeCamelException("Unable to read from data queue: " + queue.getName(), e);
} catch (IOException e) {
throw new RuntimeCamelException("Unable to read from data queue: " + queue.getName(), e);
} catch (IllegalObjectTypeException e) {
throw new RuntimeCamelException("Unable to read from data queue: " + queue.getName(), e);
} catch (InterruptedException e) {
throw new RuntimeCamelException("Unable to read from data queue: " + queue.getName(), e);
} catch (ObjectDoesNotExistException e) {
throw new RuntimeCamelException("Unable to read from data queue: " + queue.getName(), e);
}
return null;
}