ApplicationEvent
public abstract class ApplicationEvent extends EventObject
ApplicationEventPublisher
public interface ApplicationEventPublisher
void publishEvent(ApplicationEvent event)
引用
Notify all listeners registered with this application of an application event.
引用
Interface that encapsulates event publication functionality. Serves as super-interface for ApplicationContext.
ApplicationListener
public interface ApplicationListener extends EventListener
void onApplicationEvent(ApplicationEvent event)
引用
Handle an application event.
引用
Interface to be implemented by application event listeners. Based on the standard java.util.EventListener interface for the Observer design pattern
下面是一个结合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)中发布的不同事件,进行相应的处理。
引用
ApplicationContext会自动在当前所有的Bean中寻找ApplicationListener接口的实现,并将其作为事件接收的对象。当
applicationContext.publishEvent(event)方法调用时,所有的ApplicationListener接口实现都会被激发,在每个ApplicationListener可以根据事件的类型判断是自己需要处理的事件。
分享到:
相关推荐
spring发布和接收定制事件(这个上传必须要填资源分啦),ApplicationContext基于Observer模式(java.util包中有对应实现),提供了针对Bean的事件传 播功能。通过Application. publishEvent方法,我们可以将事件...
spring 事务传播 demo
Spring中事务的传播属性详解,Spring中事务的传播属性详解
系统理解spring事务传播属性,和隔离级别
通过代码解析spring传播特性,包括 1、Propagation.REQUIRED 方法被调用时自动开启事务,在事务范围内使用则使用同一个事务,否则开启新事务。 2、Propagation.REQUIRES_NEW 无论何时自身都会开启事务 3、...
spring传播机制个人学习
spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读 spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读
spring事务的传播特性和事务隔离级别
Spring事务传播属性,TransactionDefinition接口中定义的事务传播属性.TransactionDefinition接口中定义的事务传播属性.
Spring事务的传播特性和隔离级别 事务的几种传播特性详细介绍
Spring在Transaction事务传播行为种类,希望对大家有所帮助
Spring事务传播类型.md
事务传播特性&事务隔离级别 详细的事务传播特性&事务隔离级别
spring事务传播
NULL 博文链接:https://zhouchaofei2010.iteye.com/blog/2397918
本文介绍spring事务传播的几种方式
上文理解到对spring事物,事物的隔离机制,这片具体说下事物的传播机制和隔离机制
Spring事务传播原理及数据库事务操作原理.rar
事件传播 ApplicationContext基于Observer模式(java.util包中有对应实现),提供了针对Bean的事件传 播功能。通过Application. publishEvent方法,我们可以将事件通知系统内所有的 ApplicationListener。 事件...