Spring 4 @Scheduled Task正在执行两次任务

时间:2016-03-24 07:03:44

标签: java spring schedule

我正在尝试每X秒执行一些任务,但任务运行两次。我正在使用Spring 4.2.5 - 最新版本。(我用4.05尝试了相同的结果)

@Service
@Transactional
@EnableScheduling
public class PaymentServices {


@Autowired
private MMTransactionDAO mmTransactionDAO;



@Scheduled(fixedDelay=230000)
public void getListOfPenddingTransactions() throws MambuApiException {
    System.out.println("JOB Started");
    List<MMPayTransaction> listOfPenddingTransaction = mmTransactionDAO.getListOfPenddingTransaction();

    for(MMPayTransaction transaction : listOfPenddingTransaction){
        if (transaction.getErrorcode().equals("-6")){
            cancelTransactionInMambu(transaction.getMambuClientID(),transaction.getPaymentAmount(),transaction.getFeeAmount());
            transaction.setFinalStatus(TansactionStatus.FAILED);
        }else if(transaction.getErrorcode().equals("-21")){
            cancelTransactionInMambu(transaction.getMambuClientID(),transaction.getPaymentAmount(),transaction.getFeeAmount());
            transaction.setFinalStatus(TansactionStatus.FAILED);
        }else if(transaction.getErrorcode().equals("-18")){
            cancelTransactionInMambu(transaction.getMambuClientID(),transaction.getPaymentAmount(),transaction.getFeeAmount());
            transaction.setFinalStatus(TansactionStatus.FAILED);
        }else if (transaction.getErrorcode().equals("-37")){
            cancelTransactionInMambu(transaction.getMambuClientID(),transaction.getPaymentAmount(),transaction.getFeeAmount());
            transaction.setFinalStatus(TansactionStatus.FAILED);
        }
        else{
            check(transaction.getOperationID());
        }

    }
}

}

这是我的web.xml

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

已更新

这是我的应用程序配置calss:

@EnableWebMvc
@Configuration
@ComponentScan({"ge.kapi.*"})
@EnableTransactionManagement
public class AppConfig extends WebMvcConfigurerAdapter {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(Boolean.TRUE);
    vendorAdapter.setShowSql(Boolean.TRUE);

    factory.setDataSource(dataSource());
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("ge.kapi");

    Properties jpaProperties = getHibernateProperties();
    factory.setJpaProperties(jpaProperties);

    factory.afterPropertiesSet();
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    return factory;
}

private Properties getHibernateProperties() {
    Properties prop = new Properties();
    prop.put("hibernate.show_sql", "true");
    prop.put("hibernate.dialect","ge.kapi.config.SQLServerUnicodeDialect");
    return prop;
}


@Bean(name = "dataSource")
public BasicDataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    ds.setUrl("jdbc:sqlserver://host;useUnicode=true;characterEncoding=UTF-8;DatabaseName=Base");
    ds.setUsername("user");
    ds.setPassword("pass");
    return ds;
}




@Bean
public StringHttpMessageConverter stringHttpMessageConverter() {
    return new StringHttpMessageConverter(Charset.forName("UTF-8"));
}

@Bean
public PlatformTransactionManager transactionManager()
{
    EntityManagerFactory factory = entityManagerFactory().getObject();
    return new JpaTransactionManager(factory);
}



@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver
            = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setContentType("text/html; charset=UTF-8");
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

}

为什么每次都运行TWICE?

提前致谢

- 找到解决方案 -

我已从AppConfig.java中删除了@ComponentScan({“ge.kapi。*”}),因为此扫描也是从mvc-dispatcher-servlet.xml启动的,如下所示:

 <context:component-scan base-package="ge.kapi"/>

现在约伯只开始。

谢谢大家的时间帮助我!!!

2 个答案:

答案 0 :(得分:0)

我建议你检查bean是否被创建了两次。如果你有2个应用程序上下文(root和dispatcher),就会发生这种情况。

答案 1 :(得分:0)

检查你是否在xml和注释中声明了bean。

如果你的应用程序中有spring安全配置,那么在上下文加载器监听器的DispatcherServletspring-security.xml中保留mvc-dispatcher.xml声明。

否则,如果在这种情况下同时保留xml和DispatcherServlet,则会创建两个对象,这就是调度程序将调用两次的原因。