TimeoutGuardPipe extends FixedForwardPipe { @Override public PipeRunResult doPipe(Message message, IPipeLineSession session) throws PipeRunException { ParameterValueList pvl = null; if (getParameterList() != null) { try { pvl = getParameterList().getValues(message, session); } catch (ParameterException e) { throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e); } } int timeout_work; String timeout_work_str = getParameterValue(pvl, "timeout"); if (timeout_work_str == null) { timeout_work = getTimeout(); } else { timeout_work = Integer.valueOf(timeout_work_str); } log.debug(getLogPrefix(session) + "setting timeout of [" + timeout_work + "] s"); TimeoutGuard tg = new TimeoutGuard(timeout_work, getName()) { @Override protected void kill() { killPipe(); } }; try { return doPipeWithTimeoutGuarded(message, session); } catch (Exception e) { String msg = e.getClass().getName(); if (isThrowException()) { throw new PipeRunException(this, msg, e); } else { String msgString = msg + ": " + e.getMessage(); log.error(msgString, e); String msgCdataString = "<![CDATA[" + msgString + "]]>"; Message errorMessage = new Message("<error>" + msgCdataString + "</error>"); return new PipeRunResult(getForward(), errorMessage); } } finally { if(tg.cancel()) { String msgString = "TimeOutException"; Exception e = new TimeOutException("exceeds timeout of [" + timeout_work + "] s, interupting"); if (isThrowException()) { throw new PipeRunException(this, msgString, e); } else { log.error(msgString, e); String msgCdataString = "<![CDATA[" + msgString + ": "+ e.getMessage() + "]]>"; Message errorMessage = new Message("<error>" + msgCdataString + "</error>"); return new PipeRunResult(getForward(), errorMessage); } } } }  @Override PipeRunResult doPipe(Message message, IPipeLineSession session); abstract PipeRunResult doPipeWithTimeoutGuarded(Message input, IPipeLineSession session); @IbisDoc({"when <code>true</code>, a piperunexception is thrown. otherwise the output is only logged as an error (and returned in a xml string with 'error' tags)", "true"}) void setThrowException(boolean b); boolean isThrowException(); int getTimeout(); @IbisDoc({"timeout in seconds of obtaining a result", "30"}) void setTimeout(int i);  }
@Test public void doesNotTimeout() throws Exception { configureAndStartPipe(); Message input = new Message("500"); PipeRunResult result = doPipe(input); assertEquals(SUCCESS_MESSAGE, result.getResult().asString()); }
@Test public void doesTimeoutWithException() throws Exception { configureAndStartPipe(); Message input = new Message("1500"); try { doPipe(input); fail("an exception should occur!"); } catch(PipeRunException e) { assertTrue(e.getCause() instanceof TimeOutException); } }