下面列出了org.springframework.core.task.TaskRejectedException#javax.resource.spi.work.Work 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* start work
*
* @param e workEvent
*/
@Override
public void workRejected(WorkEvent e)
{
if (e.getType() != WorkEvent.WORK_REJECTED)
fail("Wrong rejected type");
timeRejected = System.currentTimeMillis();
super.workRejected(e);
Work work = e.getWork();
if (work instanceof NestProviderWork)
{
NestProviderWork nw = (NestProviderWork) work;
reject.append(nw.getName());
}
}
/**
* Create a new WorkWrapper
*
* @param workManager the work manager
* @param si The security integration
* @param work the work
* @param executionContext the execution context
* @param workListener the WorkListener
* @param startedLatch The latch for when work has started
* @param completedLatch The latch for when work has completed
* @param startTime The start time
* @throws IllegalArgumentException for null work, execution context or a negative start timeout
*/
public WorkWrapper(WorkManagerImpl workManager,
SecurityIntegration si,
Work work,
ExecutionContext executionContext,
WorkListener workListener,
CountDownLatch startedLatch,
CountDownLatch completedLatch,
long startTime)
{
super();
if (workManager == null)
throw new IllegalArgumentException("Null work manager");
if (si == null)
throw new IllegalArgumentException("Null security integration");
if (work == null)
throw new IllegalArgumentException("Null work");
if (executionContext == null)
throw new IllegalArgumentException("Null execution context");
this.workManager = workManager;
this.securityIntegration = si;
this.work = work;
this.executionContext = executionContext;
this.workListener = workListener;
this.startedLatch = startedLatch;
this.completedLatch = completedLatch;
this.startTime = startTime;
this.workContexts = null;
}
public long startWork(Work work, long startTimeout, ExecutionContext execContext, WorkListener workListener)
throws WorkException {
SimpleWorkListener wrappedListener = new SimpleWorkListener(workListener);
wrappedListener.workAccepted(new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null));
SimpleWork task = new SimpleWork();
task.setSource(this);
task.setWork(work);
task.setWorkListener(wrappedListener);
this.executor.submit(task);
return wrappedListener.waitForStart();
}
@Override
public void scheduleWork(Work work, long startTimeout, @Nullable ExecutionContext executionContext, @Nullable WorkListener workListener)
throws WorkException {
Assert.state(this.asyncTaskExecutor != null, "No 'asyncTaskExecutor' set");
executeWork(this.asyncTaskExecutor, work, startTimeout, false, executionContext, workListener);
}
public void stop() {
for (int i = 0; this.workList != null && i < this.workList.size(); i++) {
Work work = this.workList.get(i);
try {
work.release();
} catch (RuntimeException rex) {
logger.debug(rex.getMessage(), rex);
}
}
}
@Override
public long startWork(Work work, long startTimeout, @Nullable ExecutionContext executionContext, @Nullable WorkListener workListener)
throws WorkException {
Assert.state(this.asyncTaskExecutor != null, "No 'asyncTaskExecutor' set");
return executeWork(this.asyncTaskExecutor, work, startTimeout, true, executionContext, workListener);
}
/**
* {@inheritDoc}
*/
@Override
public void scheduleWork(Work work) throws WorkException
{
if (policy == null || selector == null || transport == null ||
work == null || !(work instanceof DistributableWork) || !scheduleWorkDistributionEnabled)
{
localScheduleWork(work);
}
else
{
doFirstChecks(work, WorkManager.INDEFINITE, null);
checkTransport();
DistributableWork dw = (DistributableWork)work;
boolean executed = false;
if (policy.shouldDistribute(this, dw))
{
Address dwmAddress = selector.selectDistributedWorkManager(getLocalAddress(), dw);
if (dwmAddress != null && !getLocalAddress().equals(dwmAddress))
{
transport.scheduleWork(dwmAddress, dw);
executed = true;
}
}
if (!executed)
{
localScheduleWork(work);
}
}
}
@Override
public void scheduleWork(Work work, long startTimeout, @Nullable ExecutionContext executionContext, @Nullable WorkListener workListener)
throws WorkException {
Assert.state(this.asyncTaskExecutor != null, "No 'asyncTaskExecutor' set");
executeWork(this.asyncTaskExecutor, work, startTimeout, false, executionContext, workListener);
}
public void scheduleWork(Work work, long startTimeout, ExecutionContext execContext, WorkListener workListener)
throws WorkException {
// SimpleWorkListener wrappedListener = new SimpleWorkListener(workListener);
// wrappedListener.workAccepted(new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null));
// SimpleWork task = new SimpleWork();
// task.setSource(this);
// task.setWork(work);
// task.setWorkListener(wrappedListener);
// // ScheduledFuture<?> future =
// this.scheduled.scheduleAtFixedRate(task, 0, 1000, TimeUnit.MILLISECONDS);
throw new WorkException("not supported yet!");
}
@Override
public long startWork(Work work, long startTimeout, ExecutionContext executionContext, WorkListener workListener)
throws WorkException {
Assert.state(this.asyncTaskExecutor != null, "No 'asyncTaskExecutor' set");
return executeWork(this.asyncTaskExecutor, work, startTimeout, true, executionContext, workListener);
}
@Override
public long startWork(Work work,
long l,
ExecutionContext executionContext,
WorkListener workListener) throws WorkException {
return 0;
}
/**
* Do first checks for work starting methods
* @param work to check
* @param startTimeout to check
* @param execContext to check
* @throws WorkException in case of check don't pass
*/
public void doFirstChecks(Work work, long startTimeout, ExecutionContext execContext) throws WorkException
{
if (isShutdown())
throw new WorkRejectedException(bundle.workmanagerShutdown());
if (work == null)
throw new WorkRejectedException(bundle.workIsNull());
if (startTimeout < 0)
throw new WorkRejectedException(bundle.startTimeoutIsNegative(startTimeout));
checkAndVerifyWork(work, execContext);
}
/**
* Fire complete for HintsContext
* @param work The work instance
*/
private void fireHintsComplete(Work work)
{
if (work != null && work instanceof WorkContextProvider)
{
WorkContextProvider wcProvider = (WorkContextProvider) work;
List<WorkContext> contexts = wcProvider.getWorkContexts();
if (contexts != null && !contexts.isEmpty())
{
Iterator<WorkContext> it = contexts.iterator();
while (it.hasNext())
{
WorkContext wc = it.next();
if (wc instanceof HintsContext)
{
HintsContext hc = (HintsContext) wc;
if (hc instanceof WorkContextLifecycleListener)
{
WorkContextLifecycleListener listener = (WorkContextLifecycleListener)hc;
listener.contextSetupComplete();
}
}
}
}
}
}
/**
* Check and verify work before submitting.
* @param work the work instance
* @param executionContext any execution context that is passed by apadater
* @throws WorkException if any exception occurs
*/
private void checkAndVerifyWork(Work work, ExecutionContext executionContext) throws WorkException
{
if (specCompliant)
{
verifyWork(work);
}
if (work instanceof WorkContextProvider && executionContext != null)
{
//Implements WorkContextProvider and not-null ExecutionContext
throw new WorkRejectedException(bundle.workExecutionContextMustNullImplementsWorkContextProvider());
}
}
/**
* {@inheritDoc}
*/
public void localDoWork(Work work) throws WorkException
{
if (transport != null)
{
checkTransport();
if (getLongRunningThreadPool() != null && WorkManagerUtil.isLongRunning(work))
{
transport.updateLongRunningFree(getLocalAddress(),
getLongRunningThreadPool().getNumberOfFreeThreads() - 1);
}
else
{
transport.updateShortRunningFree(getLocalAddress(),
getShortRunningThreadPool().getNumberOfFreeThreads() - 1);
}
WorkEventListener wel = new WorkEventListener(WorkManagerUtil.isLongRunning(work),
getShortRunningThreadPool(),
getLongRunningThreadPool(),
getLocalAddress(),
transport);
super.doWork(work, WorkManager.INDEFINITE, null, wel);
}
else
{
super.doWork(work);
}
}
/**
* {@inheritDoc}
*/
public long localStartWork(Work work) throws WorkException
{
if (transport != null)
{
checkTransport();
if (getLongRunningThreadPool() != null && WorkManagerUtil.isLongRunning(work))
{
transport.updateLongRunningFree(getLocalAddress(),
getLongRunningThreadPool().getNumberOfFreeThreads() - 1);
}
else
{
transport.updateShortRunningFree(getLocalAddress(),
getShortRunningThreadPool().getNumberOfFreeThreads() - 1);
}
WorkEventListener wel = new WorkEventListener(WorkManagerUtil.isLongRunning(work),
getShortRunningThreadPool(),
getLongRunningThreadPool(),
getLocalAddress(),
transport);
return super.startWork(work, WorkManager.INDEFINITE, null, wel);
}
else
{
return super.startWork(work);
}
}
/**
* {@inheritDoc}
*/
@Override
public void doWork(Work work) throws WorkException
{
if (policy == null || selector == null || transport == null ||
work == null || !(work instanceof DistributableWork) || !doWorkDistributionEnabled)
{
localDoWork(work);
}
else
{
doFirstChecks(work, WorkManager.INDEFINITE, null);
checkTransport();
DistributableWork dw = (DistributableWork)work;
boolean executed = false;
if (policy.shouldDistribute(this, dw))
{
Address dwmAddress = selector.selectDistributedWorkManager(getLocalAddress(), dw);
if (dwmAddress != null && !getLocalAddress().equals(dwmAddress))
{
transport.doWork(dwmAddress, dw);
executed = true;
}
}
if (!executed)
{
localDoWork(work);
}
}
}
@Override
public void doWork(Work work, long startTimeout, ExecutionContext executionContext, WorkListener workListener)
throws WorkException {
Assert.state(this.syncTaskExecutor != null, "No 'syncTaskExecutor' set");
executeWork(this.syncTaskExecutor, work, startTimeout, false, executionContext, workListener);
}
/**
* {@inheritDoc}
*/
@Override
public void scheduleWork(Work work) throws WorkException
{
if (policy == null || selector == null || transport == null ||
work == null || !(work instanceof DistributableWork) || !scheduleWorkDistributionEnabled)
{
localScheduleWork(work);
}
else
{
doFirstChecks(work, WorkManager.INDEFINITE, null);
checkTransport();
DistributableWork dw = (DistributableWork)work;
boolean executed = false;
if (policy.shouldDistribute(this, dw))
{
Address dwmAddress = selector.selectDistributedWorkManager(getLocalAddress(), dw);
if (dwmAddress != null && !getLocalAddress().equals(dwmAddress))
{
transport.scheduleWork(dwmAddress, dw);
executed = true;
}
}
if (!executed)
{
localScheduleWork(work);
}
}
}
/**
* Do first checks for work starting methods
* @param work to check
* @param startTimeout to check
* @param execContext to check
* @throws WorkException in case of check don't pass
*/
public void doFirstChecks(Work work, long startTimeout, ExecutionContext execContext) throws WorkException
{
if (isShutdown())
throw new WorkRejectedException(bundle.workmanagerShutdown());
if (work == null)
throw new WorkRejectedException(bundle.workIsNull());
if (startTimeout < 0)
throw new WorkRejectedException(bundle.startTimeoutIsNegative(startTimeout));
checkAndVerifyWork(work, execContext);
}
/**
* Get the executor
* @param work The work instance
* @return The executor
*/
private BlockingExecutor getExecutor(Work work)
{
BlockingExecutor executor = shortRunningExecutor;
if (longRunningExecutor != null && WorkManagerUtil.isLongRunning(work))
{
executor = longRunningExecutor;
}
fireHintsComplete(work);
return executor;
}
/**
* Fire complete for HintsContext
* @param work The work instance
*/
private void fireHintsComplete(Work work)
{
if (work != null && work instanceof WorkContextProvider)
{
WorkContextProvider wcProvider = (WorkContextProvider) work;
List<WorkContext> contexts = wcProvider.getWorkContexts();
if (contexts != null && contexts.size() > 0)
{
Iterator<WorkContext> it = contexts.iterator();
while (it.hasNext())
{
WorkContext wc = it.next();
if (wc instanceof HintsContext)
{
HintsContext hc = (HintsContext) wc;
if (hc instanceof WorkContextLifecycleListener)
{
WorkContextLifecycleListener listener = (WorkContextLifecycleListener)hc;
listener.contextSetupComplete();
}
}
}
}
}
}
/**
* Create a new WorkWrapper
*
* @param workManager the work manager
* @param si The security integration
* @param work the work
* @param executionContext the execution context
* @param workListener the WorkListener
* @param startedLatch The latch for when work has started
* @param completedLatch The latch for when work has completed
* @param startTime The start time
* @throws IllegalArgumentException for null work, execution context or a negative start timeout
*/
public WorkWrapper(WorkManagerImpl workManager,
SecurityIntegration si,
Work work,
ExecutionContext executionContext,
WorkListener workListener,
CountDownLatch startedLatch,
CountDownLatch completedLatch,
long startTime)
{
super();
if (workManager == null)
throw new IllegalArgumentException("Null work manager");
if (si == null)
throw new IllegalArgumentException("Null security integration");
if (work == null)
throw new IllegalArgumentException("Null work");
if (executionContext == null)
throw new IllegalArgumentException("Null execution context");
this.workManager = workManager;
this.securityIntegration = si;
this.work = work;
this.executionContext = executionContext;
this.workListener = workListener;
this.startedLatch = startedLatch;
this.completedLatch = completedLatch;
this.startTime = startTime;
this.workContexts = null;
}
/**
* {@inheritDoc}
*/
public void localDoWork(Work work) throws WorkException
{
if (transport != null)
{
checkTransport();
if (getLongRunningThreadPool() != null && WorkManagerUtil.isLongRunning(work))
{
transport.updateLongRunningFree(getLocalAddress(),
getLongRunningThreadPool().getNumberOfFreeThreads() - 1);
}
else
{
transport.updateShortRunningFree(getLocalAddress(),
getShortRunningThreadPool().getNumberOfFreeThreads() - 1);
}
WorkEventListener wel = new WorkEventListener(WorkManagerUtil.isLongRunning(work),
getShortRunningThreadPool(),
getLongRunningThreadPool(),
getLocalAddress(),
transport);
super.doWork(work, WorkManager.INDEFINITE, null, wel);
}
else
{
super.doWork(work);
}
}
@Override
public void doWork(Work work) throws WorkException {
obtainWorkManager().doWork(work);
}
@Override
public void doWork(Work work, long delay, ExecutionContext executionContext, WorkListener workListener)
throws WorkException {
obtainWorkManager().doWork(work, delay, executionContext, workListener);
}
/**
* {@inheritDoc}
*/
public void doWork(Work work, long startTimeout, ExecutionContext execContext, WorkListener workListener)
throws WorkException
{
mc.doWork(work, startTimeout, execContext, workListener);
}
@Override
public void scheduleWork(Work work) throws WorkException {
obtainWorkManager().scheduleWork(work);
}
/**
* {@inheritDoc}
*/
public void doWork(Work work) throws WorkException
{
mc.doWork(work);
}
@Override
public void scheduleWork(Work work) throws WorkException {
scheduleWork(work, WorkManager.INDEFINITE, null, null);
}