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