您好,登錄后才能下訂單哦!
這篇文章主要介紹了jspXCMS怎么開發(fā)定時(shí)任務(wù)的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇jspXCMS怎么開發(fā)定時(shí)任務(wù)文章都會有所收獲,下面我們一起來看看吧。
系統(tǒng)中有定時(shí)任務(wù)功能,里面有一些系統(tǒng)已經(jīng)定義好的任務(wù)類型。如果系統(tǒng)自帶的任務(wù)類型里沒有自己需要的,可以開發(fā)一個(gè)任務(wù)類型。
本著無侵入的二次開發(fā)設(shè)計(jì)思想,開發(fā)一個(gè)自己的任務(wù)類型也可以做到不修改系統(tǒng)原有代碼和文件。
Jspxcms8.0及以后版本:
/src/main/resources/conf/conf.properties
Jspxcms6.5-7.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=/cmscp/ext/collect/schedule_job.do
自定義的定時(shí)任務(wù)類型的配置也可以寫在其它的conf.properties文件中,如/src/main/resources/conf/plugin/plug/conf.properties。
scheduleJob.300:序號300決定這個(gè)類型的排序,即在選擇任務(wù)類型時(shí)的前后順序。序號不能重復(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管理的對象,如Collector collector = appContext.getBean(Collector.class);。注意:這個(gè)類中必須使用這種方法獲取Spring管理的對象,不能使用@Autowired等其他方式。
Integer collectId = map.getIntegerFromString(COLLECT_ID);可以獲取額外的參數(shù)。
scheduleJobPath.com.jspxcms.core.quartz.InfoPublishJob=如定時(shí)任務(wù)無需額外參數(shù),則等號后面留空。
定時(shí)任務(wù)有時(shí)需要傳遞外的參數(shù),比如采集定時(shí)任務(wù)需要選擇執(zhí)行哪個(gè)采集數(shù)據(jù)源。此時(shí)需要在定時(shí)任務(wù)新增/修改界面增加相應(yīng)的錄入項(xiàng)。
scheduleJobPath.com.jspxcms.ext.quartz.CollectJob=/cmscp/ext/collect/schedule_job.do
編寫一個(gè)獲取錄入界面的地址:/cmscp/ext/collect/schedule_job.do,這里使用相對路徑,相對于定時(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的值,對應(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對應(yīng)定時(shí)任務(wù)實(shí)現(xiàn)類CollectJob中獲取參數(shù)的方法Integer collectId = map.getIntegerFromString("collectId");
<c:forEach var="collect" items="${collectList}">:此處的${collectList}對應(yīng)CollectController的modelMap.addAttribute("collectList", collectList);。
關(guān)于“jspXCMS怎么開發(fā)定時(shí)任務(wù)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“jspXCMS怎么開發(fā)定時(shí)任務(wù)”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。