Coverage Report - org.mule.transport.quartz.QuartzMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
QuartzMessageReceiver
85%
52/61
64%
9/14
3
 
 1  
 /*
 2  
  * $Id: QuartzMessageReceiver.java 11613 2008-04-20 20:30:10Z rossmason $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.quartz;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.endpoint.EndpointException;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.api.lifecycle.CreateException;
 17  
 import org.mule.api.service.Service;
 18  
 import org.mule.api.transport.Connector;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 import org.mule.transport.AbstractMessageReceiver;
 21  
 import org.mule.transport.quartz.i18n.QuartzMessages;
 22  
 import org.mule.transport.quartz.config.JobConfig;
 23  
 
 24  
 import java.util.Date;
 25  
 
 26  
 import org.quartz.CronTrigger;
 27  
 import org.quartz.JobDataMap;
 28  
 import org.quartz.JobDetail;
 29  
 import org.quartz.ObjectAlreadyExistsException;
 30  
 import org.quartz.Scheduler;
 31  
 import org.quartz.SimpleTrigger;
 32  
 import org.quartz.Trigger;
 33  
 
 34  
 /**
 35  
  * Listens for Quartz sheduled events using the Receiver Job and fires events to the
 36  
  * service associated with this receiver.
 37  
  */
 38  
 public class QuartzMessageReceiver extends AbstractMessageReceiver
 39  
 {
 40  
 
 41  
     public static final String QUARTZ_RECEIVER_PROPERTY = "mule.quartz.receiver";
 42  
     public static final String QUARTZ_CONNECTOR_PROPERTY = "mule.quartz.connector";
 43  
 
 44  
     private final QuartzConnector connector;
 45  
 
 46  
     public QuartzMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint)
 47  
             throws CreateException
 48  
     {
 49  62
         super(connector, service, endpoint);
 50  62
         this.connector = (QuartzConnector) connector;
 51  62
     }
 52  
 
 53  
     protected void doDispose()
 54  
     {
 55  
         // template method
 56  124
     }
 57  
 
 58  
     protected void doStart() throws MuleException
 59  
     {
 60  
         try
 61  
         {
 62  60
             Scheduler scheduler = connector.getQuartzScheduler();
 63  
 
 64  60
             JobConfig jobConfig = (JobConfig)endpoint.getProperty(QuartzConnector.PROPERTY_JOB_CONFIG);
 65  60
             if(jobConfig==null)
 66  
             {
 67  0
                 throw new IllegalArgumentException(CoreMessages.objectIsNull(QuartzConnector.PROPERTY_JOB_CONFIG).getMessage());
 68  
             }
 69  60
             JobDetail jobDetail = new JobDetail();
 70  60
             jobDetail.setName(endpoint.getEndpointURI().getAddress());
 71  60
             jobDetail.setJobClass(jobConfig.getJobClass());
 72  60
             JobDataMap jobDataMap = new JobDataMap();
 73  60
             jobDataMap.put(QUARTZ_RECEIVER_PROPERTY, this.getReceiverKey());
 74  60
             jobDataMap.put(QUARTZ_CONNECTOR_PROPERTY, this.connector.getName());
 75  60
             jobDataMap.putAll(endpoint.getProperties());
 76  60
             jobDetail.setJobDataMap(jobDataMap);
 77  
 
 78  
             Trigger trigger;
 79  60
             String cronExpression = (String)endpoint.getProperty(QuartzConnector.PROPERTY_CRON_EXPRESSION);
 80  60
             String repeatInterval = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_INTERVAL);
 81  60
             String repeatCount = (String)endpoint.getProperty(QuartzConnector.PROPERTY_REPEAT_COUNT);
 82  60
             String startDelay = (String)endpoint.getProperty(QuartzConnector.PROPERTY_START_DELAY);
 83  60
             String groupName = jobConfig.getGroupName();
 84  60
             String jobGroupName = jobConfig.getJobGroupName();
 85  
 
 86  60
             if (groupName == null)
 87  
             {
 88  0
                 groupName = QuartzConnector.DEFAULT_GROUP_NAME;
 89  
             }
 90  60
             if (jobGroupName == null)
 91  
             {
 92  0
                 jobGroupName = groupName;
 93  
             }
 94  
 
 95  60
             jobDetail.setGroup(groupName);
 96  
 
 97  60
             if (cronExpression != null)
 98  
             {
 99  36
                 CronTrigger ctrigger = new CronTrigger();
 100  36
                 ctrigger.setCronExpression(cronExpression);
 101  36
                 trigger = ctrigger;
 102  36
             }
 103  24
             else if (repeatInterval != null)
 104  
             {
 105  24
                 SimpleTrigger strigger = new SimpleTrigger();
 106  24
                 strigger.setRepeatInterval(Long.parseLong(repeatInterval));
 107  24
                 if (repeatCount != null)
 108  
                 {
 109  22
                     strigger.setRepeatCount(Integer.parseInt(repeatCount));
 110  
                 }
 111  
                 else
 112  
                 {
 113  2
                     strigger.setRepeatCount(-1);
 114  
                 }
 115  24
                 trigger = strigger;
 116  24
             }
 117  
             else
 118  
             {
 119  0
                 throw new IllegalArgumentException(
 120  
                         QuartzMessages.cronExpressionOrIntervalMustBeSet().getMessage());
 121  
             }
 122  60
             long start = System.currentTimeMillis();
 123  60
             if (startDelay != null)
 124  
             {
 125  0
                 start += Long.parseLong(startDelay);
 126  
             }
 127  60
             trigger.setStartTime(new Date(start));
 128  60
             trigger.setName(endpoint.getEndpointURI().getAddress());
 129  60
             trigger.setGroup(groupName);
 130  60
             trigger.setJobName(endpoint.getEndpointURI().getAddress());
 131  60
             trigger.setJobGroup(jobGroupName);
 132  
 
 133  
             // We need to handle cases when the job has already been
 134  
             // persisted
 135  
             try
 136  
             {
 137  60
                 scheduler.scheduleJob(jobDetail, trigger);
 138  
             }
 139  0
             catch (ObjectAlreadyExistsException oaee)
 140  
             {
 141  
                 // Do anything here?
 142  0
                 logger.warn("A quartz Job with name: " + endpoint.getEndpointURI().getAddress() +
 143  
                         " has already been registered. Cannot register again");
 144  60
             }
 145  
 
 146  60
             scheduler.start();
 147  
         }
 148  0
         catch (Exception e)
 149  
         {
 150  0
             throw new EndpointException(CoreMessages.failedToStart("Quartz receiver"), e);
 151  60
         }
 152  60
     }
 153  
 
 154  
     protected void doStop() throws MuleException
 155  
     {
 156  
         // nothing to do
 157  60
     }
 158  
 
 159  
     protected void doConnect() throws Exception
 160  
     {
 161  
         // nothing to do
 162  60
     }
 163  
 
 164  
     protected void doDisconnect() throws Exception
 165  
     {
 166  
         // nothing to do
 167  60
     }
 168  
 
 169  
 }