0

I'm trying to execute some task every X second but the task runs twise. I'm Using Spring 4.2.5 - the latest version.(I tryed it with 4.05 the same result)

@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());
        }

    }
}

}

here is my 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>

UPDATED

here is my application configuration 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/");
}

}

Why is it running TWICE each time?

Thanks in advance

-- Found Solution --

I have removed @ComponentScan({"ge.kapi.*"}) from AppConfig.java because this scan was also initiated from mvc-dispatcher-servlet.xml like this:

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

Now Job starts only ones.

Thank you all for your time helping me!!!

Irakli
  • 973
  • 3
  • 19
  • 45

2 Answers2

0

I suggest you check if the bean is being created twice. It can happen if you have 2 app contexts (root and dispatcher).

0

Check if you have declared bean in xml and by annotation too.

And if you have spring security configuration in your application then keep mvc-dispatcher.xml declaration in of DispatcherServlet and spring-security.xml in of context loader listener .

Otherwise if you keep both xml with DispatcherServlet in that case two object will be created and that is why your scheduler will get call twice.

Danh
  • 5,916
  • 7
  • 30
  • 45
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/14266024) – Nikhil Nov 12 '16 at 07:59
  • @indramurari I think he addressed the correct point to solve this question! – Danh Nov 13 '16 at 09:58