Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. @Component("restBean")
  2. @Service
  3. public class RestService {
  4. private static final String url1 = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=1324&camera=fhaz&api_key=DEMO_KEY";
  5.  
  6. private PhotoDao photoDao;
  7.  
  8. @Autowired
  9. public void setPhotoDao(PhotoDao photoDao) {
  10. this.photoDao = photoDao;
  11. }
  12.  
  13. @Scheduled(fixedRate = 5000)
  14. public void addPhoto() {
  15. System.out.println("TIME HAS COME");
  16. RestTemplate restTemplate = new RestTemplate();
  17. ResponseEntity<Response> response = restTemplate
  18. .getForEntity(url1,
  19. Response.class);
  20. Photo[] photos = response.getBody().getPhotos();
  21. this.photoDao.save(photos[0]);
  22. }
  23. }
  24. /// настройка бобов
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  28. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
  29. xsi:schemaLocation="http://www.springframework.org/schema/beans
  30. http://www.springframework.org/schema/beans/spring-beans.xsd
  31. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
  32.  
  33. <!-- This helps in mapping the logical view names to directly view files under a certain pre-configured directory -->
  34. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  35. <property name="prefix" value="/WEB-INF/pages/"/>
  36. <property name="suffix" value=".jsp"/>
  37. </bean>
  38.  
  39. <!-- Database Information -->
  40. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  41. destroy-method="close">
  42. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  43. <property name="url"
  44. value="jdbc:mysql://localhost:3306/DayDatabase"/>
  45. <property name="username" value="root"/>
  46. <property name="password" value="root"/>
  47. </bean>
  48.  
  49. <!-- Hibernate 4 SessionFactory Bean definition -->
  50. <bean id="hibernate4AnnotatedSessionFactory"
  51. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  52. <property name="dataSource" ref="dataSource"/>
  53. <property name="packagesToScan">
  54. <list>
  55. <value>net.yan.model</value>
  56. </list>
  57. </property>
  58. <property name="hibernateProperties">
  59. <props>
  60. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
  61. </prop>
  62. <prop key="hibernate.show_sql">true</prop>
  63. </props>
  64. </property>
  65. </bean>
  66.  
  67. <!-- It register the beans in context and scan the annotations inside beans and activate them -->
  68. <context:component-scan base-package="net.yan"/>
  69. <!-- This allow for dispatching requests to Controllers -->
  70. <mvc:annotation-driven/>
  71.  
  72. <mvc:default-servlet-handler/>
  73.  
  74. <!-- PhotoDao and PhotoService beans -->
  75. <bean id="photoDao" class="net.yan.dao.PhotoDaoImpl">
  76. <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
  77. </bean>
  78.  
  79. <bean id="photoService" class="net.yan.service.PhotoServiceImpl">
  80. <property name="photoDao" ref="photoDao"/>
  81. </bean>
  82.  
  83. <bean id="restService" class="net.yan.service.RestService">
  84. <property name="photoDao" ref="photoDao"/>
  85. </bean>
  86.  
  87.  
  88. <tx:annotation-driven transaction-manager="transactionManager"/>
  89.  
  90. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  91. <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
  92. </bean>
  93.  
  94. <task:scheduled-tasks scheduler="myScheduler">
  95. <task:scheduled ref="restBean" method="addPhoto" fixed-delay="5000"/>
  96. </task:scheduled-tasks>
  97.  
  98. <task:scheduler id="myScheduler"/>
  99.  
  100.  
  101. </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement