Search in sources :

Example 6 with TaskRejectedException

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

the class TaskExecutorAdapter method submit.

@Override
public <T> Future<T> submit(Callable<T> task) {
    try {
        if (this.taskDecorator == null && this.concurrentExecutor instanceof ExecutorService) {
            return ((ExecutorService) this.concurrentExecutor).submit(task);
        } else {
            FutureTask<T> future = new FutureTask<>(task);
            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)

Example 7 with TaskRejectedException

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 {
                // Step 1. Directly instantiate the JMS Queue object.
                queue = HornetQJMSClient.createQueue(queueName);
                // Step 9. Create a JMS Message Consumer
                messageConsumer = session.createConsumer(queue);
                queueAcquired = true;
                // messageConsumer.receive(timeout) is a blocking operation!
                waitTime = 0;
            } catch (InvalidDestinationException e) {
                queueAcquired = false;
                waitTime = waitTime + 5000;
                log.error("Queue doesn't exist yet. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
            } catch (JMSException e) {
                queueAcquired = false;
                waitTime = waitTime + 5000;
                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 = waitTime + 5000;
                log.error("Can not continue. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
            }
        } else {
            // 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:" + message + ", Type:" + messageType);
                    if (messageType.equalsIgnoreCase("task")) {
                        try {
                            taskExecutorMessageProcess.execute(new Runnable() {

                                @Override
                                public void run() {
                                    // TODO: Remove in future
                                    log.info("I am going to call eventProcessor.receiveEvent(\"" + message + "\") in thread:" + 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 + "]");
                    }
                }
            } catch (InvalidDestinationException e) {
                queueAcquired = false;
                waitTime = waitTime + 5000;
                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 = waitTime + 5000;
                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 = waitTime + 5000;
                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);
            }
        }
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) InvalidDestinationException(javax.jms.InvalidDestinationException) JMSException(javax.jms.JMSException) TaskRejectedException(org.springframework.core.task.TaskRejectedException) JMSException(javax.jms.JMSException) InvalidDestinationException(javax.jms.InvalidDestinationException) UnknownMessageTypeException(cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException) TextMessage(javax.jms.TextMessage) UnknownMessageTypeException(cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException)

Example 8 with TaskRejectedException

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

the class ThreadPoolTaskExecutor method submitListenable.

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

Example 9 with TaskRejectedException

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

the class ThreadPoolTaskScheduler method schedule.

@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
    ScheduledExecutorService executor = getScheduledExecutor();
    long initialDelay = startTime.getTime() - System.currentTimeMillis();
    try {
        return executor.schedule(errorHandlingTask(task, false), initialDelay, 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 10 with TaskRejectedException

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

the class ThreadPoolTaskScheduler method scheduleAtFixedRate.

@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
    ScheduledExecutorService executor = getScheduledExecutor();
    long initialDelay = startTime.getTime() - System.currentTimeMillis();
    try {
        return executor.scheduleAtFixedRate(errorHandlingTask(task, true), initialDelay, period, 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)

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