Hi Thomas

yes, this is really annoying

you have to bypass the JEE standard and go directly inside the native
Quartz implementation to get what you want

try something like this

import java.util.ArrayList;
import java.util.List;

import javax.ejb.ScheduleExpression;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.inject.Inject;
import javax.jms.JMSException;

import org.apache.openejb.core.timer.TimerData;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;

(...)
        @SuppressWarnings("unchecked")
        public List<QuartzTrigger> listAllTimers() throws SchedulerException {

                List<QuartzTrigger> timers = new ArrayList<QuartzTrigger>();
                
                Scheduler scheduler = this.baseService.getScheduler();
                
                for (String groupName : scheduler.getJobGroupNames()) {
                        for (JobKey jobKey :
scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
                                for (Trigger trigger : (List<Trigger>) 
scheduler.getTriggersOfJob(jobKey)) {
                                        TimerData timerData = 
(TimerData)trigger.getJobDataMap().get("TIMER_DATA");
                                        String info = 
(String)timerData.getInfo();
//                                      System.out.println(info);
                                        if (info != null){
                                                QuartzTrigger qt = new 
QuartzTrigger();
                                                qt.setDescription(info);
                                                qt.setKey(trigger.getKey());
                                                
qt.setStatus(scheduler.getTriggerState(trigger.getKey()).toString());
                                                
qt.setLastFire(trigger.getPreviousFireTime());
                                                
qt.setNextFire(trigger.getNextFireTime());
                                                timers.add(qt);
                                        }
                                }
                        }
                }

                return timers;
        }

QuartzTrigger is just a bean like this

import java.io.Serializable;
import java.util.Date;
import org.quartz.TriggerKey;

public class QuartzTrigger  implements Serializable{
        private static final long serialVersionUID = -6577490874256837028L;
        private String description;
        private TriggerKey key;
        private String status;
        private Date nextFire;
        private Date lastFire;
(...)

and the scheduler is

scheduler = new StdSchedulerFactory().getScheduler("myScheduler");              

and don't forget to put this name (myScheduler) at
application.properties, like this

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreCMT
org.quartz.jobStore.dataSource=quartzDS
org.quartz.jobStore.nonManagedTXDataSource = quartzDS2
org.quartz.scheduler.instanceName = myScheduler
org.quartz.jobStore.isClustered=false
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.threadsInheritContextClassLoaderOfInitializer=true
org.quartz.scheduler.makeSchedulerThreadDaemon=true
org.quartz.jobStore.makeThreadsDaemons=true
org.quartz.threadPool.threadCount = 10

org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.driverDelegateClass =
org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.dataSource.quartzDS.jndiURL=openejb:Resource/jdbc/quartzDS
org.quartz.dataSource.quartzDS2.jndiURL=openejb:Resource/jdbc/quartzDS2

as you know, application.properties goes into
/YourApp/WebContent/WEB-INF/application.properties

and the quartz datastore may be configured at tomee.xml as


        <Resource id="jdbc/quartzDS" type="DataSource">
                JdbcDriver com.mysql.jdbc.Driver
                JdbcUrl jdbc:mysql://localhost:3306/quartz
                UserName xxx
                Password xxx
                JtaManaged true
        </Resource>

        <Resource id="jdbc/quartzDS2" type="DataSource">
                JdbcDriver com.mysql.jdbc.Driver
                JdbcUrl jdbc:mysql://localhost:3306/quartz
                UserName xxx
                Password xxx
                JtaManaged false
        </Resource>

[]

Leo


On Tue, Jul 30, 2013 at 10:09 AM, tschuler <[email protected]> wrote:
> Hi!
>
> We use a scheduled bean, the added timers are persisted in a database table.
> After server restart the timers still work and the scheduled bean is
> triggered as expected.
>
> But we get no access to the timers any more and therefore cannot cancel
> them.
> The injected TimerService on the scheduled bean does not provide the timers
> added before server restart within its getTimers() method.
>
> Is there another way to get access to the timers for a scheduled bean after
> server restart?
>
> Best regards,
> Thomas
>
>
>
> --
> View this message in context: 
> http://openejb.979440.n4.nabble.com/No-access-to-timers-after-server-restart-tp4664425.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.

Reply via email to