use of org.springframework.core.task.TaskRejectedException in project spring-framework by spring-projects.
the class WorkManagerTaskExecutor method execute.
//-------------------------------------------------------------------------
// Implementation of the Spring SchedulingTaskExecutor interface
//-------------------------------------------------------------------------
@Override
public void execute(Runnable task) {
Assert.state(this.workManager != null, "No WorkManager specified");
Work work = new DelegatingWork(this.taskDecorator != null ? this.taskDecorator.decorate(task) : task);
try {
if (this.workListener != null) {
this.workManager.schedule(work, this.workListener);
} else {
this.workManager.schedule(work);
}
} catch (WorkRejectedException ex) {
throw new TaskRejectedException("CommonJ WorkManager did not accept task: " + task, ex);
} catch (WorkException ex) {
throw new SchedulingException("Could not schedule task on CommonJ WorkManager", ex);
}
}
use of org.springframework.core.task.TaskRejectedException in project spring-framework by spring-projects.
the class ThreadPoolTaskScheduler method submitListenable.
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
ExecutorService executor = getScheduledExecutor();
try {
ListenableFutureTask<T> future = new ListenableFutureTask<>(task);
executor.execute(errorHandlingTask(future, false));
return future;
} catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
}
}
use of org.springframework.core.task.TaskRejectedException in project spring-framework by spring-projects.
the class ThreadPoolTaskScheduler method submitListenable.
@Override
public ListenableFuture<?> submitListenable(Runnable task) {
ExecutorService executor = getScheduledExecutor();
try {
ListenableFutureTask<Object> future = new ListenableFutureTask<>(task, null);
executor.execute(errorHandlingTask(future, false));
return future;
} catch (RejectedExecutionException ex) {
throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
}
}
use of org.springframework.core.task.TaskRejectedException in project spring-integration by spring-projects.
the class CallerBlocksPolicyTests method test1.
@Test
public void test1() throws Exception {
final ThreadPoolTaskExecutor te = new ThreadPoolTaskExecutor();
te.setCorePoolSize(2);
te.setMaxPoolSize(2);
te.setQueueCapacity(1);
te.setRejectedExecutionHandler(new CallerBlocksPolicy(10000));
te.initialize();
final AtomicReference<Throwable> e = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(3);
te.execute(() -> {
try {
Runnable foo = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException();
}
latch.countDown();
}
};
te.execute(foo);
// this one will be queued
te.execute(foo);
// this one will be blocked and successful later
te.execute(foo);
} catch (TaskRejectedException tre) {
e.set(tre.getCause());
}
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertNull(e.get());
te.destroy();
}
use of org.springframework.core.task.TaskRejectedException in project perun by CESNET.
the class MessageReceiver method run.
@Override
public void run() {
while (running) {
if (!queueAcquired) {
try {
log.debug("Creating new JMS queue " + queueName);
// Step 1. Directly instantiate the JMS Queue object.
queue = HornetQJMSClient.createQueue(queueName);
// Step 9. Create a JMS Message Consumer
log.debug("Creating JMS consumer");
messageConsumer = session.createConsumer(queue);
queueAcquired = true;
log.debug("Ready to receive messages.");
// messageConsumer.receive(timeout) is a blocking operation!
waitTime = 0;
} catch (InvalidDestinationException e) {
queueAcquired = false;
waitTime = setWaitTime(waitTime);
log.error("Queue doesn't exist yet. We gonna wait a bit ({} s) and try it again.", (waitTime / 1000), e);
// wait for a time mentioned in the error message before try it again
try {
Thread.sleep(waitTime);
} catch (InterruptedException interrupted) {
log.error(interrupted.toString(), interrupted);
}
} catch (JMSException e) {
queueAcquired = false;
waitTime = setWaitTime(waitTime);
log.error("Something went wrong with JMS. We are gonna wait a bit ({} s) and try it again...", (waitTime / 1000), e);
} catch (Exception e) {
queueAcquired = false;
waitTime = setWaitTime(waitTime);
log.error("Can not continue. We gonna wait a bit ({} s) and try it again...", (waitTime / 1000), e);
}
} else {
// Try to send queued messages first
while (!inputMessages.isEmpty()) {
TextMessage message = inputMessages.remove();
try {
messageProducer.send(message, DeliveryMode.PERSISTENT, message.getIntProperty("priority"), 0);
log.trace("Message {} for dispatcher sent.\n", message.getText());
} catch (JMSException e) {
queueAcquired = false;
log.error("Something went wrong with JMS. We are gonna restart and try it again...", e);
// goes back to reinitialize the connection
return;
}
}
// Step 11. Receive the message
TextMessage messageReceived = null;
try {
messageReceived = (TextMessage) messageConsumer.receive(timeout);
if (messageReceived != null) {
final String message = messageReceived.getText();
String messageType = message.split("\\|", 2)[0].trim();
log.debug("RECEIVED MESSAGE:{}, Type:{}", message, messageType);
if (messageType.equalsIgnoreCase("task")) {
try {
taskExecutorMessageProcess.execute(new Runnable() {
@Override
public void run() {
// TODO: Remove in future
log.trace("I am going to call eventProcessor.receiveEvent(\"{}\") " + "in thread: {}", message, Thread.currentThread().getName());
eventProcessor.receiveEvent(message);
}
});
} catch (TaskRejectedException ex) {
log.error("Task was rejected. Message {}", message);
throw ex;
}
} else if (messageType.equalsIgnoreCase("command")) {
// TODO: There is no need to put commandProcessor to
// a separate thread at the moment, however it is
// very likely to be so in a future.
commandProcessor.receiveCommand(message);
} else {
throw new UnknownMessageTypeException("UNKNOWN TYPE[" + messageType + "]");
}
}
waitTime = 0;
} catch (InvalidDestinationException e) {
queueAcquired = false;
waitTime = setWaitTime(waitTime);
log.error("Queue doesn't exist or the connection is broken. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
} catch (JMSException e) {
queueAcquired = false;
waitTime = setWaitTime(waitTime);
log.error("Something went wrong with JMS. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
} catch (Exception e) {
queueAcquired = false;
waitTime = setWaitTime(waitTime);
log.error("Can not continue. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
}
}
if (waitTime > 0) {
if (waitTime > TOO_LONG) {
// gonna be back after trying to reinitialize the connection
return;
}
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
log.error(e.toString(), e);
}
}
}
}
Aggregations