gpt4 book ai didi

java - 在 Web 应用程序中使用 @Scheduled 注释 (JSF+Spring)

转载 作者:行者123 更新时间:2023-12-01 05:26:54 24 4
gpt4 key购买 nike

我的 Web 应用程序正在轮询数据库中的更改并在 JSP 上显示这些更改。数据库已被旧系统更改,无法获取数据更改事件。

我的方法是轮询数据库并将数据与应用程序 bean 同步。如果数据发生变化,我使用推送机制来更新JSP页面。轮询数据库的线程是由应用程序 bean 生成的。

我已经在我的 Web 应用程序中使用 spring,所以我想使用 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
}
...
}

我的 Spring 和 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"/>

这个问题是这个问题的解决方法 Is there a best practice for showing database changes on a JSP

最佳答案

我的解决方案,使用 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();
}
}
}

计划执行的接口(interface)

public interface IScheduledComponent {
public void doWork();
}

让Data Bean实现接口(interface)并注册到SchedulerComponent

@Named("agentData")
@Scope("application")
public class AgentDataBean implements Serializable,IScheduledComponent {
...
public void doWork() {
// do database poll
}
...
}

现在我已经通过 Spring 进行了调度,并且在 AgentDataBean 中获得了 FacesContext,因此我可以执行 Icefaces 推送。

关于java - 在 Web 应用程序中使用 @Scheduled 注释 (JSF+Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9567853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com