Search in sources :

Example 6 with WorkException

use of javax.resource.spi.work.WorkException in project cxf by apache.

the class WorkManagerThreadPool method workRejected.

@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);
    }
}
Also used : WorkException(javax.resource.spi.work.WorkException)

Example 7 with WorkException

use of javax.resource.spi.work.WorkException in project spring-framework by spring-projects.

the class SimpleTaskWorkManager method executeWork.

/**
	 * Execute the given Work on the specified TaskExecutor.
	 * @param taskExecutor the TaskExecutor to use
	 * @param work the Work to execute
	 * @param startTimeout the time duration within which the Work is supposed to start
	 * @param blockUntilStarted whether to block until the Work has started
	 * @param executionContext the JCA ExecutionContext for the given Work
	 * @param workListener the WorkListener to clal for the given Work
	 * @return the time elapsed from Work acceptance until start of execution
	 * (or -1 if not applicable or not known)
	 * @throws WorkException if the TaskExecutor did not accept the Work
	 */
protected long executeWork(TaskExecutor taskExecutor, Work work, long startTimeout, boolean blockUntilStarted, ExecutionContext executionContext, WorkListener workListener) throws WorkException {
    if (executionContext != null && executionContext.getXid() != null) {
        throw new WorkException("SimpleTaskWorkManager does not supported imported XIDs: " + executionContext.getXid());
    }
    WorkListener workListenerToUse = workListener;
    if (workListenerToUse == null) {
        workListenerToUse = new WorkAdapter();
    }
    boolean isAsync = (taskExecutor instanceof AsyncTaskExecutor);
    DelegatingWorkAdapter workHandle = new DelegatingWorkAdapter(work, workListenerToUse, !isAsync);
    try {
        if (isAsync) {
            ((AsyncTaskExecutor) taskExecutor).execute(workHandle, startTimeout);
        } else {
            taskExecutor.execute(workHandle);
        }
    } catch (TaskTimeoutException ex) {
        WorkException wex = new WorkRejectedException("TaskExecutor rejected Work because of timeout: " + work, ex);
        wex.setErrorCode(WorkException.START_TIMED_OUT);
        workListenerToUse.workRejected(new WorkEvent(this, WorkEvent.WORK_REJECTED, work, wex));
        throw wex;
    } catch (TaskRejectedException ex) {
        WorkException wex = new WorkRejectedException("TaskExecutor rejected Work: " + work, ex);
        wex.setErrorCode(WorkException.INTERNAL);
        workListenerToUse.workRejected(new WorkEvent(this, WorkEvent.WORK_REJECTED, work, wex));
        throw wex;
    } catch (Throwable ex) {
        WorkException wex = new WorkException("TaskExecutor failed to execute Work: " + work, ex);
        wex.setErrorCode(WorkException.INTERNAL);
        throw wex;
    }
    if (isAsync) {
        workListenerToUse.workAccepted(new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null));
    }
    if (blockUntilStarted) {
        long acceptanceTime = System.currentTimeMillis();
        synchronized (workHandle.monitor) {
            try {
                while (!workHandle.started) {
                    workHandle.monitor.wait();
                }
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
        return (System.currentTimeMillis() - acceptanceTime);
    } else {
        return WorkManager.UNKNOWN;
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) WorkException(javax.resource.spi.work.WorkException) WorkRejectedException(javax.resource.spi.work.WorkRejectedException) WorkListener(javax.resource.spi.work.WorkListener) AsyncTaskExecutor(org.springframework.core.task.AsyncTaskExecutor) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) WorkEvent(javax.resource.spi.work.WorkEvent) TaskTimeoutException(org.springframework.core.task.TaskTimeoutException) WorkAdapter(javax.resource.spi.work.WorkAdapter)

Example 8 with WorkException

use of javax.resource.spi.work.WorkException in project spring-framework by spring-projects.

the class WorkManagerTaskExecutor method execute.

@Override
public void execute(Runnable task, long startTimeout) {
    Assert.state(this.workManager != null, "No WorkManager specified");
    Work work = new DelegatingWork(this.taskDecorator != null ? this.taskDecorator.decorate(task) : task);
    try {
        if (this.blockUntilCompleted) {
            if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) {
                this.workManager.doWork(work, startTimeout, null, this.workListener);
            } else {
                this.workManager.doWork(work);
            }
        } else if (this.blockUntilStarted) {
            if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) {
                this.workManager.startWork(work, startTimeout, null, this.workListener);
            } else {
                this.workManager.startWork(work);
            }
        } else {
            if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) {
                this.workManager.scheduleWork(work, startTimeout, null, this.workListener);
            } else {
                this.workManager.scheduleWork(work);
            }
        }
    } catch (WorkRejectedException ex) {
        if (WorkException.START_TIMED_OUT.equals(ex.getErrorCode())) {
            throw new TaskTimeoutException("JCA WorkManager rejected task because of timeout: " + task, ex);
        } else {
            throw new TaskRejectedException("JCA WorkManager rejected task: " + task, ex);
        }
    } catch (WorkException ex) {
        throw new SchedulingException("Could not schedule task on JCA WorkManager", ex);
    }
}
Also used : TaskTimeoutException(org.springframework.core.task.TaskTimeoutException) TaskRejectedException(org.springframework.core.task.TaskRejectedException) WorkRejectedException(javax.resource.spi.work.WorkRejectedException) WorkException(javax.resource.spi.work.WorkException) Work(javax.resource.spi.work.Work) SchedulingException(org.springframework.scheduling.SchedulingException)

Example 9 with WorkException

use of javax.resource.spi.work.WorkException in project teiid by teiid.

the class FakeWorkManager method execute.

void execute(final Work arg0, final WorkListener arg3, boolean join) throws WorkException {
    if (arg3 != null) {
        arg3.workAccepted(Mockito.mock(WorkEvent.class));
        arg3.workStarted(Mockito.mock(WorkEvent.class));
    }
    t = new Thread(new Runnable() {

        @Override
        public void run() {
            arg0.run();
            if (arg3 != null) {
                arg3.workCompleted(Mockito.mock(WorkEvent.class));
            }
        }
    });
    t.start();
    if (join) {
        try {
            t.join();
        } catch (InterruptedException e) {
            throw new WorkException(e);
        }
    }
}
Also used : WorkException(javax.resource.spi.work.WorkException) WorkEvent(javax.resource.spi.work.WorkEvent)

Example 10 with WorkException

use of javax.resource.spi.work.WorkException in project tomee by apache.

the class SimpleWorkManager method executeWork.

private long executeWork(final WorkType workType, final Work work, final long startTimeout, final ExecutionContext executionContext, WorkListener workListener) throws WorkException {
    // assure we have a work listener
    if (workListener == null) {
        workListener = new LoggingWorkListener(workType);
    }
    // reject work with an XID
    if (executionContext != null && executionContext.getXid() != null) {
        final WorkRejectedException workRejectedException = new WorkRejectedException("SimpleWorkManager can not import an XID", WorkException.TX_RECREATE_FAILED);
        workListener.workRejected(new WorkEvent(this, WorkEvent.WORK_REJECTED, work, workRejectedException));
        throw workRejectedException;
    }
    // accecpt all other work
    workListener.workAccepted(new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null));
    // execute work
    final Worker worker = new Worker(work, workListener, startTimeout);
    executor.execute(worker);
    if (workType == WorkType.DO) {
        // wait for completion
        try {
            worker.waitForCompletion();
        } catch (final InterruptedException e) {
            final WorkException workException = new WorkException("Work submission thread was interrupted", e);
            workException.setErrorCode(WorkException.INTERNAL);
            throw workException;
        }
        // if work threw an exception, rethrow it
        final WorkException workCompletedException = worker.getWorkException();
        if (workCompletedException != null) {
            throw workCompletedException;
        }
    } else if (workType == WorkType.START) {
        // wait for work to start
        try {
            worker.waitForStart();
        } catch (final InterruptedException e) {
            final WorkException workException = new WorkException("Work submission thread was interrupted", e);
            workException.setErrorCode(WorkException.INTERNAL);
            throw workException;
        }
        // if work threw a rejection exception, rethrow it (it is the exception for timeout)
        final WorkException workCompletedException = worker.getWorkException();
        if (workCompletedException instanceof WorkRejectedException) {
            throw workCompletedException;
        }
    }
    return worker.getStartDelay();
}
Also used : WorkRejectedException(javax.resource.spi.work.WorkRejectedException) WorkException(javax.resource.spi.work.WorkException) WorkEvent(javax.resource.spi.work.WorkEvent)

Aggregations

WorkException (javax.resource.spi.work.WorkException)12 WorkEvent (javax.resource.spi.work.WorkEvent)4 WorkRejectedException (javax.resource.spi.work.WorkRejectedException)4 CancellationException (java.util.concurrent.CancellationException)2 CompletionException (java.util.concurrent.CompletionException)2 Work (javax.resource.spi.work.Work)2 WorkCompletedException (javax.resource.spi.work.WorkCompletedException)2 TaskRejectedException (org.springframework.core.task.TaskRejectedException)2 TaskTimeoutException (org.springframework.core.task.TaskTimeoutException)2 CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)2 JavaEETransactionManager (com.sun.enterprise.transaction.api.JavaEETransactionManager)1 GlobalTID (com.sun.jts.CosTransactions.GlobalTID)1 Serializable (java.io.Serializable)1 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Callable (java.util.concurrent.Callable)1 CompletionStage (java.util.concurrent.CompletionStage)1 ExecutionException (java.util.concurrent.ExecutionException)1