Skip to main content
14-Alexandrite
November 25, 2016
Question

How to run background task periodically

  • November 25, 2016
  • 1 reply
  • 3106 views

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.

1 reply

17-Peridot
November 30, 2016

I wouldn't do it manually with a timer. Windchill has the ability to schedule jobs.

Have a look at the customization guide and search for the chapter: Procedure – Creating Custom Schedule Jobs

14-Alexandrite
December 9, 2016

It works.

But is there a way to solve by background queue?

17-Peridot
December 10, 2016

Yes, the task will probably running in the Methodserver. I don't have any solution to run it in the BMS. Perhaps somebody else can help...?