如何使用org.jboss.varia.property.SystemPropertiesService和org.jboss.util.property.PropertyListener

时间:2013-01-29 07:23:23

标签: jboss jboss-4.2.x

所有

我见过jboss-service.xml使用扩展SystemPropertiesService类来引用自定义属性文件。但我还没有完全理解这种用法​​。 有人可以帮我理解如何使用这两个课程吗?感谢。

2 个答案:

答案 0 :(得分:2)

SystemPropertiesService非常有用于定义可以从应用程序访问的属性,它通常用于参数化应用程序而无需更改代码,甚至是应用程序包(前提是将jboss-service.xml放在外部de war / ear / jar结构)。例如,您可以使用以下内容创建myapp-service.xml文件:

<server>
 <mbean code="org.jboss.varia.property.SystemPropertiesService" name="jboss:type=Service,name=MyAppProperties">
 <!-- Define the properties directly in the service.xml file-->
 <attribute name="Properties">
     myapp.property1=property1Value
     myapp.property2=property2Value
 </attribute>
 <!-- You can also specify a route to another file where you define properties-->
 <attribute name="URLList">
     /home/myuser/txtlist.properties
 </attribute>
 </mbean>
</server>

然后你可以直接在JBoss中部署这个文件,定义的属性对同一个JBoss中部署的所有应用程序都是可见的,你可以使用静态方法访问它们:

String System.getProperty(String propertyName)

因此,如果您想要从您的应用程序访问myapp.property1的值,请执行以下操作:

String property = System.getProperty("myapp.property");

另一方面,PropertyListener实际上是一个接口,它定义了一个侦听器,当一个属性发生任何事件时将触发该侦听器。 org.jboss.util.property.PropertyAdapter是此接口的抽象实现。要使用它,您必须实现其三个方法(propertyAdded,propertyChanged,propertyRemoved),当分别添加,更改或删除属性时,容器将调用这三个方法。这些方法将PropertyEvent对象作为参数,让您知道受影响的属性。

当您希望应用程序在每次属性更改时执行某些操作时,此接口/类非常有用(错误的实现将是您每隔一定时间检查一次属性更改),这样,当JBoss检测到属性有更改了它的值,它将调用相应的方法(您应该使用您想要的行为实现)。

例如,如果要在每次更改时打印新属性值,可以通过以下方式实现propertyChanged方法:

void propertyChanged (PropertyEvent pe){
    // check the property that has changed
    if (pe.getPropertyName().equals("myapp.property1")){
         System.out.println("The value of " + pe.getPropertyName() + " has changed to " + pe.getPropertyValue());
    }
}

API以及PropertyAdapterPropertyEvent中查找更多信息。

答案 1 :(得分:0)

在JBOSS 5.1中,只有当你将属性或URL放在properties-service.xml中时,它才有用,而且这个文件应该放在jboss.home / server / default / deploy目录下。