View Javadoc

1   /*
2    * $Id: PollEndpointJob.java 22396 2011-07-12 21:26:04Z mike.schilling $
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  package org.mule.transport.quartz.jobs;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.context.MuleContextAware;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.lifecycle.Lifecycle;
18  import org.mule.transport.AbstractConnector;
19  import org.mule.transport.AbstractPollingMessageReceiver;
20  
21  import org.quartz.Job;
22  import org.quartz.JobExecutionContext;
23  import org.quartz.JobExecutionException;
24  
25  public class PollEndpointJob extends AbstractJob implements Lifecycle, MuleContextAware
26  {
27      private String inboundPollingEndpointName;
28      private AbstractPollingMessageReceiver receiver;
29      
30      public PollEndpointJob(String inboundPollingEndpointName)
31      {
32          this.inboundPollingEndpointName = inboundPollingEndpointName;
33      }
34  
35      protected void doExecute(JobExecutionContext context) throws JobExecutionException
36      {
37          try
38          {
39              receiver.performPoll();
40          }
41          catch (Exception e)
42          {
43              throw new JobExecutionException(e);
44          }
45          
46      }
47  
48      public void initialise() throws InitialisationException
49      {
50          //DO NOTHING
51      }
52  
53      public void start() throws MuleException
54      {
55          InboundEndpoint endpoint = (InboundEndpoint) muleContext.getRegistry().lookupObject(this.inboundPollingEndpointName);
56          AbstractConnector connector = (AbstractConnector) endpoint.getConnector();
57          receiver = (AbstractPollingMessageReceiver) connector.getReceiver(null, endpoint);
58          receiver.disableNativeScheduling();
59      }
60  
61      public void stop() throws MuleException
62      {
63          //DO NOTHING
64      }
65  
66      public void dispose()
67      {
68          //DO NOTHING
69      }
70  
71      @Override
72      protected MuleContext getMuleContext(JobExecutionContext jobExecutionContext) throws JobExecutionException
73      {
74          return muleContext;
75      }
76  
77      public void setMuleContext(MuleContext context)
78      {
79          this.muleContext = context;
80      }
81  }