定时器,是一对一直播平台开发常见的功能之一,一些数据清理就可以通过定时器完成,那么如何实现定时器功能呢?
1、实现代码
/**
* 清理会议室历史记录
* @author Ken
*/
@Repository
public class ClearRoomRunHistoryTask {
private static final Logger logger = Logger.getLogger(ClearRoomRunHistoryTask.class);
@Autowired
private IRoomRuninfoHistoryService roomRuninfoHistoryService;
/**
* 每天凌晨1:00清理createtime超过七天的会议室历史数据。
*/
public void deleteRoomRunHistoryInfo() {
try {
int count = 0;
//获取创建时间超过七天的会议室历史信息
List<RoomRuninfoHistory> expiredMeetingHistoryList = roomRuninfoHistoryService.findExpiredMeetingHistory();
count = roomRuninfoHistoryService.batchDeleteByRoomID(expiredMeetingHistoryList);
logger.info("deleteRoomRunHistoryInfoTask success.deleteCount=" + count);
} catch (Exception ex) {
logger.error("deleteRoomRunHistoryInfoTask error:", ex);
}
}
}
spring-quartz.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
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.2.xsd">
<!-- 要调用的工作类 -->
<bean id="clearRoomRunTask" class="com.hst.ces.meetingresource.quartz.ClearRoomRunTask"></bean>
<bean id="clearRoomRunHistoryTask" class="com.hst.ces.meetingresource.quartz.ClearRoomRunHistoryTask"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="deleteExpireMeetingInfoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="clearRoomRunTask" />
<!-- 调用类中的方法 -->
<property name="targetMethod" value="deleteExpireMeetingInfo" />
</bean>
<bean id="deleteInvalidMeetingInfoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="clearRoomRunTask" />
<property name="targetMethod" value="deleteInvalidMeetingInfo" />
</bean>
<bean id="deleteRoomRunHistoryInfoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="clearRoomRunHistoryTask" />
<property name="targetMethod" value="deleteRoomRunHistoryInfo" />
</bean>
<!-- 定义触发时间 -->
<bean id="deleteExpireMeetingInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="deleteExpireMeetingInfoJob" />
<!-- cron表达式,定时器设定每隔3分钟执行定时任务 -->
<property name="cronExpression" value="0 0/3 * * * ?" />
</bean>
<bean id="deleteInvalidMeetingInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="deleteInvalidMeetingInfoJob" />
<!-- cron表达式,定时器设定每天凌晨1点执行定时任务 -->
<property name="cronExpression" value="0 0 1 * * ?" />
</bean>
<bean id="deleteRoomRunHistoryInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="deleteRoomRunHistoryInfoJob" />
<!-- cron表达式,定时器设定每天凌晨1点执行定时任务 -->
<property name="cronExpression" value="0 0 1 * * ?" />
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz1" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="deleteExpireMeetingInfoTrigger" />
<ref bean="deleteInvalidMeetingInfoTrigger" />
<ref bean="deleteRoomRunHistoryInfoTrigger" />
</list>
</property>
</bean>
</beans>
最后一步,还要在web.xml里面扫描配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>meeting-resource-server</display-name>
<!-- token验证过滤器,需要用户登录 -->
<!-- <filter>-->
<!-- <filter-name>authorization-server</filter-name>-->
<!-- <filter-class>com.hst.ces.meetingresource.filter.AuthorizationServerFilter</filter-class>-->
<!-- <async-supported>true</async-supported>-->
<!-- </filter>-->
<!-- <filter-mapping>-->
<!-- <filter-name>authorization-server</filter-name>-->
<!-- <url-pattern>/security/*</url-pattern>-->
<!-- </filter-mapping>-->
<!-- <filter-mapping>-->
<!-- <filter-name>authorization-server</filter-name>-->
<!-- <url-pattern>/api/*</url-pattern>-->
<!-- </filter-mapping>-->
<!-- client校验过滤器,不需要用户登录 -->
<filter>
<filter-name>open-authorization</filter-name>
<filter-class>com.hst.ces.meetingresource.filter.OpenAuthorizationFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>open-authorization</filter-name>
<url-pattern>/open/*</url-pattern>
</filter-mapping>
<!-- spring监听器 -->
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- SpringMVC开始 ========================= start -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:conf/spring/applicationContext.xml</param-value>
</context-param>
<!-- spring mvc servlet -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:conf/spring/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- SpringMVC结束 =========================end -->
<!--字符编码过滤-->
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- http put method 过滤器 -->
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
以上,只是一对一直播平台开发实现定时器的其中一种方式,感兴趣的可以了解一下其他实现方式。
声明:本文由云豹科技转发自洋人德德博客,如有侵权请联系作者删除