001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /*
019: * ChainBuilder ESB
020: * Visual Enterprise Integration
021: *
022: * Copyright (C) 2006 Bostech Corporation
023: *
024: * This program is free software; you can redistribute it and/or modify it
025: * under the terms of the GNU General Public License as published by the
026: * Free Software Foundation; either version 2 of the License, or (at your option)
027: * any later version.
028: *
029: * This program is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
031: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
032: * for more details.
033: *
034: * You should have received a copy of the GNU General Public License along with
035: * this program; if not, write to the Free Software Foundation, Inc.,
036: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
037: *
038: * $Id: WorkManagerImpl.java 9243 2007-09-25 00:49:54Z lzheng $
039: */
040: package com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging;
041:
042: import javax.resource.spi.work.ExecutionContext;
043: import javax.resource.spi.work.Work;
044: import javax.resource.spi.work.WorkException;
045: import javax.resource.spi.work.WorkListener;
046: import javax.resource.spi.work.WorkManager;
047:
048: import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
049: import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
050: import edu.emory.mathcs.backport.java.util.concurrent.Executors;
051:
052: public class WorkManagerImpl implements WorkManager {
053:
054: private final ExecutorService executor;
055:
056: public WorkManagerImpl() {
057: executor = Executors.newCachedThreadPool();
058: }
059:
060: public void shutDown() {
061: executor.shutdown();
062: }
063:
064: /* (non-Javadoc)
065: * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work)
066: */
067: public void doWork(Work work) throws WorkException {
068: work.run();
069: }
070:
071: /* (non-Javadoc)
072: * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
073: */
074: public void doWork(Work work, long startTimeout,
075: ExecutionContext execContext, WorkListener workListener)
076: throws WorkException {
077: throw new RuntimeException("Not implemented");
078: }
079:
080: /* (non-Javadoc)
081: * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work)
082: */
083: public long startWork(final Work work) throws WorkException {
084: final CountDownLatch latch = new CountDownLatch(1);
085: executor.execute(new Work() {
086: public void release() {
087: work.release();
088: }
089:
090: public void run() {
091: latch.countDown();
092: work.run();
093: }
094: });
095: long t = System.currentTimeMillis();
096: try {
097: latch.await();
098: } catch (InterruptedException e) {
099: throw new WorkException("Interrupted", e);
100: }
101: return System.currentTimeMillis() - t;
102: }
103:
104: /* (non-Javadoc)
105: * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
106: */
107: public long startWork(Work work, long startTimeout,
108: ExecutionContext execContext, WorkListener workListener)
109: throws WorkException {
110: throw new RuntimeException("Not implemented");
111: }
112:
113: /* (non-Javadoc)
114: * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work)
115: */
116: public void scheduleWork(Work work) throws WorkException {
117: executor.execute(work);
118: }
119:
120: /* (non-Javadoc)
121: * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
122: */
123: public void scheduleWork(Work work, long startTimeout,
124: ExecutionContext execContext, WorkListener workListener)
125: throws WorkException {
126: throw new RuntimeException("Not implemented");
127: }
128:
129: }
|