`
angie_hawk7
  • 浏览: 46731 次
  • 性别: Icon_minigender_1
  • 来自: 乌托邦
社区版块
存档分类
最新评论

Spring的事件传播

阅读更多
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可以根据事件的类型判断是自己需要处理的事件。



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics