How to run background task periodically
Hi.
I need run some code in Windchill to update objects. I need start this task at specified time and rerun it in some period.
So i tried to use java.util.TimerTask implementation.
Some code example i used:
public class MyServiceManager extends wt.services.StandardManager {
private static Logger LOGGER = LogR.getLogger(MyServiceManager .class.getName());
public static MyServiceManager newMyServiceManager () throws WTException {
MyServiceManager instance = new MyServiceManager ();
instance.initialize();
return instance;
}
@Override
protected synchronized void performStartupProcess() throws ManagerException {
try {
/*some code to init firstTime and period*/
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), firstTime, period);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
public class MyTimerTask extends java.util.TimerTask {
private static Logger LOGGER = LogR.getLogger(MyTimerTask.class.getName());
public MyTimerTask() {
//
}
@Override
public void run() {
try {
QuerySpec querySpec = new QuerySpec();
//fill query spec with conditions
QueryResult qr = PersistenceHelper.manager.find((StatementSpec) querySpec); // and i get exception here
//
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
In PersistenceHelper.manager.find i get exception:
Message: Resource bundle/Message key = wt.pom.pomResource/0
(wt.pom.pomResource/0) wt.pom.POMInitException: A persistence error occurred. System message follows:
Nested exception is: wt.method.MethodServerException: No active method context
at wt.method.MethodContext.getContext(MethodContext.java:1725)
at wt.pom.PersistentObjectManager.getPom(PersistentObjectManager.java:289)
at wt.fc.StandardPersistenceManager._query(StandardPersistenceManager.java:1993)
at wt.fc.StandardPersistenceManager._find(StandardPersistenceManager.java:2022)
at wt.fc.StandardPersistenceManager.find(StandardPersistenceManager.java:681)
at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at wt.services.ServiceFactory$ServerInvocationHandler.invoke(ServiceFactory.java:399)
at com.sun.proxy.$Proxy9.find(Unknown Source)
What shouild i do to fix this? Or is there more appropriate way to run same task in Windchill?
I expect get java code example.
Thanks.

