Spring的事件传播

ApplicationEvent
public abstract class ApplicationEvent extends EventObject

ApplicationEventPublisher
public interface ApplicationEventPublisher


void publishEvent(ApplicationEvent event)
[quote] Notify all listeners registered with this application of an application event.
[/quote]
[quote]Interface that encapsulates event publication functionality. Serves as super-interface for ApplicationContext.[/quote]
ApplicationListener
public interface ApplicationListener extends EventListener

void onApplicationEvent(ApplicationEvent event) 
[quote] Handle an application event.[/quote]
[quote]Interface to be implemented by application event listeners. Based on the standard java.util.EventListener interface for the Observer design pattern[/quote]

下面是一个结合comet的例子:
public class DwrService implements ApplicationContextAware {
private ApplicationContext ctx;

public void setApplicationContext(ApplicationContext ctx) {
this.ctx = ctx;
}

public void perform() {
int i=0;
while(i<1000){
i++;
PerformInfo info = new PerformInfo();
info.setId(i);
info.setMsg("发送" + i + "信息");
info.setTime(new Date());
InfoEvent evt = new InfoEvent(info);
ctx.publishEvent(evt);
}
}

}

必须实现ApplicationContextAware接口,这里最关键就是applicationContext的publishEvent方法,它起到发布事件,通知Listener的目的。

public class InfoEvent extends ApplicationEvent {
public InfoEvent(Object source) {
super(source);
}
}


public class NotifyClient implements ApplicationListener, ServletContextAware {
private ServletContext servletContext = null;

public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}

public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof InfoEvent) {
PerformInfo info = (PerformInfo) event.getSource();
System.out.println(info.getMsg());
// Collection<ScriptSession> sessions=ctx.getAllScriptSessions();
ServerContext ctx = ServerContextFactory.get(servletContext);
Collection<ScriptSession> sessions = ctx.getScriptSessionsByPage("/comet/dwrShow/comet.jsp");
for (ScriptSession session : sessions) {
ScriptBuffer script = new ScriptBuffer();
String s = null;
String s2 = null;
try {
s = java.net.URLEncoder.encode(info.getMsg(), "UTF-8");
s2 = java.net.URLEncoder.encode("通知结束", "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (info.getId() < 1000) {
script.appendScript("putInfo('").appendScript(
info.getId() + ":" + s).appendScript("'); ");
} else {
script.appendScript("alert(decodeURI('").appendScript(s2)
.appendScript("')); ");
}

System.out.println(script.toString());
session.addScript(script);
}
}
}
}



事件监听类实现了ApplicationListener接口,必须实现onApplicationEvent方法,在这个方法中,我们可以根据业务要求,根据
applicationContext.publishEvent(event)中发布的不同事件,进行相应的处理。

[quote]ApplicationContext会自动在当前所有的Bean中寻找ApplicationListener接口的实现,并将其作为事件接收的对象。当

applicationContext.publishEvent(event)方法调用时,所有的ApplicationListener接口实现都会被激发,在每个ApplicationListener可以根据事件的类型判断是自己需要处理的事件。
[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值