Hi Stuart, funny to run into you here. Thanks for your help, although I had hoped for a more simple solution..
regards Nino 2010/6/18 Stuart McCulloch <[email protected]> > On 19 June 2010 01:54, nino martinez wael <[email protected]>wrote: > >> Hi >> >> im getting a NPE in my typelistener, and are wondering why.. >> > > unfortunately typelisteners are not injected in time... > > http://code.google.com/p/google-guice/issues/detail?id=409 > > one workaround is to grab the relevant provider via the TypeEncounter > argument and cache it in a field - you can then call this provider once > injection has started (but not before, as per the encounter javadoc) > > to find out when injection has started register an InjectionListener with > the same TypeEncounter (or a MembersInjector if you want to inject > members of the instance being created) > > see also: > > http://code.google.com/p/google-guice/wiki/CustomInjections > > > http://www.sonatype.com/people/2010/01/from-plexus-to-guice-2-the-guiceplexus-bridge-and-custom-bean-injection/ > > HTH > > Heres the module definiton: >> >> bind(SchedulerFactory.class).toProvider(getScheduleFactoryProvider()) >> .asEagerSingleton(); >> >> ScheduledTypeListener scheduledTypeListener = new >> ScheduledTypeListener(); >> requesting injection --> requestInjection(scheduledTypeListener); >> Matcher<TypeLiteral<?>> matcher = new >> AbstractMatcher<TypeLiteral<?>>() { >> public boolean matches(TypeLiteral<?> type) { >> return >> (type.getRawType().isAnnotationPresent(Scheduled.class)); >> } >> }; >> bindListener(matcher, scheduledTypeListener); >> >> and the code for the listener: >> >> public class ScheduledTypeListener implements TypeListener { >> >> private SchedulerFactory schedulerFactory; >> >> public <T> void hear(TypeLiteral<T> typeLiteral, >> TypeEncounter<T> typeEncounter) { >> if (typeLiteral.getRawType().isAnnotationPresent(Scheduled.class)) >> { >> startSchedule(typeLiteral.getRawType()); >> } >> } >> >> private void startSchedule(Class clazz) { >> >> Scheduler sched = null; >> try { >> NPE--> sched = getSchedulerFactory().getScheduler(); >> sched.start(); >> } catch (SchedulerException e) { >> // TODO Auto-generated catch block >> e.printStackTrace(); >> } >> >> JobDetail jobDetail = new JobDetail("myJob", // job name >> sched.DEFAULT_GROUP, // job group (you can also specify >> 'null' >> // to use the default group) >> clazz); // the java class to execute >> >> CronTrigger trigger = new CronTrigger(clazz.getCanonicalName()); >> >> Scheduled scheduled = (Scheduled) >> clazz.getAnnotation(Scheduled.class); >> >> String cronString = scheduled.cron(); >> try { >> trigger.setCronExpression(cronString); >> sched.scheduleJob(jobDetail, trigger); >> } catch (Exception e) { >> // TODO Auto-generated catch block >> e.printStackTrace(); >> } >> >> } >> >> Asking for injection--> @Inject >> public void setSchedulerFactory(SchedulerFactory schedulerFactory) { >> this.schedulerFactory = schedulerFactory; >> } >> >> public SchedulerFactory getSchedulerFactory() { >> return schedulerFactory; >> } >> >> } >> >> >> Full source with test case here: https://slurry.googlecode.com/svn , >> under quartz4guice >> >> -- >> You received this message because you are subscribed to the Google Groups >> "google-guice" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<google-guice%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/google-guice?hl=en. >> > > -- > Cheers, Stuart > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
