简体   繁体   中英

How to execute code based on class names in database

My application needs to send some notification (in future) to the customer based on the some business logic. I need to save all these notification with some baisc information in database (calling them as jobs like SMSJob or EmailJob). One of the column will have class name for each specific job. Now there will be another logic which will query the database and pick up all these jobs and start executing them one by one. As suggested by Mikhail and Arsen i can use reflection to create instance of all class based on the name of class.

Is there something similar i can refer to for the code. In nut shell i am just trying to implement similar functionalty which quartz is provding. I have very stringent timelines so any reference to the article or code will really help me.

thanks in advance.

You need to use reflection. If your classes implement Runnable interface, do like this:

Class <?> clazz = Class.forName (classNameFromDB);
Runnable runnable = (Runnable)clazz.newInstance ();
runnable.run ();

Your classes need to override some base interface for them with for example execute() method. And after retreiving them from the database You can use reflection to execute them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM