Beállítások mentése
A korábbi SAL Scheduling példa szerint az időzítés már megfelelő, de egyelőre kódba égetett értéket használtunk az intervallumra. Ha ezt az action hívásával módosítottuk volna (reschedule), akkor a következő szerver újraindításkor a rendszer elfelejtette volna amit megadtunk. Ennek kezelésére a módosításokat adatbázisba menthetjük saját (ActiveObject) vagy a JIRA által kínált megoldással (PluginSettings).
Időzítő felület
Az időzítés ismétlődési ciklusának megadásához befejezzük a JIRA példa második felének Todo-ba integrálását. Így a SchedulerAction hívásával átállítható lesz az intervallum.
SchedulerAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public class SchedulerAction extends JiraWebActionSupport { private static final long serialVersionUID = -1993489970401317470L; private final TodoMonitor todoMonitor; private long interval; public SchedulerAction(TodoMonitor todoMonitor, PluginSettingsFactory pluginSettingsFactory) { this .todoMonitor = todoMonitor; this .interval = todoMonitor.getInterval(); } @Override protected String doExecute() throws Exception { return SUCCESS; } public String doReschedule() { this .todoMonitor.reschedule( this .interval); return getRedirect( "TodoScheduler.jspa" ); } public long getInterval() { return this .interval; } public void setInterval( long interval) { this .interval = interval; } public Date getLastRun() { return this .todoMonitor.getLastRun(); } public Date getNextRun() { return this .todoMonitor.getNextRun(); } public int getTodoSize() { return this .todoMonitor.getTodoSize(); } public SimpleDateFormat getHunDateFormatter() { return GlobalSettings.createHunDateFormatter(); } } |
PluginScheduler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | private final PluginScheduler pluginScheduler; private final TodoService todoService; private final GlobalSettings settings; private final HashMap<String, Object> jobDataMap = new HashMap<String, Object>(); private Date lastRun; private Date nextRun; private long interval = 0 ; public TodoMonitorImpl(PluginScheduler pluginScheduler, TodoService todoService, PluginSettingsFactory pluginSettingsFactory) { this .pluginScheduler = pluginScheduler; this .todoService = todoService; this .settings = new GlobalSettings(pluginSettingsFactory.createGlobalSettings()); Long loadInterval = this .settings.loadInterval(); if (loadInterval != null ) { this .interval = loadInterval; } } @Override public void onStart() { this .jobDataMap.put( "TodoMonitorImpl:instance" , TodoMonitorImpl. this ); this .jobDataMap.put( "TodoService" , this .todoService); if ( this .interval > 0 ) { schedule(); } } @Override public void reschedule( long interval) { this .interval = interval; this .settings.storeInterval(interval); this .schedule(); } |
PluginSettings
A beállításokat akár a PluginSettingsFactory-n keresztül is közvetlenül elérhetjük, de érdemes a funkciót kiemelni. Így az átállított értéket a GlobalSettings osztályon keresztül tölthetjük be és menthetjük el, ezzel leválasztva a beállított értékek kezelését a kód többi funkciójától.
GlobalSettings,java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class GlobalSettings { private final PluginSettings pluginSettings; public GlobalSettings(PluginSettings pluginSettings) { this .pluginSettings = pluginSettings; } public Long loadLong(String key) { Object object = this .pluginSettings.get(key); if (object == null ) { return null ; } return Long.valueOf((String) object); } public Long loadInterval() { return this .loadLong( "todo.monitor.interval" ); } public void storeInterval(Long interval) { this .pluginSettings.put( "todo.monitor.interval" , String.valueOf(interval)); } public static SimpleDateFormat createHunDateFormatter() { return new SimpleDateFormat( "yyyy.MM.dd. HH:mm" ); } } |
Plugin konfigurációs hivatkozás
A pluginhez tartozó beállítási oldalt kiemelhetjük a JIRA admin oldalára is a configure.url paraméter megadásával. Itt az időzítés beállításához használt TodoScheduler.jspa oldalt adjuk meg.
atlassian-plugin.xml
1 2 3 4 5 6 7 8 9 10 11 12 | <? xml version = "1.0" encoding = "UTF-8" ?> < atlassian-plugin key = "${project.groupId}.${project.artifactId}" name = "${project.name}" plugins-version = "2" > < plugin-info > < description >${project.description} < version >${project.version} < vendor name = "${project.organization.name}" url = "${project.organization.url}" /> < param name = "plugin-icon" >images/pluginIcon.png</ param > < param name = "plugin-logo" >images/pluginLogo.png</ param > < param name = "configure.url" >/secure/TodoScheduler.jspa</ param > </ plugin-info > ... </ atlassian-plugin > |
Az admin oldalról a plugin beállításokat megnyitva vagy közvetlenül elérve (http://localhost:2990/jira/plugins/servlet/upm) a Configure linkre kattintva érhetjük el a plugin beállításokat.
Eredmény
A szerver indítása után még nincs beállított érték, de ha az időzítő felületen megadunk egyet, akkor az "Apply" után visszatérve már látjuk a következő futtatási időt (Legközelebb). Ha lefutott az első task, utána már a "Legutóbb" dátum is látható, és ezzel egy időben nő a rekordok száma is.
Apró hiba a működésben: Az időzítés első futásakor ismétlődően fut le a task, amit a 2.7.x verzióban még nem javítottak.
A fenti módosítások letölthetők SVN-en keresztül és becsomagolva a blog GoogleCode oldaláról a JiraDevTutor projektből.