在 Wildfly CLI 脚本中,可以测试资源的存在并执行一些条件逻辑:
if (outcome != success) of /subsystem=datasources/xa-data-source=MY_DATASOURCE:read-resource
// now do something
当您运行 cli 时,您可以像这样提供脚本中可用的属性:
$JBOSS_HOME/bin/jboss-cli.sh --file=my.cli --properties=my.properties
我想引入一些基于这些属性的条件逻辑来做一些条件配置。
一个例子是配置邮件服务器,有时邮件服务器需要用户名和密码,而在其他情况下它允许匿名访问。在设置邮件服务器配置时,我希望能够有这样的条件逻辑
if MAILSERVER_USERNAME is defined //confiure mailserver with username password else // configure mailserver without attributes username/password
我能看到的唯一可能的事情是,如果我已经在我的 standalone-full.xml 中添加了系统属性设置,我可以像这样查询它:
if (outcome != success) of /system-property=foo:read-resource
// now do something
我想根据从 my.properties 传入的属性做一些类似的事情。
这可能吗?
谢谢,
丹尼尔
最佳答案
您通过 --properties
传递的属性不能在每个上下文中使用,这使得测试它们的存在很麻烦:
[standalone@localhost] :resolve-expression(expression=$myProperty)
Unrecognized variable myProperty
[standalone@localhost] :resolve-expression(expression=${myProperty})
{
"outcome" => "failed",
"failure-description" => "WFLYCTL0211: Cannot resolve expression '${myProperty}'",
"rolled-back" => true
}
不过,您可以通过在 cli 脚本中使用 set
命令来解决该问题:
[standalone@localhost] set myProperty=${myProperty}
[standalone@localhost] :resolve-expression(expression=$myProperty)
{
"outcome" => "success",
"result" => "myValue"
}
然后您可以使用 resolve-expression
命令的结果来测试您的属性是否存在:
[standalone@localhost] if (outcome == success) of :resolve-expression(expression=$myProperty)
[standalone@localhost] echo success ! myProperty is set ( $myProperty )
[standalone@localhost] end-if
success ! myProperty is set ( myValue )
如果您的属性未定义,尝试使用 resolve-expression
解析它会引发错误:
[standalone@localhost] set myProperty=${notMyProperty}
[standalone@localhost] :resolve-expression(expression=$myProperty)
{
"outcome" => "failed",
"failure-description" => "WFLYCTL0211: Cannot resolve expression '${notMyProperty}'",
"rolled-back" => true
}
关于jboss - 在 Wildfly-CLI 脚本中为 if/else 逻辑使用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53187833/