Example 6: Listener

If you can't play the video, make sure that you are logged in at Wex Solutions.

This example illustrates how to add a Listener to a Windchill Extension.

You can find the code for this example in our GitHub repository.

Implementation

A listener is implemented by tagging a Java class with the @WexComponent, @WexWtListener and @WexWtEventKey annotations: the @WexComponent annotation defines it as a feature, the @WexWtListener one makes it a listener and the @WexWtEventKey tag configures it to listen for certain events generated by a list of types of Windchill object. An example like the one below can be found in the com/wincomplm/wex/example/listener/impl/listeners/StateListener.java class located in the example's code:

@WexComponent(uid = "wex-example-listener", description = "Example Set State Listener")
@WexWtListener({
@WexWtEventKey(eventClass = LifeCycleServiceEvent.class, keys = LifeCycleServiceEvent.SET_STATE, supported = {WTPart.class}, fatal = false)})
public class StateListener implements IWexWtListener<Object> {

The listener fires with the event specified in the eventClass parameter (a lifecycle service event, in this case), and is then filtered by a list of supported classes, a WTPart in our example.

To monitor this, we have added some logging:

logger.warn("A part has changed state {0} to {1}", part.getNumber(), part.getState().toString());

Note: if you want to learn more about logging, make sure you check out our example on it.

Execution

After installing the example WEX, locate any WTPart in your system and change its state. After doing that, download the logs from the Extension Manager.

1618172290915

In the MS logs you will find this line, which is generated by the listener's code.

2021-04-11 20:09:23 WARN  com.wincomplm.wex-example-listener.StateListener - A part has changed state GC000009 to INWORK