Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event插件如何让用户关注某主题 #53

Open
Rekoe opened this issue Jun 15, 2017 · 11 comments
Open

event插件如何让用户关注某主题 #53

Rekoe opened this issue Jun 15, 2017 · 11 comments

Comments

@Rekoe
Copy link
Member

Rekoe commented Jun 15, 2017

@qinerg
RT

@Rekoe
Copy link
Member Author

Rekoe commented Jun 15, 2017

另外 同一个类型的 监听 允许定义多个 ?

subscribeTopic

@qinerg
Copy link
Member

qinerg commented Jun 15, 2017

用户需要自行实现 EventListener 接口,并通过 subscribeTopic() 方法,返回自己关注的主题。EventBus 会在该主题被发布时主动去调用监听类的 onEvent(Event e) 方法。
具体的实现可以参考 test 下的测试用例。

目前一个事件的处理类只能处理单一类型的事件,为降低代码的耦合性也建议一个类只处理一种事件。
如果未来发现有什么特别的好处,可以考虑一个类处理多种类型的事件。

@Rekoe
Copy link
Member Author

Rekoe commented Jun 16, 2017

@qinerg 你的意思是在EventListener里维护所有的订阅者么

@qinerg
Copy link
Member

qinerg commented Jun 16, 2017

EventListener负责消费事件,EventBus里维护所有主题的订阅者,当事件发生后,EventBus负责将消息推送至每一个订阅了此主题的EventListener消费者。
举个实例说明吧:
有两个消费者同时订阅了reg-user(用户注册)事件:

/**
 * 注册成功后,给用户增加积分
 */
@IocBean
public class AddCreditsListener implements EventListener {
	@Inject
	private CreditsService creditsService;

	@Override
	public String subscribeTopic() {
		return "reg-user";
	}

	@Async
	@Override
	public void onEvent(Event e) {
		NutMap user = (NutMap) e.getParam();
		Long uid = user.getLong("id");
		creditsService.addCredits(uid, 100); // 给用户增加100个金豆
	}
}`

`/**
 * 日志跟踪消费者
 */
@IocBean
public class LogEventListener implements EventListener {

	private Log log = Logs.get();

	@Override
	public String subscribeTopic() {
		return "reg-user";
	}

	@Override
	public void onEvent(Event e) {
		log.debugf("->new user event: %s", Json.toJson(e.getParam(), JsonFormat.compact()));
	}
}

当注册事件触发时:
Event e1 = new Event("reg-user", newUser);
eventBus.fireEvent(e1);
这时eventBus会将此消息通知到 AddCreditsListener 和 LogEventListener 两个消费者消费。
灵活使用事件机制,可降低代码的耦合度。一旦业务变更,如注册的同时要发送邮件通知,我们不需要修改原来的注册流程,只需要新增加一个 EmailListener 消费者即可,无需修改任何代码。

@Rekoe
Copy link
Member Author

Rekoe commented Jun 16, 2017

@qinerg 你QQ 或微信是什么 我有问题请教下

@wendal
Copy link
Member

wendal commented Jun 16, 2017

我想改名叫eventbus

@Rekoe
Copy link
Member Author

Rekoe commented Jun 16, 2017

还是没弄明白 整个流程 我加 下作者咨询下

@Rekoe
Copy link
Member Author

Rekoe commented Jun 16, 2017

public void fireEvent(Event event)

这个方法负责把消息分发给所有订阅的用户么 ?
@wendal

@Rekoe
Copy link
Member Author

Rekoe commented Jun 16, 2017

好像明白点了 ……

@qinerg
Copy link
Member

qinerg commented Jun 16, 2017

@Rekoe 有疑问的话,在 nutz.cn 上发贴问吧,你可以把你的所有疑惑说出来,我尽力解答。
在 nutz.cn 上发贴的话,也希望引起更多人对这个插件的关注。
其实“事件”机制就类似于生产者消费者模式,很多场景都会用到,不难理解。

@qinerg
Copy link
Member

qinerg commented Jun 16, 2017

@wendal 改名我无意见,呵呵

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants