Adding configurable parameters to a service
For a simple service as the one we just wrote, there is only one parameter that can be configured. It is the delay at which the service runs! What if we need to add more parameters? Let's say we want to add the tutorial name in the service, which can be changed later if needed.
How to do it…
The following are the steps to do so:
Modify the service configuration XML to include the configurable properties:
<someservice id="jtricksserviceid"> <description>My New Service</description> <properties> <property> <key>Tutorial</key> <name>The tutorial you like</name> <type>string</type> </property> </properties> </someservice>
Here, we have added a string property with the key
Tutorial
.Override the
init()
method in the service class to retrieve the new property:@Override public void init(PropertySet props) throws ObjectConfigurationException ...