How to use org.jboss.varia.property.SystemProperti

2019-09-07 08:41发布

问题:

All

I have seen the jboss-service.xml which use an extended SystemPropertiesService class to reference to custom property file. But I didn't totally understood this kind of usage yet. Could someone please give me some help to understand how to use these two class? thanks.

回答1:

The SystemPropertiesService is very useful to define properties that then can be accessed from your application, it's usually used to parametrize the application without having to change to code, or even the application package (provided you place the jboss-service.xml outside de war / ear / jar structure). For example, you can create a myapp-service.xml file with the following content:

<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>

Then you can deploy this file directly in JBoss, the properties defined will be visible to all the applications deployed in the same JBoss and you'll be able to access them with the static method:

String System.getProperty(String propertyName)

So if you want to access to the value of myapp.property1 from your application you'd do:

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

On the other hand the PropertyListener is really an interface that defines a listener that will be triggered when any event occurs with a property. The org.jboss.util.property.PropertyAdapter is an abstract implementation of this interface. To use it you've to implement its three methods (propertyAdded, propertyChanged, propertyRemoved), that will be called by the container when a property is added, changed or removed respectively. Those methods have a PropertyEvent object as parameter, which let you know the property affected.

This interface/class is useful when you want your application to do something every time a property changes (a bad implementation would be that you check every certain time for a property change), this way, when JBoss detects that a property has changed its value, it will call the respective method (that you should implement with the behaviour you want).

For example, if you want to print the new property value everytime it's changed you could implement the propertyChanged method this way:

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());
    }
}

Look for more information in the API, and for PropertyAdapter and PropertyEvent.



回答2:

In JBOSS 5.1 it only works when you put the properties or URL in properties-service.xml and this file should go under jboss.home/server/default/deploy directory.