Skip to content

Commit

Permalink
camel-core - Using shared dummy exchange instance for special needs i… (
Browse files Browse the repository at this point in the history
#14616)

* camel-core - Using shared dummy exchange instance for special needs instead of creating a new instance to be thrown away afterwards.
  • Loading branch information
davsclaus authored Jun 22, 2024
1 parent 7a316a0 commit 1a6a166
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,12 @@ public void run() {
Object result = null;

try {
// create dummy exchange
Exchange dummy = ExchangeHelper.createCopy(exchange, true);

task = dummy.getIn().getMandatoryBody(String.class);
// create copy of exchange to not cause side effect
Exchange copy = ExchangeHelper.createCopy(exchange, true);
task = copy.getIn().getMandatoryBody(String.class);
if (task != null) {
Expression exp = language.createExpression(task);
result = exp.evaluate(dummy, Object.class);
result = exp.evaluate(copy, Object.class);
}

if (result != null && !getEndpoint().isAsync()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.component.file.GenericFileProcessStrategy;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
Expand Down Expand Up @@ -217,7 +218,7 @@ private FTPFile[] pollNamedFile() {
FTPFile[] files = null;
// we cannot use the LIST command(s) so we can only poll a named
// file so created a pseudo file with that name
Exchange dummy = endpoint.createExchange();
Exchange dummy = ExchangeHelper.getDummy(getEndpoint().getCamelContext());
String name = evaluateFileExpression(dummy);
if (name != null) {
FTPFile file = new FTPFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.apache.camel.component.file.GenericFileProcessStrategy;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
Expand Down Expand Up @@ -216,7 +217,7 @@ private SftpRemoteFile[] pollNamedFile() {

// we cannot use the LIST command(s) so we can only poll a named
// file so created a pseudo file with that name
Exchange dummy = endpoint.createExchange();
Exchange dummy = ExchangeHelper.getDummy(getEndpoint().getCamelContext());
String name = evaluateFileExpression(dummy);
if (name != null) {
SftpRemoteFile file = new SftpRemoteFileSingle(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.camel.Processor;
import org.apache.camel.RollbackExchangeException;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.ExchangeFactory;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.support.ScheduledBatchPollingConsumer;
import org.apache.camel.util.CastUtils;
Expand All @@ -50,6 +51,7 @@ public class SqlConsumer extends ScheduledBatchPollingConsumer {

private final String query;
private String resolvedQuery;
private final ExchangeFactory exchangeFactory;
private final JdbcTemplate jdbcTemplate;
private final NamedParameterJdbcTemplate namedJdbcTemplate;
private final SqlParameterSource parameterSource;
Expand Down Expand Up @@ -84,6 +86,7 @@ public SqlConsumer(DefaultSqlEndpoint endpoint, Processor processor, JdbcTemplat
this.parameterSource = null;
this.sqlPrepareStatementStrategy = sqlPrepareStatementStrategy;
this.sqlProcessingStrategy = sqlProcessingStrategy;
this.exchangeFactory = endpoint.getCamelContext().getCamelContextExtension().getExchangeFactory();
}

public SqlConsumer(DefaultSqlEndpoint endpoint, Processor processor, NamedParameterJdbcTemplate namedJdbcTemplate,
Expand All @@ -96,6 +99,7 @@ public SqlConsumer(DefaultSqlEndpoint endpoint, Processor processor, NamedParame
this.parameterSource = parameterSource;
this.sqlPrepareStatementStrategy = sqlPrepareStatementStrategy;
this.sqlProcessingStrategy = sqlProcessingStrategy;
this.exchangeFactory = endpoint.getCamelContext().getCamelContextExtension().getExchangeFactory();
}

@Override
Expand Down Expand Up @@ -129,7 +133,7 @@ protected int poll() throws Exception {
shutdownRunningTask = null;
pendingExchanges = 0;

final Exchange dummy = getEndpoint().createExchange();
final Exchange dummy = exchangeFactory.create(getEndpoint(), true);
final String preparedQuery
= sqlPrepareStatementStrategy.prepareQuery(resolvedQuery, getEndpoint().isAllowNamedParameters(), dummy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.RouteTemplateContext;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.ExchangeFactory;
import org.apache.camel.spi.Language;
import org.apache.camel.spi.ScriptingLanguage;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.ScriptHelper;
import org.apache.camel.util.StringHelper;
Expand Down Expand Up @@ -78,16 +78,10 @@ public static Object newInstance(BeanFactoryDefinition def, CamelContext context
bindings.put("context", context);
target = slan.evaluate(def.getScript(), bindings, clazz);
} else {
// exchange based languages needs a dummy exchange to be evaluated
ExchangeFactory ef = context.getCamelContextExtension().getExchangeFactory();
Exchange dummy = ef.create(false);
try {
String text = ScriptHelper.resolveOptionalExternalScript(context, dummy, def.getScript());
Expression exp = lan.createExpression(text);
target = exp.evaluate(dummy, clazz);
} finally {
ef.release(dummy);
}
Exchange dummy = ExchangeHelper.getDummy(context);
String text = ScriptHelper.resolveOptionalExternalScript(context, dummy, def.getScript());
Expression exp = lan.createExpression(text);
target = exp.evaluate(dummy, clazz);
}

// a bean must be created
Expand Down Expand Up @@ -215,9 +209,8 @@ public static void bind(BeanFactoryDefinition<?> def, RouteTemplateContext route
// and memorize so the script is only evaluated once and the local bean is the same
// if a route template refers to the local bean multiple times
routeTemplateContext.bind(def.getName(), clazz, Suppliers.memorize(() -> {
ExchangeFactory ef = camelContext.getCamelContextExtension().getExchangeFactory();
Exchange dummy = ef.create(false);
try {
Exchange dummy = ExchangeHelper.getDummy(camelContext);
String text = ScriptHelper.resolveOptionalExternalScript(camelContext, dummy, script);
if (text != null) {
Expression exp = lan.createExpression(text);
Expand All @@ -242,8 +235,6 @@ public static void bind(BeanFactoryDefinition<?> def, RouteTemplateContext route
} catch (Exception e) {
throw new IllegalStateException(
"Cannot create bean: " + def.getType(), e);
} finally {
ef.release(dummy);
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.camel.processor.ChoiceProcessor;
import org.apache.camel.processor.FilterProcessor;
import org.apache.camel.spi.ExpressionFactoryAware;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.ExchangeHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -100,7 +100,7 @@ private void initBranch(WhenDefinition whenClause) {
*/
private Processor getMatchingBranchProcessor() throws Exception {
// evaluate when predicates to optimize
Exchange dummy = new DefaultExchange(camelContext);
Exchange dummy = ExchangeHelper.getDummy(camelContext);
for (WhenDefinition whenClause : definition.getWhenClauses()) {
ExpressionDefinition exp = whenClause.getExpression();
exp.initPredicate(camelContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public class ExchangeHelperTest extends ContextTestSupport {

protected Exchange exchange;

@Test
public void testGetDummy() {
Exchange one = ExchangeHelper.getDummy(context);
Exchange two = ExchangeHelper.getDummy(context);
assertSame(one, two);
assertNotSame(exchange, one);
assertNotSame(exchange, two);
}

@Test
public void testValidProperty() throws Exception {
String value = ExchangeHelper.getMandatoryProperty(exchange, "foo", String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,25 @@ public final class ExchangeHelper {
= ObjectHelper.getSystemProperty(Exchange.DEFAULT_CHARSET_PROPERTY, "UTF-8");
private static final Charset DEFAULT_CHARSET = Charset.forName(DEFAULT_CHARSET_NAME);

private static Exchange DUMMY;

/**
* Utility classes should not have a public constructor.
*/
private ExchangeHelper() {
}

/**
* Gets a singleton dummy exchange used for special purposes only.
*/
public static Exchange getDummy(CamelContext context) {
// we dont worry about thread-safety
if (DUMMY == null) {
DUMMY = new DefaultExchange(context);
}
return DUMMY;
}

/**
* Extracts the Exchange.BINDING of the given type or null if not present
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.URISupport;

public final class RawParameterHelper {
Expand All @@ -42,7 +42,7 @@ private RawParameterHelper() {
public static void resolveRawParameterValues(CamelContext camelContext, Map<String, Object> parameters) {
URISupport.resolveRawParameterValues(parameters, s -> {
if (s != null && s.contains("$simple{")) {
Exchange dummy = new DefaultExchange(camelContext);
Exchange dummy = ExchangeHelper.getDummy(camelContext);
s = camelContext.resolveLanguage("simple").createExpression(s).evaluate(dummy, String.class);
}
return s;
Expand Down

0 comments on commit 1a6a166

Please sign in to comment.