alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Apache CXF example source code file (WorkManagerThreadPool.java)

This example Apache CXF source code file (WorkManagerThreadPool.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Apache CXF tags/keywords

cxfworkadapter, interruptedexception, override, runnable, runnable, threadpool, work, workexception, workexception, workimpl, workimpl, workmanager, workmanagerthreadpool, workmanagerthreadpool

The Apache CXF WorkManagerThreadPool.java source code

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 org.apache.cxf.jca.cxf;

import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkEvent;
import javax.resource.spi.work.WorkException;
import javax.resource.spi.work.WorkManager;

import org.eclipse.jetty.util.thread.ThreadPool;

/**
 * The adapter for using Application Server's thread pool. 
 * Just simply override the dispatch method.
 */
public class WorkManagerThreadPool extends CXFWorkAdapter implements ThreadPool {
    
    private WorkManager workManager;
    
    private boolean isLowOnThreads;
    
    private Runnable theJob;
    
    public WorkManagerThreadPool(WorkManager wm) {
        this.workManager = wm;
    }
    
    public boolean dispatch(Runnable job) {
        try {
            theJob = job;
            workManager.startWork(new WorkImpl(job), DEFAULT_START_TIME_OUT, null, this);
            return true;
        } catch (WorkException e) {
            e.printStackTrace();
            return false;
        }
    }

    
    public int getIdleThreads() {
        return 0;
    }

    
    public int getThreads() {
        return 1;
    }

    
    public boolean isLowOnThreads() {
        return isLowOnThreads;
    }
    
    
    void setIsLowOnThreads(boolean isLow) {
        this.isLowOnThreads = isLow;
    }
    
    public void join() throws InterruptedException {
        //Do nothing
    }
    
    public class WorkImpl implements Work {
        
        private Runnable job;
        
        public WorkImpl(Runnable job) {
            this.job = job;
        }
        
        public void run() {
            job.run();
        }
        
        public void release() {
            //empty
        }
    }
    
    @Override
    public void workRejected(WorkEvent e) {
        super.workRejected(e);
        WorkException we = e.getException();
        if (WorkException.START_TIMED_OUT.equals(we.getErrorCode()) && !isLowOnThreads) {
            setIsLowOnThreads(true);
            dispatch(theJob);
        }
    }

}

Other Apache CXF examples (source code examples)

Here is a short list of links related to this Apache CXF WorkManagerThreadPool.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.