To integrate Quartz with Guice. One can follow below steps. The most important part is, we should be able to inject dependencies inside Job.
To provide injectable Scheduler Instance we need to write Scheduler provider as follows:
Now that we have defined provider for Scheduler we need to map the same inside Guice Module
With the above code, we should be able to inject Scheduler instance where we want. Lets look at how we would be able to get dependencies injected inside Job. As instantiation of the Job object is done by Scheduler itself, we just pass on the class name through JobDetails. We need intercept this instantiation process to do that we will write our own Job Factory as follows:
We need ask scheduler to use this JobFactory while instantiating Job object. We have already connected scheduler with JobFactory inside SchedulerProvider, look at line number 20 for SchedulerProvider code above.
As we are returning same instance from Providers get method. Where ever you will inject Schedular, you will get the same object. Schedule your jobs the way it is done normally there is no change in it.
To provide injectable Scheduler Instance we need to write Scheduler provider as follows:
import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; /** * @author Pandurang Patil 27-Apr-2014 * */ @Singleton public class SchedulerProvider implements Provider<Scheduler> { private Scheduler scheduler; @Inject public SchedulerProvider(SchedulerJobFactory jobFactory) throws SchedulerException { scheduler = new StdSchedulerFactory().getScheduler(); scheduler.setJobFactory(jobFactory); } @Override public Scheduler get() { return scheduler; } }
Now that we have defined provider for Scheduler we need to map the same inside Guice Module
import org.quartz.Scheduler; import com.google.inject.AbstractModule; /** * @author Pandurang Patil 27-Apr-2014 * */ public class SchedulerModule extends AbstractModule { @Override protected void configure() { bind(Scheduler.class).toProvider(SchedulerProvider.class); } }
With the above code, we should be able to inject Scheduler instance where we want. Lets look at how we would be able to get dependencies injected inside Job. As instantiation of the Job object is done by Scheduler itself, we just pass on the class name through JobDetails. We need intercept this instantiation process to do that we will write our own Job Factory as follows:
import org.quartz.Job; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.spi.JobFactory; import org.quartz.spi.TriggerFiredBundle; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Singleton; /** * @author Pandurang Patil 27-Apr-2014 * */ @Singleton public class SchedulerJobFactory implements JobFactory { @Inject private Injector injector; @Override public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException { return (Job) injector.getInstance(bundle.getJobDetail() .getJobClass()); } }
We need ask scheduler to use this JobFactory while instantiating Job object. We have already connected scheduler with JobFactory inside SchedulerProvider, look at line number 20 for SchedulerProvider code above.
As we are returning same instance from Providers get method. Where ever you will inject Schedular, you will get the same object. Schedule your jobs the way it is done normally there is no change in it.
import org.quartz.Scheduler; import org.quartz.SchedulerException; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; /** * @author Pandurang Patil 27-Apr-2014 * */ public class SchedulerSample { @Inject private Scheduler scheduler; void start() throws SchedulerException { scheduler.start(); } public static void main(String[] args) { Injector injector = Guice.createInjector(new SchedulerModule()); SchedulerSample sample = injector.getInstance(SchedulerSample.class); try { sample.start(); } catch (SchedulerException e) { System.out.println("Error while starting the scheduler..."); } ... ... } }
Google Guice is basically used for AdWords product .
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for the blog.Proved to be helpful. I felt schedulerFactory can be injected instead of creating the object in provider. This dependency injection helps with unit testing
ReplyDeletegreat
DeleteWhere before you were constrained to one arrangement of promotions for countless keyterms, now you can target particular advertisements for particular keyterms. Edkent Media
ReplyDelete192.168.0.1 D-Link Login is a private IP address (sometimes it could be mistyping to 192.168.o.1 or 192.168.0.l ), familiar to every router-user, who is setting up LAN-network
ReplyDeleteGreat Article
ReplyDeleteData Mining Projects
Python Training in Chennai
Project Centers in Chennai
Python Training in Chennai
Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...
ReplyDeleteSoftgen Infotech is a leading training institute for all kind of the Best Python Training in Bangalore with real-time experienced trainers with 100% Placement Assistance.
Not utilizing the quality behind negative watchwords. Numerous advertisers don't think to incorporate a negative catchphrase, one that you should keep from appearing on any inquiry questions. Google Ads Expert
ReplyDeleteUseful blog.Thank you padurangpatil to shared with us.
ReplyDeleteJava training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
Great Article
ReplyDeleteArtificial Intelligence Projects
Project Center in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai
Impressive. Your story always bring hope and new energy. Keep up the good work.
ReplyDeletedata science training in chennai
data science training in tambaram
android training in chennai
android training in tambaram
devops training in chennai
devops training in tambaram
artificial intelligence training in chennai
artificial intelligence training in tambaram
Nice Article you have posted here. Thank you for giving this innovative information and
ReplyDeleteplease add more in future.
sap training in chennai
sap training in annanagar
azure training in chennai
azure training in annanagar
cyber security course in chennai
cyber security course in annanagar
ethical hacking course in chennai
ethical hacking course in annanagar
Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
ReplyDeleteangular js training in chennai
angular js training in velachery
full stack training in chennai
full stack training in velachery
php training in chennai
php training in velachery
photoshop training in chennai
photoshop training in velachery
I would like to say thank you for the amazing details and concepts you are sharing in this.The style of writing is excellent and also the content is top-notch. Thanks for that shrewdness you provide the readers!
ReplyDeletehardware and networking training in chennai
hardware and networking training in omr
xamarin training in chennai
xamarin training in omr
ios training in chennai
ios training in omr
iot training in chennai
iot training in omr
Yet Google's scale and scope when combined with its way of making money, which can feel like the exploitation of someone else's efforts, don't engender a lot of trust, regardless of the purity of the company's motives. how to get emails from a website
ReplyDeleteThanks for the useful tips and information about this programming language.
ReplyDeletedata science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai
Google Analytics is a web analytics service offered by Google that tracks and reports website traffic. You can create goals and conversion tracking to get the most of your analytics information and use it to improve your website's content, user experience and optimization for better online sales. Lead generation website
ReplyDeleteMua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
vé máy bay phú quốc tp hồ chí minh
chuyến bay chu lai hà nội
vé máy bay hanoi nha trang
ve may bay gia re di Hue
xe đưa rước sân bay
combo đi đà nẵng