Search in sources :

Example 1 with TaskRejectedException

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);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) WorkRejectedException(commonj.work.WorkRejectedException) WorkException(commonj.work.WorkException) Work(commonj.work.Work) SchedulingException(org.springframework.scheduling.SchedulingException)

Example 2 with TaskRejectedException

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);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 3 with TaskRejectedException

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);
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ListenableFutureTask(org.springframework.util.concurrent.ListenableFutureTask) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 4 with TaskRejectedException

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();
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 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 {
                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);
            }
        }
    }
}
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)

Aggregations

TaskRejectedException (org.springframework.core.task.TaskRejectedException)19 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)12 ListenableFutureTask (org.springframework.util.concurrent.ListenableFutureTask)8 ExecutorService (java.util.concurrent.ExecutorService)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)5 FutureTask (java.util.concurrent.FutureTask)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 WorkException (javax.resource.spi.work.WorkException)2 WorkRejectedException (javax.resource.spi.work.WorkRejectedException)2 Test (org.junit.Test)2 TaskTimeoutException (org.springframework.core.task.TaskTimeoutException)2 SchedulingException (org.springframework.scheduling.SchedulingException)2 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)2 OpenApiException (com.z.gateway.common.exception.OpenApiException)1 OpenApiHttpSessionBean (com.z.gateway.protocol.OpenApiHttpSessionBean)1 Work (commonj.work.Work)1 WorkException (commonj.work.WorkException)1 WorkRejectedException (commonj.work.WorkRejectedException)1 UnknownMessageTypeException (cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException)1