groovy - Hybris -> 如何使用执行 beanshell 脚本创建 cronjob?

标签 groovy beanshell sap-commerce-cloud

我有一个 beanshell 脚本,我希望它能够自动化。 我需要创建 cronjob 并将我的 beanshell 代码添加到其中。 也许有人这么认为? 有谁知道该怎么做? 或者如何将我的脚本与 cronjob 匹配?

最佳答案

答案其实在Cronjob scripting中给出了文档。

简而言之,您将使用动态脚本来创建 cronjob,而不是使用缓慢的老式方法。


概念

使用了三个新对象(引用自文档):

  • Script - the item type where the script content is stored (a separate deployment table)
  • ScriptingJob - a new ServicelayerJob item, which contains additionally the scriptURI (consequently, the stored script can be found at runtime from different locations (classpath, db, etc..)
  • ScriptingJobPerformable - the spring bean assigned to every ScriptingJob instance; it implements the usual perform() method (like for any other cronjob). This is where the "scripted" cronjob logic is executed

如何使用 Cronjob 脚本

第一步 - 将您的脚本实例保存在数据库中。对于此示例,我使用了来自 http://www.beanshell.org/manual/quickstart.html 的代码

Note: you can create script with Groovy, BeanShell ad JavaScript

INSERT_UPDATE Script; code[unique=true];content;scriptType(code)
;myBshScript;"foo = ""Foo"";    
four = (2 + 2)*2/2;
print( foo + "" = "" + four );  // print() is a BeanShell command

// Do a loop
for (i=0; i<5; i++)
    print(i);   

// Pop up a frame with a button in it
button = new JButton( ""My Button"" );
frame = new JFrame( ""My Frame"" );
frame.getContentPane().add( button, ""Center"" );
frame.pack();
frame.setVisible(true);";BEANSHELL

Don't forget to escape " in the script with an other " (Impex restriction).

第二步 - 创建一个使用之前创建的脚本的 ScriptingJob

INSERT_UPDATE ScriptingJob; code[unique=true];scriptURI
;myBshDynamicJob;model://myBshScript

model://myBshScript is used to retrieve a Script stored in the DB

第三步 - 创建 CronJob

INSERT_UPDATE CronJob; code[unique=true];job(code);singleExecutable;sessionLanguage(isocode)
;myBshDynamicCronJob;myBshDynamicJob;true;en

可选步骤 - 为 CronJob 创建触发器

INSERT_UPDATE Trigger;cronjob(code)[unique=true];cronExpression
;myBshDynamicCronJob;0 0 0/1 1/1 * ? *

This executes the cronjob every hour.

通过脚本执行 CronJob

在 hac 脚本选项卡中,选择 Groovy 并在提交模式下运行它。

def dynamicCJ = cronJobService.getCronJob("myBshDynamicCronJob")
cronJobService.performCronJob(dynamicCJ,true)

运行脚本后你应该显示这个

enter image description here

在控制台中

INFO  [hybrisHTTP27] (myBshDynamicCronJob) [ScriptingJobPerformable] Foo = 4
0
1
2
3
4

关于groovy - Hybris -> 如何使用执行 beanshell 脚本创建 cronjob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40238760/

相关文章:

groovy - 在 Groovy 控制台中使用 Geb

jmeter - 如何在JMeter中使用beanshell获取机器IP地址?

Linux、BASH 带有特殊字符的启动命令

spring - 在已运行的 Hybris 服务器中执行集成测试

date - Grails "Unparseable Date"错误

Groovy 枚举构造函数匹配

validation - 是否可以在 Grails 之外使用 Grails 验证?如何?

iteration - jmeter 停止当前迭代

jmeter - 没有方法签名 : org. apache.jmeter.threads.JMeterVariables.put() 适用于参数类型 : (java. lang.String, java.lang.Integer)

java - HybrisContextFactory - 初始化全局应用程序上下文时出错