cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Access deny: How to run background task periodically

HelesicPetr
22-Sapphire I

Access deny: How to run background task periodically

Hello,

I don't have access to the Discussion "How to run background task periodically ",

https://community.ptc.com/message/472772

found in https://community.ptc.com/t5/Windchill/Is-there-anyway-to-schdule/m-p/184990#M56259 post

Why i dnot have access to the original topic?

I would like to get an access.?

 

Thank you so much for an answer.

Best Regards

Petr H.

 

Interesting answer was published in the original discusion:

If you wan't to use a time, you need to create a Windchill context :

import wt.method.MethodContext

import wt.method.RemoteMethodServer

import wt.session.SessionContext

import wt.session.SessionHelper

new MethodContext(null, null)

SessionContext.newContext()

SessionHelper.manager.setPrincipal("wcadmin")

RemoteMethodServer rms = RemoteMethodServer.getDefault()

rms.setUserName("wcadmin")

rms.setPassword("wcadmin")

But as Bjorg said, it's not the best way. In fact, if you have several MS & a BGMS, the thread would run everywhere, and it would probably raise stranges issues.

I had to do something similar, and I used wt.queue.ScheduleQueue and its associated entry wt.queue.ScheduleQueueEntry

ScheduleQueueEntries have a "processAt" parameter.

The algorithm is :

    In a service, check that the custom schedule queue exists
    if no create it
    Is there an entry in the queue ?
    if yes, do nothing
    if no, add a new one, scheduled to execute at a specific date

And iat the end of the code launched by the ScheduleQueueEntry, you add a new entry in the queue. So there is always a pending job.

Some code :

The starting point is to call addQueueEntry(); in the performStartupProcess method.

Everyhting is done from this method.

Of course you need to define some constants, like HAGER_QUEUE_NAME

private static ScheduleQueue initialiseQueue(String qname)

   throws WTException {

  ScheduleQueue q = (ScheduleQueue) QueueHelper.manager.getQueue(qname, wt.queue.ScheduleQueue.class);

   if (q == null) {

  q = createScheduleQueue(qname);

   //QueueHelper.manager.setInterval(q, interval);
  // q = (ScheduleQueue)PersistenceHelper.manager.save(q); Voila qui ne m'a plus l'air n�cessaire.....
   if (logger.isDebugEnabled()) {

  logger.debug("initialiseQueue() - " + CLASSNAME + ".initialiseQueue() Creating Queue " + qname); //$NON-NLS-1$ //$NON-NLS-2$
   }

  }

   if (logger.isDebugEnabled()) {

  logger.debug("initialiseQueue() - " + CLASSNAME + ".initialiseQueue() Starting Queue " + qname); //$NON-NLS-1$ //$NON-NLS-2$
   }

  QueueHelper.manager.startQueue(q);

   return q;

}

//##begin newStandardHagerService%newStandardHagerServicef.doc preserve=no

private static ScheduleQueue createScheduleQueue(String qname)

   throws WTException {

  ScheduleQueue schedulequeue = null;

   try {

  schedulequeue = QueueHelper.manager.createScheduleQueue(qname);

  } catch (ObjectIsStaleException objectisstaleexception) {

  schedulequeue = (ScheduleQueue) QueueHelper.manager.getQueue(qname, wt.queue.ScheduleQueue.class);

  } catch (UniquenessException uniquenessexception) {

  schedulequeue = (ScheduleQueue) QueueHelper.manager.getQueue(qname, wt.queue.ScheduleQueue.class);

  }

   return schedulequeue;

}

//##begin getAllSigleService%3BF8E35000FAg.doc preserve=no

public static void addQueueEntry() {

  ScheduleQueue queue = null;

// Check existing entries
   try {

  QuerySpec qs = new QuerySpec(wt.queue.ScheduleQueueEntry.class);

  ClassAttribute theClassAttribute = new ClassAttribute(wt.queue.ScheduleQueueEntry.class, "targetClass");

  RelationalExpression theExpression = ConstantExpression.newExpression("ext.hager.services.StandardHagerService", theClassAttribute.getColumnDescriptor().getJavaType());

  SearchCondition theCondition = new SearchCondition(theClassAttribute, SearchCondition.EQUAL, theExpression);

  qs.appendWhere(theCondition);

  qs.appendAnd();

  theClassAttribute = new ClassAttribute(wt.queue.ScheduleQueueEntry.class, "statusInfo.code");

  theExpression = ConstantExpression.newExpression("READY", theClassAttribute.getColumnDescriptor().getJavaType());

  theCondition = new SearchCondition(theClassAttribute, SearchCondition.EQUAL, theExpression);

  qs.appendWhere(theCondition);

  QueryResult qr = PersistenceServerHelper.manager.query(qs);

   if (qr.size() > 0) {

  // Found something
   if (logger.isDebugEnabled()) {

  logger.debug("addQueueEntry() - " + CLASSNAME + ".addQueueEntry() No need to add an entry. "); //$NON-NLS-1$ //$NON-NLS-2$
   }

   return;

  }

  } catch (WTException wte) {

   if (logger.isDebugEnabled()) {

  logger.debug("addQueueEntry() - " + CLASSNAME + ".addQueueEntry() Exception caught " + wte.toString()); //$NON-NLS-1$ //$NON-NLS-2$
   }

  logger.error("addQueueEntry()", wte); //$NON-NLS-1$
   return;

  }

   try {

  queue = initialiseQueue(HAGER_QUEUE_NAME);

  GregorianCalendar cDate = new GregorianCalendar();

  cDate.add(Calendar.DAY_OF_YEAR, NB_JOURS_SCHED_QUEUE);

  cDate.set(Calendar.HOUR, HEURE_TACHE_ARCHIVAGE);

  cDate.set(Calendar.MINUTE, 0);

  cDate.set(Calendar.SECOND, 0);

  Timestamp processAt = new Timestamp(cDate.getTime().getTime());

   //processAt = new Timestamp((new java.util.Date()).getTime()+10000);

   WTPrincipal user = SessionContext.getEffectivePrincipal();

  WTPrincipal admin = SessionHelper.manager.getAdministrator();

  WTPrincipal admin2 = SessionContext.setEffectivePrincipal(admin);



   queue.addEntry(

  admin,

   "archiveDocuments",

  CLASSNAME,

   new Class[]{},

   new Object[]{},

  processAt);

   if (logger.isDebugEnabled()) {

  logger

  .debug("addQueueEntry() - " + CLASSNAME + ".addQueueEntry() Prochaine recherche des documents obol�tes �  " + cDate.getTime()); //$NON-NLS-1$ //$NON-NLS-2$
   }

  SessionContext.setEffectivePrincipal(user);

  } catch (WTException wte) {

   if (logger.isDebugEnabled()) {

  logger

  .debug("addQueueEntry() - " + CLASSNAME + ".addQueueEntry() Exception caught " + wte.toString()); //$NON-NLS-1$ //$NON-NLS-2$
   }

  logger.error("addQueueEntry()", wte); //$NON-NLS-1$
   return;

  }

}

//-------------------------------------- code executed by the queue :

public static void archiveDocuments() throws WTException {

  // all the hthings you need goes here...

// And here we go again.

  addQueueEntry();

}


 

ACCEPTED SOLUTION

Accepted Solutions
Simona
14-Alexandrite
(To:HelesicPetr)

Hi @HelesicPetr ,

 

I just did an update to that specific post and it should be visible for everyone now.

 

Simona

View solution in original post

2 REPLIES 2
Simona
14-Alexandrite
(To:HelesicPetr)

Hi @HelesicPetr ,

 

I just did an update to that specific post and it should be visible for everyone now.

 

Simona

HelesicPetr
22-Sapphire I
(To:Simona)

Hello Simona,

Thank you so much. 

BestRegards

PetrH

Announcements

Top Tags