My Web application is polling a Database for changes and Showing those changes on a JSP. The Database is changed by a legacy System and no possibility to get an event on Data change.
My approach is to poll the Database and synchronizing the data with an Application bean. If the data has changed, I use a push mechanism to update the JSP page. The Thread that polls the database is spawned by the Application bean.
I’m already using spring in my Web application so I want to use the spring @Scheduled annotation because I don’t want to spawn a Thread and let Spring handle the periodical execution. But the the Method is not execute Periodically.
I’m using Tomcat and MS Sql Server.
My Application bean looks like this
@Named("agentData")
@Scope("application")
public class AgentDataBean implements Serializable {
...
@Scheduled(fixedRate=5000)
public void loadData() {
// do database poll
}
...
}
My Spring and Web Xml look like this
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JavaServerFaces</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>#{login.theme}</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/config/database.properties" />
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource" p:driverClassName="${jdbc.driverClassName}"
p:password="${jdbc.password}" p:url="${jdbc.url}" p:username="${jdbc.username}" />
<!-- Activates annotaion-based bean configuration -->
<context:annotation-config />
<!-- needed is for @Configurable -->
<context:component-scan base-package="de.cpls.alo.dashboard" />
<tx:annotation-driven />
<!-- Activates @Scheduled and @Async annotations fpr scheduling -->
<task:annotation-driven executor="executorWithPoolSizeRange" scheduler="taskScheduler"/>
<task:executor id="executorWithPoolSizeRange"
pool-size="5-25"
queue-capacity="100"/>
<!-- Defines a ThreadPoolTaskScheduler instance with configurable pool size.
The id becomes the default thread name prefix. -->
<task:scheduler id="taskScheduler" pool-size="1"/>
This question is a solution approach to this Question
Is there a best practice for showing database changes on a JSP
My Solution, using a SchedulerComponent
An Interface for Scheduled Execution
An let the Data Bean Implement the Interface and Register to the SchedulerComponent
Now i got Scheduled with Spring and i got FacesContext in AgentDataBean so i can Execute a Icefaces Push.