Search in sources :

Example 11 with TaskRejectedException

use of org.springframework.core.task.TaskRejectedException in project spring-framework by spring-projects.

the class ThreadPoolTaskScheduler method scheduleWithFixedDelay.

@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
    ScheduledExecutorService executor = getScheduledExecutor();
    long initialDelay = startTime.getTime() - System.currentTimeMillis();
    try {
        return executor.scheduleWithFixedDelay(errorHandlingTask(task, true), initialDelay, delay, TimeUnit.MILLISECONDS);
    } catch (RejectedExecutionException ex) {
        throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 12 with TaskRejectedException

use of org.springframework.core.task.TaskRejectedException 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 13 with TaskRejectedException

use of org.springframework.core.task.TaskRejectedException 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 14 with TaskRejectedException

use of org.springframework.core.task.TaskRejectedException in project spring-framework by spring-projects.

the class TaskExecutorAdapter method submitListenable.

@Override
public ListenableFuture<?> submitListenable(Runnable task) {
    try {
        ListenableFutureTask<Object> future = new ListenableFutureTask<>(task, null);
        doExecute(this.concurrentExecutor, this.taskDecorator, future);
        return future;
    } catch (RejectedExecutionException ex) {
        throw new TaskRejectedException("Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 15 with TaskRejectedException

use of org.springframework.core.task.TaskRejectedException in project spring-framework by spring-projects.

the class TaskExecutorAdapter method submit.

@Override
public Future<?> submit(Runnable task) {
    try {
        if (this.taskDecorator == null && this.concurrentExecutor instanceof ExecutorService) {
            return ((ExecutorService) this.concurrentExecutor).submit(task);
        } else {
            FutureTask<Object> future = new FutureTask<>(task, null);
            doExecute(this.concurrentExecutor, this.taskDecorator, future);
            return future;
        }
    } catch (RejectedExecutionException ex) {
        throw new TaskRejectedException("Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) FutureTask(java.util.concurrent.FutureTask) ExecutorService(java.util.concurrent.ExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

TaskRejectedException (org.springframework.core.task.TaskRejectedException)15 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)11 ListenableFutureTask (org.springframework.util.concurrent.ListenableFutureTask)8 ExecutorService (java.util.concurrent.ExecutorService)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)5 FutureTask (java.util.concurrent.FutureTask)2 WorkException (javax.resource.spi.work.WorkException)2 WorkRejectedException (javax.resource.spi.work.WorkRejectedException)2 TaskTimeoutException (org.springframework.core.task.TaskTimeoutException)2 SchedulingException (org.springframework.scheduling.SchedulingException)2 Work (commonj.work.Work)1 WorkException (commonj.work.WorkException)1 WorkRejectedException (commonj.work.WorkRejectedException)1 UnknownMessageTypeException (cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException)1 InvalidDestinationException (javax.jms.InvalidDestinationException)1 JMSException (javax.jms.JMSException)1 TextMessage (javax.jms.TextMessage)1 Work (javax.resource.spi.work.Work)1 WorkAdapter (javax.resource.spi.work.WorkAdapter)1 WorkEvent (javax.resource.spi.work.WorkEvent)1