| 1 |
/* |
| 2 |
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 |
* use this file except in compliance with the License. You may obtain a copy |
| 6 |
* of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
* License for the specific language governing permissions and limitations |
| 14 |
* under the License. |
| 15 |
* |
| 16 |
*/ |
| 17 |
package org.quartz.commonj; |
| 18 |
|
| 19 |
import commonj.work.Work; |
| 20 |
import commonj.work.WorkManager; |
| 21 |
import org.quartz.spi.ThreadExecutor; |
| 22 |
import org.slf4j.Logger; |
| 23 |
import org.slf4j.LoggerFactory; |
| 24 |
|
| 25 |
import javax.naming.InitialContext; |
| 26 |
import javax.naming.NamingException; |
| 27 |
|
| 28 |
/** |
| 29 |
* CommonJ WorkManager implementation of hacked Quartz ThreadExecutor class. |
| 30 |
* This class schedules work on a WorkManager which is looked up in JNDI. The |
| 31 |
* advantage is that all the work performed is done on a managed thread which is |
| 32 |
* required by WebSphere, see <a |
| 33 |
* href="http://jira.opensymphony.com/browse/QUARTZ-743">QUARTZ-743</a> for |
| 34 |
* details. |
| 35 |
* |
| 36 |
* @author matt.accola |
| 37 |
* @version $Revision$ $Date$ |
| 38 |
*/ |
| 39 |
public class WorkManagerThreadExecutor implements ThreadExecutor { |
| 40 |
private final Logger log = LoggerFactory.getLogger(getClass()); |
| 41 |
|
| 42 |
private String workManagerName; |
| 43 |
private WorkManager workManager; |
| 44 |
|
| 45 |
public void execute(Thread thread) { |
| 46 |
Work work = new org.quartz.commonj.DelegatingWork(thread); |
| 47 |
try { |
| 48 |
this.workManager.schedule(work); |
| 49 |
} catch (Exception e) { |
| 50 |
log.error("Error attempting to schedule QuartzSchedulerThread: " + e.getMessage(), e); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public void initialize() { |
| 55 |
try { |
| 56 |
this.workManager = (WorkManager) new InitialContext().lookup(workManagerName); |
| 57 |
} catch (NamingException e) { |
| 58 |
throw new IllegalStateException("Could not locate WorkManager: " + e.getMessage(), e); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Sets the JNDI name of the work manager to use. |
| 64 |
* |
| 65 |
* @param workManagerName the JNDI name to use to lookup the work manager |
| 66 |
*/ |
| 67 |
public void setWorkManagerName(String workManagerName) { |
| 68 |
this.workManagerName = workManagerName; |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
class DelegatingWork implements Work { |
| 74 |
|
| 75 |
private final Runnable delegate; |
| 76 |
|
| 77 |
/** |
| 78 |
* Create a new DelegatingWork. |
| 79 |
* |
| 80 |
* @param delegate the Runnable implementation to delegate to |
| 81 |
*/ |
| 82 |
public DelegatingWork(Runnable delegate) { |
| 83 |
this.delegate = delegate; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return the wrapped Runnable implementation. |
| 88 |
*/ |
| 89 |
public final Runnable getDelegate() { |
| 90 |
return this.delegate; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Delegates execution to the underlying Runnable. |
| 95 |
*/ |
| 96 |
public void run() { |
| 97 |
this.delegate.run(); |
| 98 |
} |
| 99 |
|
| 100 |
public boolean isDaemon() { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* This implementation is empty, since we expect the Runnable to terminate |
| 106 |
* based on some specific shutdown signal. |
| 107 |
*/ |
| 108 |
public void release() { |
| 109 |
} |
| 110 |
|
| 111 |
} |