2012-03-05 5 views
1

Webアプリケーションがデータベースをポーリングして変更を確認し、その変更をJSPで表示しています。データベースはレガシーシステムによって変更され、データ変更時にイベントを取得する可能性はありません。Webapplikation(JSF + Spring)で@Scheduledアノテーションを使用する

私のアプローチは、データベースをポーリングし、データをアプリケーションBeanと同期させることです。データが変更された場合は、プッシュメカニズムを使用してJSPページを更新します。データベースをポーリングするスレッドは、アプリケーションBeanによって生成されます。

私は既にSpringをWebアプリケーションで使用していますので、Spring @ Scheduledアノテーションを使用したいのです。なぜなら、スレッドを生成し、Springに定期的な実行を処理させたくないからです。しかし、このメソッドは定期的に実行されません。

私はTomcatとMS Sql Serverを使用しています。

私のアプリケーションBeanがこの

@Named("agentData") 
@Scope("application") 
public class AgentDataBean implements Serializable { 
... 
@Scheduled(fixedRate=5000) 
public void loadData() { 
// do database poll 
} 
... 
} 

のように見える私の春とWeb XMLはこの

<?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"/> 

のように見えるこの質問は、ソリューションのアプローチですスケジュール実行のためにSchedulerComponent

@Named("scheduler") 
@Component 
public class SchedulerComponent { 

private List<IScheduledComponent> components; 

public SchedulerComponent() { 
    this.components = new ArrayList<IScheduledComponent>(); 
} 


public void register(IScheduledComponent component){ 
    this.components.add(component); 
} 


@Scheduled(fixedRate = 5000) 
public void doAllWork() { 
    try { 
     for(IScheduledComponent component : this.components){ 
      component.doWork(); 
     } 

     System.out.println("SchedulerComponent.doAllWork()"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

アン・インタフェースを使用して、この質問 Is there a best practice for showing database changes on a JSP

+0

「それは機能していません」とはどういう意味ですか? – Ralph

+0

メソッドは定期的に実行されません。 – outofBounds

+0

@Componentアノテーションをクラスに追加すると機能しますか? – Ralph

答えて

0

私の解決策に

public interface IScheduledComponent { 
    public void doWork(); 
} 

アンは、データBeanのインタフェースを実装し、SchedulerComponent

に登録してみましょう
@Named("agentData") 
@Scope("application") 
public class AgentDataBean implements Serializable,IScheduledComponent { 
... 
public void doWork() { 
    // do database poll 
} 
... 
} 

N私はSpringでScheduledを取得し、AgentDataBeanでFacesContextを取得してIcefaces Pushを実行できます。

関連する問題