All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.logspace.agent.shaded.quartz.commonj.WorkManagerThreadExecutor Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show newest version
/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */
 package io.logspace.agent.shaded.quartz.commonj;

import commonj.work.Work;
import commonj.work.WorkManager;
import io.logspace.agent.shaded.quartz.spi.ThreadExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * CommonJ WorkManager implementation of hacked Quartz ThreadExecutor class.
 * This class schedules work on a WorkManager which is looked up in JNDI. The
 * advantage is that all the work performed is done on a managed thread which is
 * required by WebSphere, see QUARTZ-743 for
 * details.
 *
 * @author matt.accola
 * @version $Revision: 2243 $ $Date: 2013-04-30 11:39:48 -0700 (Tue, 30 Apr 2013) $
 */
public class WorkManagerThreadExecutor implements ThreadExecutor {
    private final Logger log = LoggerFactory.getLogger(getClass());

    private String workManagerName;
    private WorkManager workManager;

    public void execute(Thread thread) {
        Work work = new io.logspace.agent.shaded.quartz.commonj.DelegatingWork(thread);
        try {
            this.workManager.schedule(work);
        } catch (Exception e) {
            log.error("Error attempting to schedule QuartzSchedulerThread: " + e.getMessage(), e);
        }
    }

    public void initialize() {
        try {
            this.workManager = (WorkManager) new InitialContext().lookup(workManagerName);
        } catch (NamingException e) {
            throw new IllegalStateException("Could not locate WorkManager: " + e.getMessage(), e);
        }
    }

    /**
     * Sets the JNDI name of the work manager to use.
     *
     * @param workManagerName the JNDI name to use to lookup the work manager
     */
    public void setWorkManagerName(String workManagerName) {
        this.workManagerName = workManagerName;
    }

}

class DelegatingWork implements Work {

    private final Runnable delegate;

    /**
     * Create a new DelegatingWork.
     *
     * @param delegate the Runnable implementation to delegate to
     */
    public DelegatingWork(Runnable delegate) {
        this.delegate = delegate;
    }

    /**
     * @return the wrapped Runnable implementation.
     */
    public final Runnable getDelegate() {
        return this.delegate;
    }

    /**
     * Delegates execution to the underlying Runnable.
     */
    public void run() {
        this.delegate.run();
    }

    public boolean isDaemon() {
        return false;
    }

    /**
     * This implementation is empty, since we expect the Runnable to terminate
     * based on some specific shutdown signal.
     */
    public void release() {
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy