apache > ws.apache

## Apache Muse - Add WSDM Metrics to Resource Properties

You can augment your custom resource properties by adding WSDM Metrics metadata to them; this metadata includes timestamps that tell you when and how frequently a property is updated. Follow the steps below to turn one of your resource properties into a WSDM metric.

- Add the muws2:CurrentTime property to your WSRP document. This is the only property defined by the WSDM Metrics capability and will alert the wsdl2java tool that your resource interface should include that capability. Below is an XML fragment that you can plug into your WSRP document:

  > `<xsd:element xmlns:muws2="http://docs.oasis-open.org/wsdm/muws2-2.xsd" ref="muws2:CurrentTime"/>`

- Create a WSRMD (metadata) file for your resource properties, if you haven't done so already. Be sure to create a <wsrmd:Property/> element for each property in your WSRP document. The WSRF sample project has an RMD file that you can use as a starting point.
- For each property that you want to add metrics to, you must add the following XML under its <wsrmd:Property/> element:

  > `<wsrmd:Property name="myns:MyProperty" mutability="..." modifiability="...">
  > <muws2:Capability>http://docs.oasis-open.org/wsdm/muws/capabilities/Metrics</muws2:Capability>
  > <muws2:TimeScope>Interval | PointInTime | SinceReset</muws2:TimeScope>
  > <muws2:GatheringTime>OnChange | OnDemand | Periodic | Unknown</muws2:GatheringTime>
  > <muws2:CalculationInterval>an xsd:duration value</muws2:CalculationInterval>
  > </wsrmd:Property>`

  In the fragment above, the <muws2:Capability/> element tells Muse that the property must be monitored by the WSDM Metrics capability. You should select the values of the other three element based on the semantics described in the WSDM MUWS Part 2 specification, Section 3.4.
- Run wsdl2java and complete your project as normal. You do not have to write any code to ensure that the metrics are added to your properties - this will be done dynamically by the Metrics capability.
- Once your endpoint is deployed and running, you can use the org.apache.muse.ws.dm.muws.remote.MetricsClient class to retrieve resource properties and metric values as Java objects (that is, without using the DOM API to get the attribute values). Below is some sample code that illustrates the use of MetricsClient to read the LastUpdated time for the myns:MyProperty property:

  > `URI address = URI.create("http://localhost/resources/services/metric-providing-resource");
  > EndpointReference epr = new EndpointReference(address);
  > MetricsClient client = new MetricsClient(epr);
  > Map metrics = new HashMap();
  > QName name = new QName("http://my/namespace", "MyProperty", "myns");
  > String[] values = (String[])client.getPropertyAsObjectAndMetrics(name, String.class, metrics);
  > System.out.println("The property values are: ");
  > for (int n = 0; n < values.length; ++n)
  > System.out.println(values[n]);
  > Date lastUpdated = (Date)metrics.get(MuwsConstants.LAST_UPDATED);
  > System.out.println("The property was last updated on: " + lastUpdated);`

  The JavaDoc for MetricsClient provides details on the names and types of the metrics returned by its methods. Note that in addition to getPropertyAsObjectAndMetrics(), there is also a method named getResourcePropertyAndMetrics() that will give you the raw XML for the resource properties (and metrics) if you really need it.