001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.model; 018 019import java.util.concurrent.ExecutorService; 020 021import javax.xml.bind.annotation.XmlAccessType; 022import javax.xml.bind.annotation.XmlAccessorType; 023import javax.xml.bind.annotation.XmlAttribute; 024import javax.xml.bind.annotation.XmlRootElement; 025import javax.xml.bind.annotation.XmlTransient; 026 027import org.apache.camel.Expression; 028import org.apache.camel.builder.ExpressionBuilder; 029import org.apache.camel.model.language.ExpressionDefinition; 030import org.apache.camel.spi.Metadata; 031 032/** 033 * Delays processing for a specified length of time 034 */ 035@Metadata(label = "eip,routing") 036@XmlRootElement(name = "delay") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class DelayDefinition extends ExpressionNode implements ExecutorServiceAwareDefinition<DelayDefinition> { 039 040 @XmlTransient 041 private ExecutorService executorService; 042 @XmlAttribute 043 private String executorServiceRef; 044 @XmlAttribute 045 @Metadata(defaultValue = "true") 046 private Boolean asyncDelayed; 047 @XmlAttribute 048 @Metadata(defaultValue = "true") 049 private Boolean callerRunsWhenRejected; 050 051 public DelayDefinition() { 052 } 053 054 public DelayDefinition(Expression delay) { 055 super(delay); 056 } 057 058 @Override 059 public String getShortName() { 060 return "delay"; 061 } 062 063 @Override 064 public String getLabel() { 065 return "delay[" + getExpression() + "]"; 066 } 067 068 @Override 069 public String toString() { 070 return "Delay[" + getExpression() + " -> " + getOutputs() + "]"; 071 } 072 073 // Fluent API 074 // ------------------------------------------------------------------------- 075 076 /** 077 * Sets the delay time in millis to delay 078 * 079 * @param delay delay time in millis 080 * @return the builder 081 */ 082 public DelayDefinition delayTime(Long delay) { 083 setExpression(ExpressionNodeHelper.toExpressionDefinition(ExpressionBuilder.constantExpression(delay))); 084 return this; 085 } 086 087 /** 088 * Whether or not the caller should run the task when it was rejected by the 089 * thread pool. 090 * <p/> 091 * Is by default <tt>true</tt> 092 * 093 * @param callerRunsWhenRejected whether or not the caller should run 094 * @return the builder 095 */ 096 public DelayDefinition callerRunsWhenRejected(boolean callerRunsWhenRejected) { 097 setCallerRunsWhenRejected(callerRunsWhenRejected); 098 return this; 099 } 100 101 /** 102 * Enables asynchronous delay which means the thread will <b>not</b> block 103 * while delaying. 104 */ 105 public DelayDefinition asyncDelayed() { 106 setAsyncDelayed(true); 107 return this; 108 } 109 110 /** 111 * Enables asynchronous delay which means the thread will <b>not</b> block 112 * while delaying. 113 */ 114 public DelayDefinition syncDelayed() { 115 setAsyncDelayed(false); 116 return this; 117 } 118 119 /** 120 * To use a custom Thread Pool if asyncDelay has been enabled. 121 */ 122 @Override 123 public DelayDefinition executorService(ExecutorService executorService) { 124 setExecutorService(executorService); 125 return this; 126 } 127 128 /** 129 * Refers to a custom Thread Pool if asyncDelay has been enabled. 130 */ 131 @Override 132 public DelayDefinition executorServiceRef(String executorServiceRef) { 133 setExecutorServiceRef(executorServiceRef); 134 return this; 135 } 136 137 // Properties 138 // ------------------------------------------------------------------------- 139 140 /** 141 * Expression to define how long time to wait (in millis) 142 */ 143 @Override 144 public void setExpression(ExpressionDefinition expression) { 145 // override to include javadoc what the expression is used for 146 super.setExpression(expression); 147 } 148 149 public Boolean getAsyncDelayed() { 150 return asyncDelayed; 151 } 152 153 public void setAsyncDelayed(Boolean asyncDelayed) { 154 this.asyncDelayed = asyncDelayed; 155 } 156 157 public Boolean getCallerRunsWhenRejected() { 158 return callerRunsWhenRejected; 159 } 160 161 public void setCallerRunsWhenRejected(Boolean callerRunsWhenRejected) { 162 this.callerRunsWhenRejected = callerRunsWhenRejected; 163 } 164 165 @Override 166 public ExecutorService getExecutorService() { 167 return executorService; 168 } 169 170 @Override 171 public void setExecutorService(ExecutorService executorService) { 172 this.executorService = executorService; 173 } 174 175 @Override 176 public String getExecutorServiceRef() { 177 return executorServiceRef; 178 } 179 180 @Override 181 public void setExecutorServiceRef(String executorServiceRef) { 182 this.executorServiceRef = executorServiceRef; 183 } 184}