a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 152|回复: 2

[其他] JAVA技巧:Servlet上下文实现侦听器

[复制链接]
发表于 2012-8-4 12:28:23 | 显示全部楼层 |阅读模式
在项目中使用到按时使命,出来使用Spring自带的调剂之外,还可以使用Servlet上下文实现一、建树一个Listener 类,实现ServletContextListener  接口,实现接口中的体例: # n9 W' R1 Q" ]7 N- M6 L
  import javax.servlet.ServletContextEvent;4 m* Q( O+ X- ^9 E7 D! Q
  import javax.servlet.ServletContextListener;
4 S/ \4 ~: m" ^4 y  /**
# k" R! Y; }' F6 c. R, C( N  X  * 侦听器类 和Timer 连系起来按时执行使命  D* y9 }5 w: d; W) r  r
  */
- F/ m% Y3 V& T  public class ContextListener implements ServletContextListener {
. I+ f  G- s. Y  private java.util.Timer timer = null;
- e9 O- J" J& U. }( T) ~# r) }( Q- j$ ^  public void contextInitialized(ServletContextEvent arg0) {/ A) I" C  P  y5 d! }4 L
  timer = new java.util.Timer(true);// 建树一个新计时骥,指定其相关的线程作为守护轨范运行。8 J/ j, m2 c. I0 u3 ?5 r' }8 d
  //System.out.println("启动按时器");3 e+ i3 N3 Z' I
  //调剂器,EarlyWarningTask() 为自界说调剂使命
0 [, p' c9 D) @8 W. e  n! t% M  //初始化时就执行一次使命/ _: |% n* P: d4 N1 D3 f
  timer.schedule(new EarlyWarningTask(),10, 60*60*1000);//执行使命前延迟10毫秒,执行使命的距离60*60*1000毫秒. I/ D+ y6 `- l
  //System.out.println("调剂使命添加完成");
5 E7 K: Q6 |  [  }
  v/ _2 E  t* f, q6 B( ^  public void contextDestroyed(ServletContextEvent arg0) {
' T- L  ]% r6 w( y9 G  timer.cancel();  K8 X, ?2 N  e, V6 ~
  //System.out.println("按时使命销毁");+ x' m( r& @  s! y/ H( C5 }
  }& A( V$ T  W1 o& @
  }) d) r1 B+ j' H  B
二、获得Spring 的ApplicationContext设置装备摆设信息,以便在调剂使命中篡夺设置装备摆设文件中的相关营业体例 ( k* B3 b* b+ @$ |' M0 D
  import org.springframework.context.support.AbstractApplicationContext;
  d! o) f/ d0 k! E0 d3 R  import org.springframework.context.support.ClassPathXmlApplicationContext;( I' e! R5 q2 H  B5 F' q& w8 U
  public class AppContext {
2 T3 ^5 d( n. ]. J) {# d  private static AppContext instance;
& S) f. T& m8 q# v2 ]6 l+ a  private AbstractApplicationContext appContext;
  K% f' o# C- f/ `8 e& n  public synchronized static AppContext getInstance() {
+ w* l% v& v3 m  if (instance == null) {
) l0 g8 c+ t* Q( ^1 m# a% q5 P; v: E  instance = new AppContext();
6 s2 A" q' ]3 k( d' Q' D! d; h  }
- A& Z! w% z1 M& r  return instance;) r0 `) j/ ?3 c) ?1 G( g, J& g
- @9 j  n& Q- Q
  }
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:28:24 | 显示全部楼层

JAVA技巧:Servlet上下文实现侦听器

</p>  private AppContext() {2 X; O5 e2 p- }0 E
  this.appContext = new ClassPathXmlApplicationContext(; U/ K% a% j9 u( u, y
  "/applicationContext.xml");+ D, I# _$ {2 x! E" |  p/ y- D& _
  }& N$ [, ^/ d2 z; I1 B4 G, f+ M
  public AbstractApplicationContext getAppContext() {
# I: b8 u% C4 P" u. }  return appContext;# g8 T" D( O( _9 I+ @
  }
8 {& n' a4 q  @" W/ Z2 _  }
! \8 T3 s. m+ X; |( N7 ]* a3 e! @3 n三、自界说调剂使命类,担任TimerTask 类
% [* C, U7 l4 j  import java.util.TimerTask;3 l8 H3 T8 R5 K4 l, ~- s
  public class EarlyWarningTask extends TimerTask {
. v2 D$ A$ _) q8 g  private static final int C_SCHEDULE_HOUR = 23;//执行时刻23时5 ~4 O) |; a% J2 ]% A3 k  a" e7 T
  private static boolean isRunning = false;
) d4 c2 i9 T& {; M! S: G  //经由过程AppContext 类,获得设置装备摆设文件中相关营业体例1 l6 G2 o' C$ U  N
  //inStorageDetailImp' G( W# b, `# F* E# G/ L- `; E
  protected IInStorageDetailService getInStorageDetailService(){' e. Y* T, f" k8 }
  return (IInStorageDetailService)AppContext.getInstance().getAppContext()! t& X! n6 a+ _4 D" ?4 D( H0 r
  .getBean("inStorageDetailImp");+ |! I' B* q2 g1 Y
  }
- d. F+ B: |9 v% O9 W; S) i/ }  //    goodsWarningImp# {( Z7 o$ ~( d8 F+ C' D/ Z, m
  protected IGoodsWarningService getGoodsWarningService(){# `$ X0 H; p5 [
  return (IGoodsWarningService)AppContext.getInstance().getAppContext()
  S- o- _3 a3 n3 ?% u/ E  .getBean("goodsWarningImp");
, Z! @' M! U6 v- w8 K, |1 f  }
3 [4 T# y1 d7 A  M  //    lendOutAccountImp
1 S: h8 w9 f8 \# J6 q  protected ILendOutAccountService getLendOutAccountService(){  S& ]! e2 |, [- l
  return (ILendOutAccountService) AppContext.getInstance().getAppContext()
7 X  r5 I( V2 m3 R: _; Q- Q  .getBean("lendOutAccountImp");
' c$ v9 U! ^/ S6 J  ^$ Z# w( h) U3 s+ M3 M) i$ B
  }
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-8-4 12:28:25 | 显示全部楼层

JAVA技巧:Servlet上下文实现侦听器

</p>  //lendOutWarningImp
0 ^$ _  U5 b8 p9 f3 X0 D( _# M  protected ILendOutWarningService getLendOutWarningService(){9 X8 Z$ v& q4 q, G
  return (ILendOutWarningService)AppContext.getInstance().getAppContext()
- b$ @6 z% [, B3 w. C0 C4 u6 e( ^+ r5 V  .getBean("lendOutWarningImp");
" _6 b2 o+ S8 d3 L; J  }0 }" u$ M. C% c. X; ~
  //使命在run()体例中实现
8 c* u5 C3 m, `' ^0 V  @Override
1 \# z3 m* N2 f% g  public void run() {
" b% k9 y$ V# _7 ]! a& g2 \) ?  Calendar cal = Calendar.getInstance() ;
0 W! ]4 x7 q$ y  if(!isRunning){* U) I2 P1 c: w  @. s4 z" h
  if(C_SCHEDULE_HOUR==cal.get(Calendar.HOUR_OF_DAY)){//HOUR_OF_DAY 用于 24 小时制时钟2 f5 D  C" K6 W* }4 [
  isRunning = true;2 T) I2 h3 Q" ]( H
  System.out.println("执行按时使命");
/ w" H3 x# E5 U4 u% ]. G: [9 ^  //使命代码
! l) m5 w% z0 O; a- J2 L  //…………………………/ m! U9 r3 x% E; r1 T
  isRunning = false;8 A$ `% ?8 t+ H* y
  System.out.println("按时使命竣事");
' A  ~9 H/ K$ N  }else {
: y- N$ b9 G- M  u7 h' Y- U& b6 C/ @  System.out.println("未到按时使命时刻");( ?: S" Z5 P6 v* o; F
  }
9 _. N2 n0 U3 n7 x5 z& B  }else{4 F- W9 V- n* W
  System.out.println("上一个按时使命未竣事");) K' b/ Z9 N% \$ |  P/ ?
  }
! i4 T0 b' p. H) p2 l- _* {  }( c& t8 l1 p6 B% D! p' k, S0 a
四、web.xml设置装备摆设监听
$ p' s2 @0 ]& y0 F  0 U' d* n9 \" N5 ]+ H+ ]: w
  com.cl.gdb.util.ContextListener$ c( x) [6 Z) H8 r& z
  
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-21 21:44 , Processed in 0.249678 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表