Re: NSNotificationCenter
Re: NSNotificationCenter
- Subject: Re: NSNotificationCenter
- From: Simon <email@hidden>
- Date: Tue, 01 Mar 2011 18:25:41 +0000
we do a lot of this type of thing using NSNotifications. the key is to
use pass in the user as GID, not an EO, then use a thread execution
pool to do your stuff. i've pasted below some sample code - this is a
class that listens for a new "ReportJob", then actually creates the
report. I've ripped out all the code that is specific to the task but
you should be able to figure the the general concept from what is
left.
public class ClickReportJob {
public static class Queue extends Object {
private static NSMutableDictionary<EOGlobalID,
FutureTask<ReportJobThread>> _queue;
public static void addToQueue(FutureTask<ReportJobThread> ft,
EOGlobalID gid) {
getQueue().setObjectForKey(ft, gid);
}
public static NSMutableDictionary<EOGlobalID,
FutureTask<ReportJobThread>> getQueue() {
if (_queue == null) {
_queue = new NSMutableDictionary<EOGlobalID, FutureTask<ReportJobThread>>();
}
return _queue;
}
}
class ReportJobThread extends Thread {
public EOGlobalID gid;
public ReportJobThread(String str, EOGlobalID gid) {
super(str);
this.gid = gid;
}
@Override
public void run() {
log.debug("ReportJobListner: Just started run() method...");
EOEditingContext ec = ERXEC.newEditingContext();
ReportJob job = null;
try {
ec.lock();
// Fetch the ReportJob.
job = (ReportJob) ERXEOControlUtilities.convertGIDtoEO(ec, gid);
// Do stuff....
} catch (Exception e) {
// Do stuff...
} finally {
ec.unlock();
ec.dispose();
}
}
}
public Logger log = Logger.getLogger(this.getClass());
public ExecutorService pool = Executors.newFixedThreadPool(3);
public ClickReportJob() {
super();
log.debug("Registering " + this.getClass() + " for notifications");
NSNotificationCenter nsnc = NSNotificationCenter.defaultCenter();
nsnc.addObserver(this, ERXSelectorUtilities.notificationSelector("trigger"),
ReportJob.NEW_REPORT_JOB_NOTIFICATION, null);
}
public void trigger(NSNotification notification) {
log.debug("TRIGGER: " + this.getClass());
try {
EOGlobalID gid = (EOGlobalID) notification.object();
ReportJobThread t = new ReportJobThread("Job" + gid, gid);
FutureTask<ReportJobThread> ft = new FutureTask<ReportJobThread>(t, null);
pool.submit(ft);
ClickReportJob.Queue.addToQueue(ft, gid);
} catch (Exception e) {
log.debug("Exception whilst handing a report thread to the executor
pool", e);
}
}
}
On 1 March 2011 09:48, Frédéric JECKER <email@hidden> wrote:
> Hello,
> Some customers asked us for adding some automatic per user notifications
> when some specific events happen within our application (each user should be
> able to specify its own criterias).
> I'm looking at NSNotification/NSNotificationCenter for this purpouse but I'm
> wondering if the notification dispatching and processing is performed on the
> main thread or on a separate one.
> Thanks
> Fred
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden