您好,登錄后才能下訂單哦!
本篇文章為大家展示了Jspxcms定時(shí)任務(wù)的開發(fā)是怎樣的,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
系統(tǒng)中有定時(shí)任務(wù)功能,里面有一些系統(tǒng)已經(jīng)定義好的任務(wù)類型。如果系統(tǒng)自帶的任務(wù)類型里沒有自己需要的,可以開發(fā)一個(gè)任務(wù)類型。
本著無侵入的二次開發(fā)設(shè)計(jì)思想,開發(fā)一個(gè)自己的任務(wù)類型也可以做到不修改系統(tǒng)原有代碼和文件。
Jspxcms6.5及以后版本:
/src/main/resources/conf/application.properties
Jspxcms6.0及以前的版本:
/WEB-INF/conf/application.properties
相關(guān)配置內(nèi)容:
scheduleJob.100=com.jspxcms.core.quartz.InfoPublishJob scheduleJobPath.com.jspxcms.core.quartz.InfoPublishJob= scheduleJob.200=com.jspxcms.core.quartz.HtmlHomeJob scheduleJobPath.com.jspxcms.core.quartz.HtmlHomeJob= scheduleJob.300=com.jspxcms.ext.quartz.CollectJob scheduleJobPath.com.jspxcms.ext.quartz.CollectJob=../../ext/collect/schedule_job.do
自定義的定時(shí)任務(wù)類型的配置也可以寫在其它的application.properties文件中,如/src/main/resources/conf/plugin/plug/application.properties。
scheduleJob.300:序號(hào)300決定這個(gè)類型的排序,即在選擇任務(wù)類型時(shí)的前后順序。序號(hào)不能重復(fù)。
com.jspxcms.ext.quartz.CollectJob既是定時(shí)任務(wù)實(shí)現(xiàn)類,又是定時(shí)任務(wù)類型名稱。需要在國際化文件中定義相應(yīng)的國際化名稱。
國際化文件(Jspxcms6.5及以后版本):/src/main/resources/messages/ext/ext.properties
國際化文件(Jspxcms6.0及以前的版本):/WEB-INF/messages/ext/ext.properties
scheduleJob.code.com.jspxcms.ext.quartz.CollectJob=采集
需以scheduleJob.code.開頭。國際化名稱也可以寫在其他文件中,如/src/main/resources/messages/plugin/plug/plug.properties。
com.jspxcms.ext.quartz.CollectJob是定時(shí)任務(wù)實(shí)現(xiàn)類。
public class CollectJob implements Job { private static final Logger logger = LoggerFactory .getLogger(CollectJob.class); public static final String COLLECT_ID = "collectId"; public void execute(JobExecutionContext context) throws JobExecutionException { try { ApplicationContext appContext = (ApplicationContext) context .getScheduler().getContext().get(Constants.APP_CONTEXT); Collector collector = appContext.getBean(Collector.class); JobDataMap map = context.getJobDetail().getJobDataMap(); Integer collectId = map.getIntegerFromString(COLLECT_ID); collector.start(collectId); System.out.println("collect ok"); logger.info("run collect job: " + collectId); } catch (SchedulerException e) { throw new JobExecutionException("Cannot get ApplicationContext", e); } } }
需要實(shí)現(xiàn)org.quartz.Job接口,在public void execute(JobExecutionContext context)方法中編寫任務(wù)需要執(zhí)行的代碼。
ApplicationContext appContext = (ApplicationContext) context.getScheduler().getContext().get(Constants.APP_CONTEXT);可以獲取Spring的ApplicationContext,通過ApplicationContext可以獲取到Spring管理的對(duì)象,如Collector collector = appContext.getBean(Collector.class);。注意:這個(gè)類中必須使用這種方法獲取Spring管理的對(duì)象,不能使用@Autowired等其他方式。
Integer collectId = map.getIntegerFromString(COLLECT_ID);可以獲取額外的參數(shù)。
scheduleJobPath.com.jspxcms.core.quartz.InfoPublishJob=如定時(shí)任務(wù)無需額外參數(shù),則等號(hào)后面留空。
定時(shí)任務(wù)有時(shí)需要傳遞外的參數(shù),比如采集定時(shí)任務(wù)需要選擇執(zhí)行哪個(gè)采集數(shù)據(jù)源。此時(shí)需要在定時(shí)任務(wù)新增/修改界面增加相應(yīng)的錄入項(xiàng)。
scheduleJobPath.com.jspxcms.ext.quartz.CollectJob=../../ext/collect/schedule_job.do
編寫一個(gè)獲取錄入界面的地址:../../ext/collect/schedule_job.do,這里使用相對(duì)路徑,相對(duì)于定時(shí)任務(wù)新增界面的地址。
此例中,這個(gè)地址的實(shí)現(xiàn)類是com.jspxcms.ext.web.back.CollectController。
@Controller @RequestMapping("/ext/collect") public class CollectController { ... @RequestMapping("schedule_job.do") public String scheduleJob(HttpServletRequest request, org.springframework.ui.Model modelMap) { Integer siteId = Context.getCurrentSiteId(); List<Collect> collectList = service.findList(siteId); modelMap.addAttribute("collectList", collectList); modelMap.addAttribute("includePage", "../../ext/collect/collect_job.jsp"); return "core/schedule_job/schedule_job_form"; } ... }
modelMap.addAttribute("collectList", collectList);傳遞數(shù)據(jù)。
modelMap.addAttribute("includePage", "../../ext/collect/collect_job.jsp");傳遞錄入界面。
根據(jù)Controller中傳遞的includePage的值,對(duì)應(yīng)JSP頁面為:/WEB-INF/views/ext/collect/collect_job.jsp。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="f" uri="http://www.jspxcms.com/tags/form"%> <tr> <td class="in-lab" width="15%"><s:message code="scheduleJob.collectSource"/>:</td> <td class="in-ctt" width="85%" colspan="3"> <select name="data_collectId"> <c:forEach var="collect" items="${collectList}"> <f:option value="${collect.id}" selected="${dataMap['collectId']}">${collect.name}</f:option> </c:forEach> </select> </td> </tr>
<select name="data_collectId">:此處的data_collectId對(duì)應(yīng)定時(shí)任務(wù)實(shí)現(xiàn)類CollectJob中獲取參數(shù)的方法Integer collectId = map.getIntegerFromString("collectId");
<c:forEach var="collect" items="${collectList}">:此處的${collectList}對(duì)應(yīng)CollectController的modelMap.addAttribute("collectList", collectList);。
上述內(nèi)容就是Jspxcms定時(shí)任務(wù)的開發(fā)是怎樣的,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。