Problem starting a new JRuby ScriptingContainer and setting Output & Error PrintStreams
I am embedding JRuby using Red Bridge into an app and seem to have problems setting out and err. My tests use a PrintStream
that writes to a StringBuilder
, after doing some stuff with the JRuby, I then check the stuff printed to out with puts
. The problem is only the first setOutput
works everyting gets printed to that.
The simple sample below demonstrates the problem. It is using PrintStream
s that write to different files to demonstrate the problem. Looking at your file system will show that everything goes to the first "file" when one would expect files 2 and 3 to also be written.
package XXX.jruby;
import java.io.PrintStream;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;
public开发者_如何学JAVA class JRubyShellPutsProblem {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 3; i++) {
final ScriptingContainer container = new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.PERSISTENT);
// container.resetWriter();
// container.resetErrorWriter();
final PrintStream printStream = new PrintStream("d:\\out" + i + ".txt");
container.setOutput(printStream);
container.setError(printStream);
container.runScriptlet("puts \"hello" + i + "\"\n");
printStream.flush();
container.terminate();
}
}
}
I've tried ScriptingContainer.resetWriter
but that does not work as one would expect. Sometimes stuff gets written to files 1 and 2 but nothing to 3.
I changed LocalContextScope to THREADSAFE from CONCURRENT and this seemed to fix the problem.
精彩评论