저는 그루비 스크립트에 초보자입니다. 그러나 튜토리얼을 따라 가면 parseClass()
메소드 GroovyClassLoader
을 사용하여 자바 코드 내에서 그루비 클래스를 동적으로로드하려고했습니다. 나는 약간의 발췌 문장을 썼다. 그리고 그것은 나를 위해 잘 근무했다. 문제는 내가 그루비 엔진이 내 관점을 넘어서 무엇을하는지, 그리고 그 스크립트가 어떻게 컴파일되는지를 명확하게 이해하지 못한다는 것입니다.
새 클래스가 jvm에로드되고로드됩니까? 아니면 내 애플리케이션이 캐시 된 소스를 사용합니까?GroovyClassLoading Mechanom
private static class MyScript {
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("public class SomeClass\n");
builder.append("{\n");
builder.append("Some code...").append("\n");
builder.append("}\n");
return builder.toString();
}
(가) 나는 다음과 같이 build()
와로드 :
private Class MyGroovyBuilder {
private Script script = new Script();
public String build() throws TemplateCompilationException
//
String groovyText = script.toString();
//
CompilerConfiguration config = new CompilerConfiguration();
//
byte[] bytes;
try
{
bytes = groovyText.getBytes(config.getSourceEncoding());
}
catch (UnsupportedEncodingException e)
{
throw new TemplateCompilationException(e, groovyText);
}
//
InputStream in = new ByteArrayInputStream(bytes);
GroovyCodeSource gcs = new GroovyCodeSource(in, "SomeName", "/groovy/shell");
GroovyClassLoader loader = new
GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
Class<?> scriptClass;
try
{
scriptClass = loader.parseClass(gcs, false);
}
catch (CompilationFailedException e)
{
throw new GroovyCompilationException(e, "SomeName", groovyText);
}
catch (ClassFormatError e)
{
throw new GroovyCompilationException(e, "SomeName", groovyText);
}
return scriptClass.getName();
}
모든 설명이 greatelly
BR.
안녕 @Seaagull, 설명을 주셔서 감사합니다,하지만의 깊은하자 나는 두 개의 노드로 일부 clusterd의 environement를 구성하고 난에서 해당 개체의 인스턴스를 만든거야 가정하면 ** 그런 다음 node1 **은 ** node2 **에 복제하려고했습니다. 두 번째 노드가이 클래스의 소스를 보유하지 않으므로 복제 프로세스가 확실히 실패합니다. 그렇지 않습니다. 이런 경우 어떻게 처리해야합니까? – tmarwen
@tmarwen 자, 더 자세히 살펴 보도록하겠습니다;) 분리 된 VM이 있기 때문에로드 된 클래스를 각각 공유해야합니다. 복제의 경우 클래스 인스턴스를 직렬화하여 두 번째 노드로 전송 한 다음 다시 직렬화 할 수 있습니다. – Seagull
이 작업은 수동으로 수행해야합니다. 이는 일부 공유 리소스 메커니즘을 구현하기 위해 다시 나에게 돌아가는 것을 의미합니다. 이러한 클래스 복제를 쉽게 할 수있는 기본 기능은 없습니다. – tmarwen