Spring启动时的作业调度

时间:2017-05-23 15:15:38

标签: java spring spring-boot scheduler spring-scheduled

我的项目中有3个不同的模块,即core-services services sync-engine。每个模块都有自己的Application.javapublic static void main(String[] args)。我在sync-engine模块中有一个调度程序,它在指定时间调用google API。 Scheduler中的sync-engine类依赖于core-service模块。所以,我自动化了依赖并使用它。自动连接的依赖项在core-service模块中调用repositorty,但我得到空的结果集。但是,如果我将调度程序移动到core-service模块,我会得到理想的结果。如何在不将调度程序移动到不同模块的情况下获得所需结果。春天我很新,请帮忙

以下是sync-engine模块配置

package com.xxx.abc;
@SpringBootApplication( exclude = { EmbeddedDataSource.class } )
@EnableJpaRepositories
@EnableEncryptableProperties
@PropertySource( name = "encrypedProps", value ="classpath:secret.properties" )
@Import( { HikariDataSourceConfig.class } )
@EnableJms
@EnableScheduling
@EnableSpringDataWebSupport
@EnableAsync
public class KatSyncEngineApplication {

private static final Logger LOG = LoggerFactory.getLogger(KatSyncEngineApplication.class);

/**
 *
 * @param args
 */
public static void main( final String[] args )
{
    LOG.debug("Booting Spring Application ...... ");
    SpringApplication.run(KatSyncEngineApplication.class, args);
}

/**
 *
 * @return
 */
@Bean
public LocaleResolver localeResolver()
{
    final SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
    sessionLocaleResolver.setDefaultLocale(Locale.US);
    return sessionLocaleResolver;
}

/**
 *
 * @return
 */
@Bean
public ReloadableResourceBundleMessageSource messageSource()
{
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:bundles/messages", "classpath:bundles/errors",
            "classpath:bundles/tooltip");
    messageSource.setCacheSeconds(3600 * 24); // every day
    messageSource.setAlwaysUseMessageFormat(true);
    return messageSource;
}

/**
 *
 * @return
 */
@Bean
public MultipartConfigElement multipartConfigElement()
{
    final MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setMaxFileSize("50Mb");
    factory.setMaxRequestSize("50Mb");
    return factory.createMultipartConfig();
}
}

sync-engine模块中的调度程序是

package com.xxx.abc.scheduler;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.xxx.abc.app.auth.UserAccessDetailsToken;
import com.xxx.abc.beans.GoogleUserTokenBean;
import com.xxx.abc.data.katgooglecalendar.domain.AggregateKatGoogleCalendar;
import com.xxx.abc.katgooglecalendar.service.KatGoogleCalendarService;
import com.xxx.abc.exception.DataException;
import com.xxx.abc.cmutils.NullEmptyUtils;
@Component
public class CalendarSchedular {

private static final Logger logger = LoggerFactory.getLogger(CalendarSchedular.class);

/**
 *
 */
private static final int FIXED_RATE = 1200000;

@Autowired
private KatGoogleCalendarService katGoogleCalendarService;

@Scheduled( fixedRate = FIXED_RATE )
public void getCalendarData() throws DataException
{
    try
    {
        int pageNumber = 0;
        final int pageSize = 30;
        new PageRequest(pageNumber, pageSize);
        List<AggregateKatGoogleCalendar> googleCalendars = new ArrayList<AggregateKatGoogleCalendar>();
        do
        {
            System.out.println(getString());
            googleCalendars =katGoogleCalendarService.getAllUsersCalendarDetails();
            System.out.println("result set: " + googleCalendars.size());
            if( !NullEmptyUtils.isNullorEmpty(googleCalendars) )
            {
                for( final AggregateKatGoogleCalendar aggregateKatGoogleCalendar : googleCalendars )
                {
                   //Business logic
                }
                ++pageNumber;
                new PageRequest(pageNumber, pageSize);
            }
        } while( !NullEmptyUtils.isNullorEmpty(googleCalendars) && googleCalendars.size() >= pageSize );
    }
    catch( final DataException e )
    {
        logger.error("Error", e);
    }
    catch( final Exception e )
    {
        logger.error("Error", e);
    }
}
}

在,     @Autowired     私人KatGoogleCalendarService katGoogleCalendarService; KatGoogleCalendarService位于core-service模块

getAllUsersCalendarDetails()方法调用存储库。但我得到空的结果集。但是,如果我将相同的调度程序类添加到core-service模块,我将得到所需的结果。

0 个答案:

没有答案