Example usage for org.springframework.scheduling.concurrent ThreadPoolTaskScheduler setErrorHandler

List of usage examples for org.springframework.scheduling.concurrent ThreadPoolTaskScheduler setErrorHandler

Introduction

In this page you can find the example usage for org.springframework.scheduling.concurrent ThreadPoolTaskScheduler setErrorHandler.

Prototype

public void setErrorHandler(ErrorHandler errorHandler) 

Source Link

Document

Set a custom ErrorHandler strategy.

Usage

From source file:com.joshlong.esb.springintegration.modules.net.sftp.Main.java

static void run(SFTPSessionFactory sftpSessionFactory, String lp, String rp) throws Throwable {
    // local path
    File local = new File(lp); // obviously this is just for test. Do what you need to do in your own

    // we are testing, after all
    if (local.exists() && (local.list().length > 0)) {
        for (File f : local.listFiles()) {
            if (!f.delete()) {
                logger.debug("couldn't delete " + f.getAbsolutePath());
            }/*from   w ww  .  ja va2  s. c o  m*/
        }
    }

    Resource localDirectory = new FileSystemResource(local);

    // pool
    QueuedSFTPSessionPool queuedSFTPSessionPool = new QueuedSFTPSessionPool(sftpSessionFactory);
    queuedSFTPSessionPool.afterPropertiesSet();

    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(10);
    taskScheduler.setErrorHandler(new ErrorHandler() {
        public void handleError(Throwable t) {
            logger.debug("error! ", t);
        }
    });

    taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    taskScheduler.initialize();

    // synchronizer
    final SFTPInboundSynchronizer sftpInboundSynchronizer = new SFTPInboundSynchronizer();
    sftpInboundSynchronizer.setLocalDirectory(localDirectory);
    sftpInboundSynchronizer.setRemotePath(rp);
    sftpInboundSynchronizer.setAutoCreatePath(true);
    sftpInboundSynchronizer.setPool(queuedSFTPSessionPool);
    sftpInboundSynchronizer.setShouldDeleteDownloadedRemoteFiles(false);
    sftpInboundSynchronizer.setTaskScheduler(taskScheduler);
    sftpInboundSynchronizer.afterPropertiesSet();
    sftpInboundSynchronizer.start();

    /*
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(60 * 1000); // 1 minute
            
                    sftpInboundSynchronizer.stop();
            
                } catch (InterruptedException e) {
                    // don't care
                }
            }
        }).start();
    */
}

From source file:io.lavagna.config.PersistenceAndServiceConfig.java

@Bean(destroyMethod = "shutdown")
public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setErrorHandler(new ErrorHandler() {
        @Override/*from   w  ww . j  a  va  2 s  . c om*/
        public void handleError(Throwable t) {
            LogManager.getLogger().error("error while handling job", t);
        }
    });
    scheduler.initialize();
    return scheduler;
}

From source file:com.joshlong.esb.springintegration.modules.net.sftp.config.SFTPMessageSourceFactoryBean.java

@Override
protected SFTPMessageSource createInstance() throws Exception {
    try {//from   www .  j av a  2 s.c o m
        if ((localWorkingDirectory == null) || StringUtils.isEmpty(localWorkingDirectory)) {
            File tmp = SystemUtils.getJavaIoTmpDir();
            File sftpTmp = new File(tmp, "sftpInbound");
            this.localWorkingDirectory = "file://" + sftpTmp.getAbsolutePath();
        }
        assert !StringUtils
                .isEmpty(this.localWorkingDirectory) : "the local working directory mustn't be null!";

        // resource for local directory
        ResourceEditor editor = new ResourceEditor(this.resourceLoader);
        editor.setAsText(this.localWorkingDirectory);
        this.localDirectoryResource = (Resource) editor.getValue();

        fileReadingMessageSource = new FileReadingMessageSource();

        synchronizer = new SFTPInboundSynchronizer();

        if (null == taskScheduler) {
            Map<String, TaskScheduler> tss = null;

            if ((tss = applicationContext.getBeansOfType(TaskScheduler.class)).keySet().size() != 0) {
                taskScheduler = tss.get(tss.keySet().iterator().next());
            }
        }

        if (null == taskScheduler) {
            ThreadPoolTaskScheduler ts = new ThreadPoolTaskScheduler();
            ts.setPoolSize(10);
            ts.setErrorHandler(new ErrorHandler() {
                public void handleError(Throwable t) {
                    // todo make this forward a message onto the error channel (how does that work?)
                    logger.debug("error! ", t);
                }
            });

            ts.setWaitForTasksToCompleteOnShutdown(true);
            ts.initialize();
            this.taskScheduler = ts;
        }

        SFTPSessionFactory sessionFactory = SFTPSessionUtils.buildSftpSessionFactory(this.getHost(),
                this.getPassword(), this.getUsername(), this.getKeyFile(), this.getKeyFilePassword(),
                this.getPort());

        QueuedSFTPSessionPool pool = new QueuedSFTPSessionPool(15, sessionFactory);
        pool.afterPropertiesSet();
        synchronizer.setRemotePath(this.getRemoteDirectory());
        synchronizer.setPool(pool);
        synchronizer.setAutoCreatePath(this.isAutoCreateDirectories());
        synchronizer.setShouldDeleteDownloadedRemoteFiles(this.isAutoDeleteRemoteFilesOnSync());

        SFTPMessageSource sftpMessageSource = new SFTPMessageSource(fileReadingMessageSource, synchronizer);

        sftpMessageSource.setTaskScheduler(taskScheduler);

        if (null != this.trigger) {
            sftpMessageSource.setTrigger(trigger);
        }

        sftpMessageSource.setLocalDirectory(this.localDirectoryResource);
        sftpMessageSource.afterPropertiesSet();
        sftpMessageSource.start();

        return sftpMessageSource;
    } catch (Throwable thr) {
        logger.debug("error occurred when trying to configure SFTPmessageSource ", thr);
    }

    return null;
}

From source file:org.springframework.integration.test.util.TestUtils.java

public static TestApplicationContext createTestApplicationContext() {
    TestApplicationContext context = new TestApplicationContext();
    ErrorHandler errorHandler = new MessagePublishingErrorHandler(context);
    ThreadPoolTaskScheduler scheduler = createTaskScheduler(10);
    scheduler.setErrorHandler(errorHandler);
    registerBean("taskScheduler", scheduler, context);
    registerBean("integrationConversionService", new DefaultFormattingConversionService(), context);
    return context;
}