Advertisement
Guest User

Untitled

a guest
Aug 20th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. public class GetDataFromTheWarehouse implements ServletContextListener {
  2.  
  3. @Autowired
  4. ScheduledTask scheduledTask;
  5.  
  6. private ScheduledExecutorService scheduler = null;
  7.  
  8. public GetDataFromTheWarehouse() {
  9. }
  10.  
  11. public void contextDestroyed(ServletContextEvent arg0) {
  12. try {
  13. System.out.println("Scheduler Shutting down successfully " + new Date());
  14. scheduler.shutdown();
  15. } catch (Exception ex) {
  16. }
  17. }
  18.  
  19. public void contextInitialized(ServletContextEvent arg0) {
  20. if ((scheduler == null) || (!scheduler.isTerminated())) {
  21. scheduler = Executors.newSingleThreadScheduledExecutor();
  22. scheduler.scheduleAtFixedRate(scheduledTask, 0, 60*60, TimeUnit.SECONDS);
  23. }
  24. }
  25. }
  26.  
  27. @Component
  28. public class ScheduledTask extends TimerTask {
  29. @Autowired
  30. ProductService productService;
  31.  
  32. public void run() {
  33. try {
  34. parse();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. public void parse() throws IOException {
  41. ...
  42. productService.save(product);
  43. ...
  44. }
  45. }
  46. }
  47.  
  48. <?xml version="1.0" encoding="UTF-8"?>
  49. <beans xmlns="http://www.springframework.org/schema/beans"
  50. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  51. xmlns:tx="http://www.springframework.org/schema/tx"
  52. xmlns:mvc="http://www.springframework.org/schema/mvc"
  53. xmlns:context="http://www.springframework.org/schema/context"
  54. xsi:schemaLocation="http://www.springframework.org/schema/beans
  55. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  56. http://www.springframework.org/schema/tx
  57. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  58. http://www.springframework.org/schema/context
  59. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  60. http://www.springframework.org/schema/mvc
  61. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  62.  
  63. <!-- Enable autowire -->
  64. <context:component-scan base-package="com" />
  65. <context:annotation-config />
  66.  
  67. <bean id="usersUpdateTask" class="com.demo.task.ScheduledTask">
  68. </bean>
  69.  
  70. <mvc:annotation-driven />
  71.  
  72. <mvc:resources mapping="/resources/**" location="/resources/" />
  73.  
  74. <mvc:resources mapping="/views/**" location="/views/" />
  75. <mvc:resources mapping="/img/**" location="/img/" />
  76. <mvc:resources mapping="/fonts/**" location="/fonts/" />
  77. <mvc:resources mapping="/css/**" location="/css/" />
  78. <mvc:resources mapping="/js/**" location="/js/" />
  79.  
  80. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  81. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  82. <property name="url" value="jdbc:mysql://localhost:3306/demo" />
  83. <property name="username" value="root" />
  84. <property name="password" value="root" />
  85. </bean>
  86.  
  87. <!-- Session Factory Declaration -->
  88. <bean id="sessionFactory"
  89. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  90. <property name="dataSource" ref="dataSource" />
  91. <property name="packagesToScan" value="com.demo.model" />
  92. <property name="hibernateProperties">
  93. <props>
  94. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  95. <prop key="hibernate.show_sql">true</prop>
  96. <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
  97. <prop key="hibernate.default_schema">demo</prop>
  98. <prop key="format_sql">true</prop>
  99. <prop key="use_sql_comments">true</prop>
  100. <!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
  101. </props>
  102. </property>
  103. </bean>
  104.  
  105. <tx:annotation-driven transaction-manager="transactionManager" />
  106.  
  107. <bean id="transactionManager"
  108. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  109. <property name="sessionFactory" ref="sessionFactory" />
  110. </bean>
  111.  
  112. </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement