1:配置定时任务
@SpringBootApplication
@EnableDiscoveryClient
@Order
@ImportResource(locations = {"classpath:conf/spring/spring-res.xml","classpath:conf/spring/spring-ds.xml","classpath:conf/spring/spring-scheduler.xml"})
public class PassengerFlowCountingApplication extends SpringBootServletInitializer {
//log
private final static Logger logger = LoggerFactory.getLogger(PassengerFlowCountingApplication.class);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//设置应用服务器端口
ApplicationServerHelper.setSpringBootAppServerPort();
return builder.sources(PassengerFlowCountingApplication.class);
}
}
spring-scheduler.xml
<!-- 线程执行器配置,用于任务注册 -->
<bean id="executor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="queueCapacity" value="500" />
</bean>
<!-- 启动触发器的配置开始 -->
<bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="sessionsJobTrigger" />
</list>
</property>
</bean>
<bean id="sessionsJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="sessionsJobDetail" />
</property>
<property name="cronExpression">
<!--每30分钟执行一次调度任务 -->
<value>0 */30 * * * ?</value>
</property>
</bean>
<bean id="sessionsJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="sessionsJob" />
</property>
<property name="targetMethod">
<value>removeOvertimeSessionInfo</value>
</property>
</bean>
<!-- 工作的bean -->
<bean id="sessionsJob" class="com.passenger.flow.counting.corn.job.session.SystemSessionsScheduler" />
工作类:
public class SystemSessionsScheduler {
//log
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 功能描述: 本地定时任务 定时清理过时的用户session 信息<br>
* 〈功能详细描述〉
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public void removeOvertimeSessionInfo() {
long start = System.currentTimeMillis();
logger.info("start excute task : removeOvertimeSessionInfo() ");
try {
//临时存放过时的session信息
List<String> temp = new ArrayList<String>();
for (Map.Entry<String, Object> element : StaticContainer.getSystemSessions().entrySet()) {
if (RedisUtils.getInstance().get(AuthUtil.SESS_PREFIX + element.getKey()) == null) {
temp.add(element.getKey());
}
}
for (String key : temp) {
StaticContainer.getSystemSessions().remove(key);
}
temp.clear();
} catch (Exception e) {
logger.error("task removeOvertimeSessionInfo() error:", ExceptionUtils.getStackTrace(e));
}
logger.info("end excute removeOvertimeSessionInfo use [" + (System.currentTimeMillis() - start) + "]");
}
}