0

以下の技術スタックを使用してアプリケーションを開発しました。

Struts 2.1 
Springs 3.1 
Groovy 1.8 
Tomcat 6.0.26 - For deployment 

私たちの要件は、アプリケーションの特定のレイヤーを Groovy に移行することです。そのレイヤーを groovy に移行し、アプリケーションは正常に動作しています。移行後、Groovy Bean は Spring コンテナーによって適切に初期化されますが、Groovy の「動的言語」機能を利用できません。つまり、Groovy スクリプトを変更すると、更新された変更がアプリケーションに動的に反映されません。

参考までに、groovy スクリプト (GroovyConnection.groovy) のメソッドを添付します。

@Component 
class GroovyConnection implements EndPointManager, Serializable{ 
private static final long serialVersionUID = 1481681752777429674L; 
private final static Logger log = Logger.getLogger(GroovyConnection.class); 
private static final String DESTINATION_DIR = ARTIFACT_DIR_PATH.toString()+"/"; 
private static final String JIVE_USER = "";  //"jivescpt"; 
/** 
* @param default 
*/ 
public GroovyConnection() { 
    super(); 
    jschInstance = new JSch(); 
} 

public String executeCommands(Session session, String inputCommand, Device device) { 
log.info("Entering executeCommand() device:" + inputCommand); 
Channel channel = null; 
PipedOutputStream commandIO = null; 
InputStream sessionInput = null; 
InputStream sessionOutput = null; 
InputStream sessionError = null; 
String commandResponse = null; 
try { 
    channel = session.openChannel("shell"); 
    commandIO = new PipedOutputStream(); 
    sessionInput = new PipedInputStream(commandIO); 
    channel.setInputStream(sessionInput); 
    sessionOutput = channel.getInputStream(); 
    sessionError = channel.getExtInputStream(); 
    channel.connect(); 

    return fireCommandsOnTerminal(session, commandIO, sessionOutput, sessionError, inputCommand, device); 
} 
catch (JSchException e) { 
    log.error(AutomationConstants.ERROR_MSG.toString(), e); 
} 
catch (IOException e) { 
    log.error(AutomationConstants.ERROR_MSG.toString(), e); 
} 
catch (InterruptedException e) { 
    log.error(AutomationConstants.ERROR_MSG.toString(), e); 
} 
finally { 
    boolean isClosed = GroovyConnectionHelper.closeAll(session, channel, commandIO, sessionInput, sessionOutput, sessionError); 
    if (!isClosed) { 
        return AutomationUtils.getPropertyValue("jive.vpn.error.msg"); 
    } 
} 
log.info("Exiting executeCommand() device:" + inputCommand); 
return commandResponse; 
} 
} 

Groovy コンテキスト ファイル:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
<lang:defaults refresh-check-delay="2000"/>
<lang:groovy id="endPointManager" 
             script-source="classpath:GroovyConnection.groovy" refresh-check-delay="1000">
</lang:groovy>
</beans>

1 秒間の更新チェック遅延時間を指定した後でも、Groovy Bean は実行時に適切に動作しません。更新された Groovy の変更を再デプロイするには、Tomcat を再起動する必要があります。Groovy Bean を Java Bean として扱うアプリケーションのように見えます。

これとは別に、Springs を使用せずに groovy スクリプトを実行すると、期待どおりに実行されます。修正された groovy スクリプトの変更は、動的に反映されます。参照用のコードは次のとおりです。

グルーヴィーなスクリプト:

package com
public class test1 {
def hello_world() {
    println "Connectddded"
    }
}

メインクラス:

public static void main(String arg[]) {
 try {
        new GroovyShell().parse(newFile("test1.groovy")).invokeMethod("hello_world", null);              
    } catch (CompilationFailedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

私の理解によると、Groovy Bean の変更をリフレッシュできる Spring-Groovy Integration 構成の何かが欠けているか、Tomcat が更新された Groovy スクリプトをリフレッシュすることを許可していません。

誰かがこれに関する提案を提供できる場合は役に立ちます。前もって感謝します。

よろしく、 Divya Garg

4

1 に答える 1

1

時間を割いて私の投稿を読んでくれたすべての人に感謝します。問題は解決しました。

実際、@component と groovy-context.xml ファイルを同時に使用していたため、この問題に直面していました。注釈を削除し、groovy ファイルをクラスパスに配置しました。これで、すべてが期待どおりに機能するようになりました。Groovy Bean が適切に更新されます。しかし、春の注釈を使用して同じことを達成することはできませんでした。

今、私は別の問題に直面しており、そのために多くのことを試みました。(GroovyConnectionHelper.groovy) と呼ばれるすべての静的メソッドがある別の groovy Bean があります。GroovyConnection.script でこれらの静的メソッドを呼び出すことができません。アプリケーションが実行されると、実行が GroovyConnectionHelper 静的メソッドに到達する場所でアプリケーションがハングします。

問題を解決するために、次のように GroovyConnection に GroovyConnectionHelper Bean を挿入しようとしました (これは良い方法ではありません)。

groovy-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:lang="http://www.springframework.org/schema/lang"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
    <lang:defaults refresh-check-delay="1000"/>
    <lang:groovy name="endPointManager" script-source="classpath:GroovyConnection.groovy">
         <lang:property name="groovyConnectionHelper" ref="groovyConnectionHelper"/>
    </lang:groovy>

    <lang:groovy id="groovyConnectionHelper" script-source="classpath:GroovyConnectionHelper.groovy"/>
</beans>

そして、GroovyConnection.groovy に同じセッターを配置します。

public void setGroovyConnectionHelper(GroovyConnectionHelper groovyConnectionHelper) {
    System.out.println( "Setting ----------------------------------------- groovyConnectionHelper : " + groovyConnectionHelper  );
    if ( null != groovyConnectionHelper ){
        System.out.println( "Object groovyConnectionHelper: ::: " + groovyConnectionHelper.dump() );
    }
    this.groovyConnectionHelper = groovyConnectionHelper;
} 

しかし、これはうまくいきません。このエラーに直面しています:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scripting.groovy.GroovyScriptFactory#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptedObject.org.springframework.scripting.groovy.GroovyScriptFactory#0': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'groovyConnectionHelper' of bean class [GroovyConnection]: Bean property 'groovyConnectionHelper' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 71 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptedObject.org.springframework.scripting.groovy.GroovyScriptFactory#0': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'groovyConnectionHelper' of bean class [GroovyConnection]: Bean property 'groovyConnectionHelper' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1396)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.aop.target.dynamic.BeanFactoryRefreshableTargetSource.obtainFreshBean(BeanFactoryRefreshableTargetSource.java:77)
    at org.springframework.scripting.support.RefreshableScriptTargetSource.obtainFreshBean(RefreshableScriptTargetSource.java:79)
    at org.springframework.aop.target.dynamic.BeanFactoryRefreshableTargetSource.freshTarget(BeanFactoryRefreshableTargetSource.java:66)
    at org.springframework.aop.target.dynamic.AbstractRefreshableTargetSource.refresh(AbstractRefreshableTargetSource.java:97)
    at org.springframework.aop.target.dynamic.AbstractRefreshableTargetSource.getTargetClass(AbstractRefreshableTargetSource.java:68)
    at org.springframework.scripting.support.ScriptFactoryPostProcessor.createRefreshableProxy(ScriptFactoryPostProcessor.java:553)
    at org.springframework.scripting.support.ScriptFactoryPostProcessor.postProcessBeforeInstantiation(ScriptFactoryPostProcessor.java:322)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:880)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:852)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:446)
    ... 79 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'groovyConnectionHelper' of bean class [GroovyConnection]: Bean property 'groovyConnectionHelper' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:924)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
    ... 94 more

これにGroovy Bean Injectionを使用するか、Groovy静的メソッドを直接呼び出すことができるかを提案してください。前もって感謝します

よろしく、 Divya Garg

于 2013-01-25T15:01:25.917 に答える