creating and running a background thread
creating and running a background thread
- Subject: creating and running a background thread
- From: "Jonathan Fleming" <email@hidden>
- Date: Sat, 13 Mar 2004 14:51:26 +0000
Sorry about this guys, I know this isn't really a WebObjects question but
I need a bit of help on this. I don't know how to run this code from a
background thread (never used them before) which I want to run once a day
every day. I'm not worried about the once a day thing just correctly
starting and finishing a thread to run this class.The way I am running
the class at the moment is hap-hazzard I'm sure, but it functions for now
just so I know it works. This is how I'm calling the jobAdvertCleaner()
method which deletes active jobs in the DB that are over 45 days old...
from my class named JobAdvert, in the constructor I have this statement
// constructor of JobAdvert class
public JobAdvert(WOContext context) {
super(context);
runJobAdvertTimer();
}
/*
which obviously calls runJobAdvertTimer(), which then runs the
JobAdvertCleaner class code.
I know this is not the right way to do this so how do I use a thread
correctly
anyone... please... thank you.
Jonathan :^)
*/
protected JobAdvertCleaner runJobAdvertTimer(){
JobAdvertCleaner nextPage =
(JobAdvertCleaner)pageWithName("JobAdvertCleaner");
return nextPage;
} This is the code I want to run:
/* JobAdvertCleaner.java created by Jonathan on Fri 12-Mar-2004 */import
com.webobjects.foundation.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
import com.webobjects.appserver.*;public class JobAdvertCleaner extends
WOComponent {
protected TbJobAdvert tbJobAdvert;
/** @TypeInfo TbJobAdvert */
protected NSMutableArray queryResults; public
JobAdvertCleaner(WOContext context) {
super(context);
jobAdvertCleaner();
}
/***********************************************************************************/
/*===============
jobAdvertCleaner ===============*/ public void
jobAdvertCleaner() { tbJobAdvert = new TbJobAdvert();
NSLog.out.appendln( "\n************ pageWithName() = " + name()
+" ************"); NSTimestamp now = new NSTimestamp();
NSTimestampFormatter formatter = new NSTimestampFormatter("%A %d
%B %Y");
String nowStringDate = formatter.format(now);
String lastRmdCheck =
((Application)application()).getLastRmdCheck();
while (!nowStringDate.equals(lastRmdCheck)) {
/*-------------------------------------------------------------------------------*/
/*---------------- Start Of Progmatically Created Fetch
Spec ----------------*/
Integer active = new Integer(1);
/*------------- end local variables & start qualifier
bindings --------------*/
NSArray bindings = new NSArray( new Object [] { active } );
EOQualifier qualifier =
EOQualifier.qualifierWithQualifierFormat("(active = %@)",
bindings );
NSLog.out.appendln("\n*** bindings = " + bindings + "\n" +
" qualifier " + qualifier);
/*------------- end qualifier statements & start fetch
spec -------------*/
EOFetchSpecification fetchSpec = new
EOFetchSpecification("TbJobAdvert", qualifier, null);
&nbs p; EOEditingContext myEC =
this.session().defaultEditingContext();
queryResults = new
NSMutableArray(myEC.objectsWithFetchSpecification(fetchSpec));
NSLog.out.appendln("\n*** value for queryResults from
JobAdvertCleaner is: \r" + queryResults +
"\n++++++++ END OF ACTIVE JOB ADVERT
FIND ++++++++\n");
/*----------------------------- end of fetchSpec
----------------------------*/
// get editing context
EOEditingContext ec = session().defaultEditingContext();
int index;
for (index=0; index<queryResults.count(); index++)
{
if (queryResults.count() > 0) {
NSLog.out.appendln("*** IN QUERY_RESULTS FOR LOOP");
if (queryResults.lastObject() !=null)
{
&nbs p; tbJobAdvert =
(TbJobAdvert)queryResults.objectAtIndex( index );
String title =
(String)((EOEnterpriseObject)queryResults.objectAtIndex( index
)).valueForKeyPath("title");
String firstName =
(String)((EOEnterpriseObject)queryResults.objectAtIndex( index
)).valueForKeyPath("firstName");
String lastName =
(String)((EOEnterpriseObject)queryResults.objectAtIndex( index
)).valueForKeyPath("lastName");
NSTimestamp creationDate =
(NSTimestamp)((EOEnterpriseObject)queryResults
.objectAtIndex( index )).valueForKey("creationDate");
NSTimestamp timeLapse =
creationDate.timestampByAddingGregorianUnits(0, 0, +45, 0, 0, 0);
NSLog.out.appendln(" registration creationDate
is: " + creationDate + "\r" +
;
" for: " + title+" "+firstName+"
"+lastName+"'s\r" +
" advert to be deleted
after: " + timeLapse + "\r" +
" Date/Time now
is: " + now + ".\r");
NSLog.out.appendln("*** THE OBJECT BEING PREPARED
FOR DELETION AT INDEX ( "+index+" ) IS: \r " +
queryResults.objectAtIndex(
index ) + "");
if (now.after(timeLapse))
{
NSLog.out.appendln( "*** EXECUTING DELETE
CODE NOW!\r ..." );
// remove tbJobAdvert from queryResults
queryResults.removeObject(tbJobAdvert);
// get objects editing context
EOEditingContext ecTb =
tbJobAdvert.editingContext();
ecTb.lock();
// remove tbJobAdvert from object graph
ecTb.deleteObject(tbJobAdvert);
// save changes made in editing context to
object store
&nbs p; ec.saveChanges();
ecTb.unlock();
NSLog.out.appendln(" THE OBJECT AT INDEX
"+index+" HAS BEEN DELETED \n");
}
}
}
}
((Application)application()).setLastRmdCheck(formatter.format(now));
lastRmdCheck =
((Application)application()).getLastRmdCheck();
queryResults = null;
}
}
/*============== End of JobAdvert Cleaner
==============*/
/***********************************************************************/
}
------------------------------------------------------------------------
Are you going travelling? Help us to find the 100 best internet cafes in
the world. Click here for more details and you could win #250 and a
digital camera!
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.