INT-3162 Fix AbstractScriptExecutor Thread-Safety
JIRA: https://jira.springsource.org/browse/INT-3162
This commit is contained in:
committed by
Gary Russell
parent
a7a722a59e
commit
7b2ba3e0a3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
@@ -14,13 +14,14 @@ package org.springframework.integration.scripting.jsr223;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.SimpleBindings;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.integration.scripting.ScriptExecutor;
|
||||
import org.springframework.integration.scripting.ScriptingException;
|
||||
import org.springframework.scripting.ScriptSource;
|
||||
@@ -31,6 +32,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author David Turanski
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
* @since 2.1
|
||||
*/
|
||||
abstract class AbstractScriptExecutor implements ScriptExecutor {
|
||||
@@ -66,18 +68,18 @@ abstract class AbstractScriptExecutor implements ScriptExecutor {
|
||||
Object result = null;
|
||||
|
||||
try {
|
||||
if (variables != null) {
|
||||
for (Entry<String, Object> entry : variables.entrySet()) {
|
||||
scriptEngine.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
String script = scriptSource.getScriptAsString();
|
||||
Date start = new Date();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("executing script: " + script);
|
||||
}
|
||||
|
||||
result = scriptEngine.eval(script);
|
||||
if (variables != null) {
|
||||
result = scriptEngine.eval(script, new SimpleBindings(variables));
|
||||
}
|
||||
else {
|
||||
result = scriptEngine.eval(script);
|
||||
}
|
||||
|
||||
result = postProcess(result, scriptEngine, script);
|
||||
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:script="http://www.springframework.org/schema/integration/scripting"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:script="http://www.springframework.org/schema/integration/scripting"
|
||||
xmlns:task="http://www.springframework.org/schema/task"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/scripting http://www.springframework.org/schema/integration/scripting/spring-integration-scripting.xsd">
|
||||
http://www.springframework.org/schema/integration/scripting http://www.springframework.org/schema/integration/scripting/spring-integration-scripting.xsd
|
||||
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
|
||||
|
||||
<transformer input-channel="referencedScriptInput">
|
||||
<script:script
|
||||
lang="ruby"
|
||||
lang="ruby"
|
||||
location="org/springframework/integration/scripting/config/jsr223/Jsr223TransformerTests.rb"/>
|
||||
</transformer>
|
||||
|
||||
@@ -19,4 +21,23 @@
|
||||
]]></script:script>
|
||||
</transformer>
|
||||
|
||||
<task:executor id="executor" pool-size="100"/>
|
||||
|
||||
<channel id="int3162InputChannel">
|
||||
<dispatcher task-executor="executor"/>
|
||||
</channel>
|
||||
|
||||
<channel id="int3162OutputChannel">
|
||||
<queue/>
|
||||
</channel>
|
||||
|
||||
<transformer input-channel="int3162InputChannel" output-channel="int3162OutputChannel">
|
||||
<script:script lang="javascript"><![CDATA[
|
||||
(function(){
|
||||
return payload;
|
||||
})();
|
||||
]]></script:script>
|
||||
</transformer>
|
||||
|
||||
|
||||
</beans:beans>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,9 @@ package org.springframework.integration.scripting.config.jsr223;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@@ -26,12 +29,15 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.PollableChannel;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
* @since 2.0
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@@ -44,6 +50,12 @@ public class Jsr223TransformerTests {
|
||||
@Autowired
|
||||
private MessageChannel inlineScriptInput;
|
||||
|
||||
@Autowired
|
||||
private MessageChannel int3162InputChannel;
|
||||
|
||||
@Autowired
|
||||
private PollableChannel int3162OutputChannel;
|
||||
|
||||
|
||||
@Test
|
||||
public void referencedScript() {
|
||||
@@ -73,4 +85,22 @@ public class Jsr223TransformerTests {
|
||||
assertNull(replyChannel.receive(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInt3162ScriptExecutorThreadSafety() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
this.int3162InputChannel.send(new GenericMessage<Object>(i));
|
||||
}
|
||||
|
||||
Set<Object> result = new HashSet<Object>();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Message<?> message = this.int3162OutputChannel.receive(1000);
|
||||
result.add(message.getPayload());
|
||||
}
|
||||
|
||||
assertEquals(100, result.size());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user