{"id":1177,"date":"2016-08-18T17:44:29","date_gmt":"2016-08-18T09:44:29","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=1177"},"modified":"2016-08-18T23:09:38","modified_gmt":"2016-08-18T15:09:38","slug":"%e8%b5%b0%e8%bf%91guava%e5%85%ad-%e4%ba%8b%e4%bb%b6%e6%80%bb%e7%ba%bfeventbus","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=1177","title":{"rendered":"\u8d70\u8fd1Guava(\u516d): \u4e8b\u4ef6\u603b\u7ebfEventBus"},"content":{"rendered":"<div class=\"blog-body\">\n<div class=\"BlogContent\">\n<h1>EventBus:<\/h1>\n<h2>\u521b\u5efaEventBus\u5b9e\u4f8b\uff1a<\/h2>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>EventBus eventBus = new EventBus();\n\/\/\u6216\u8005\nEventBus eventBus = new EventBus(TradeAccountEvent.class.getName());\/\/\u5e26\u6807\u8bc6\u7b26,\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55<\/code><\/pre>\n<h2>\u8ba2\u9605\u4e8b\u4ef6\uff1a<\/h2>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u6a21\u62df\u4e00\u4e2a\u4ea4\u6613\u8fc7\u7a0b\u3002<\/span><\/li>\n<\/ul>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u4e8b\u4ef6\u7c7b\uff1a<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u4e8b\u4ef6\u7c7b\n *\/<!--more-->\n\n\npublic class TradeAccountEvent {\n\tprivate double amount;\n\tprivate Date tradeExecutionTime;\n\tprivate TradeType tradeType;\n\tprivate TradeAccount tradeAccount;\n\n\tpublic TradeAccountEvent(TradeAccount account, double amount,\n\t\t\tDate tradeExecutionTime, TradeType tradeType) {\n\t\tthis.amount = amount;\n\t\tthis.tradeExecutionTime = tradeExecutionTime;\n\t\tthis.tradeAccount = account;\n\t\tthis.tradeType = tradeType;\n\t}\n}\n\n\/**\n * \u8d2d\u4e70\u4e8b\u4ef6\n *\/\npublic class BuyEvent extends TradeAccountEvent {\n\tpublic BuyEvent(TradeAccount tradeAccount, double amount,\n\t\t\tDate tradExecutionTime) {\n\t\tsuper(tradeAccount, amount, tradExecutionTime, TradeType.BUY);\n\t}\n}\n\n\/**\n * \u5356\u51fa\u4e8b\u4ef6\n *\/\npublic class SellEvent extends TradeAccountEvent {\n\tpublic SellEvent(TradeAccount tradeAccount, double amount,\n\t\t\tDate tradExecutionTime) {\n\t\tsuper(tradeAccount, amount, tradExecutionTime, TradeType.SELL);\n\t}\n}<\/code><\/pre>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u8ba2\u9605\u8005<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u5356\u51fa\u548c\u8d2d\u4e70\u5ba1\u8ba1\uff0c\u5373\u8ba2\u9605\u8005\n *\/\npublic class AllTradesAuditor {\n\tprivate List&lt;BuyEvent&gt; buyEvents = Lists.newArrayList();\n\tprivate List&lt;SellEvent&gt; sellEvents = Lists.newArrayList();\n\n\tpublic AllTradesAuditor(EventBus eventBus) {\n\t\teventBus.register(this);\n\t}\n\n\t\/**\n\t * \u8ba2\u9605\u5356\u51fa\u4e8b\u4ef6\n\t *\/\n\t@Subscribe\n\tpublic void auditSell(SellEvent sellEvent) {\n\t\tsellEvents.add(sellEvent);\n\t\tSystem.out.println(\"Received TradeSellEvent \" + sellEvent);\n\t}\n\n\t\/**\n\t * \u8ba2\u9605\u8d2d\u4e70\u4e8b\u4ef6\n\t *\/\n\t@Subscribe\n\tpublic void auditBuy(BuyEvent buyEvent) {\n\t\tbuyEvents.add(buyEvent);\n\t\tSystem.out.println(\"Received TradeBuyEvent \" + buyEvent);\n\t}\n}<\/code><\/pre>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u53d1\u5e03\u8005<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>\/**\n * \u6267\u884c\u4ea4\u6613, \u5373\u53d1\u5e03\u8005\n *\/\npublic class SimpleTradeExecutor {\n\tprivate EventBus eventBus;\n\n\tpublic SimpleTradeExecutor(EventBus eventBus) {\n\t\tthis.eventBus = eventBus;\n\t}\n\n\t\/**\n\t * \u6267\u884c\u4ea4\u6613\n\t *\/\n\tpublic void executeTrade(TradeAccount tradeAccount, double amount,\n\t\t\tTradeType tradeType) {\n\t\tTradeAccountEvent tradeAccountEvent = processTrade(tradeAccount,\n\t\t\t\tamount, tradeType);\n\t\teventBus.post(tradeAccountEvent); \/\/ \u53d1\u5e03\u4e8b\u4ef6\n\t}\n\n\t\/**\n\t * \u5904\u7406\u4ea4\u6613\n\t * \n\t * @return \u4ea4\u6613\u4e8b\u4ef6\n\t *\/\n\tprivate TradeAccountEvent processTrade(TradeAccount tradeAccount,\n\t\t\tdouble amount, TradeType tradeType) {\n\t\tDate executionTime = new Date();\n\t\tString message = String.format(\n\t\t\t\t\"Processed trade for %s of amount %n type %s @ %s\",\n\t\t\t\ttradeAccount, amount, tradeType, executionTime);\n\t\tTradeAccountEvent tradeAccountEvent;\n\t\tif (tradeType.equals(TradeType.BUY)) { \/\/\u8d2d\u4e70\u52a8\u4f5c\n\t\t\ttradeAccountEvent = new BuyEvent(tradeAccount, amount,\n\t\t\t\t\texecutionTime);\n\t\t} else { \/\/\u5356\u51fa\u52a8\u4f5c\n\t\t\ttradeAccountEvent = new SellEvent(tradeAccount, amount,\n\t\t\t\t\texecutionTime);\n\t\t}\n\t\tSystem.out.println(message);\n\t\treturn tradeAccountEvent;\n\t}\n}<\/code><\/pre>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u6d4b\u8bd5\u7528\u4f8b<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>EventBus eventBus = new EventBus();\nAllTradesAuditor auditor = new AllTradesAuditor(eventBus);\nSimpleTradeExecutor tradeExecutor = new SimpleTradeExecutor(eventBus);\ntradeExecutor.executeTrade(new TradeAccount(), 1000, TradeType.SELL);\ntradeExecutor.executeTrade(new TradeAccount(), 2000, TradeType.BUY);<\/code><\/pre>\n<h3>\u53d6\u6d88\u8ba2\u9605\uff1a<\/h3>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u8ba2\u9605\u8005\u6765\u53d6\u6d88\u6ce8\u518c<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>public void unregister(){\n      this.eventBus.unregister(this);\n}<\/code><\/pre>\n<h2>AsyncEventBus\u7c7b<\/h2>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u95fb\u5176\u540d\uff0c\u5c31\u662f<span style=\"color: #e53333; font-size: 16px;\">\u5f02\u6b65\u4e8b\u4ef6\u603b\u7ebf<\/span>\uff0c\u5f53\u5904\u7406\u8017\u65f6\u7684\u5904\u7406\u65f6\u5f88\u6709\u7528\uff0c\u6211\u4eec\u8981\u4f9d\u8d56Executors\u6765\u5b9e\u73b0\u5f02\u6b65\u4e8b\u4ef6\u603b\u7ebf<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>AsyncEventBus asyncEventBus = new AsyncEventBus(executorService);<\/code><\/pre>\n<h3>DeadEvents:<\/h3>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u5f53\u603b\u7ebf\u63a5\u6536\u5230\u53d1\u5e03\u8005\u53d1\u5e03\u7684\u4fe1\u606f\u65f6\uff0c\u4f46\u8fd9\u65f6\u6ca1\u6709\u8ba2\u9605\u8005\uff0c\u90a3\u4e48\u8be5\u4e8b\u4ef6\u4f1a\u88ab\u5305\u88c5\u4e3a<span style=\"color: #e53333; font-size: 16px;\">DeadEvent<\/span>\u4e8b\u4ef6<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>public class DeadEventSubscriber {\n\tprivate static final Logger logger = \n\t\t\tLogger.getLogger(DeadEventSubscriber.class.getName());\n\n\tpublic DeadEventSubscriber(EventBus eventBus) {\n\t\teventBus.register(this);\n\t}\n\t\n\t\/**\n\t * \u6ca1\u6709\u8ba2\u9605\u8005\u65f6\u88ab\u89e6\u53d1\n\t *\/\n\t@Subscribe\n\tpublic void handleUnsubscribedEvent(DeadEvent event){\n\t\tlogger.warning(\"No subscribers for \"+event.getEvent());\n\t}\n}<\/code><\/pre>\n<h3>\u4f9d\u8d56\u6ce8\u5165<\/h3>\n<ul>\n<li><span style=\"line-height: 1.5; font-size: 12.5px;\">\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7DI\u6846\u67b6(Spring\u6216Guice)\u6765\u6ce8\u5165\u540c\u6837\u7684EventBus<\/span><\/li>\n<\/ul>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>@Component\npublic class SimpleTradeExecutor {\n       private EventBus eventBus;\n       @Autowired\n       public SimpleTradeExecutor(EventBus eventBus) {\n           this.eventBus = checkNotNull(eventBus, \"EventBus can't be null\"); \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n}<\/code><\/pre>\n<pre class=\"brush:java; toolbar: true; auto-links: false;\"><code>@Component\npublic class SimpleTradeAuditor {\n       private List&lt;TradeAccountEvent&gt; tradeEvents =\n\u00a0\u00a0\u00a0\u00a0Lists.newArrayList();\n    @Autowired\n    public SimpleTradeAuditor(EventBus eventBus){\n           checkNotNull(eventBus,\"EventBus can't be null\");\n           eventBus.register(this);\n   }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5c31\u4ecb\u7ecd\u4e86Guava\u7684EventBus\u3002<\/p>\n<p>\u6765\u6e90: http:\/\/my.oschina.net\/indestiny\/blog\/219421<\/p>\n<\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"cVVTU3quMC\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1171\">\u8d70\u8fd1Guava(\u4e00): \u57fa\u672c\u5de5\u5177<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e00): \u57fa\u672c\u5de5\u5177\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1171&#038;embed=true#?secret=zuIioubYab#?secret=cVVTU3quMC\" data-secret=\"cVVTU3quMC\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"wsM07Yat2F\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1173\">\u8d70\u8fd1Guava(\u4e8c): \u51fd\u6570\u5f0f\u7f16\u7a0b<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e8c): \u51fd\u6570\u5f0f\u7f16\u7a0b\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1173&#038;embed=true#?secret=NdQxApQi5y#?secret=wsM07Yat2F\" data-secret=\"wsM07Yat2F\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"LjPG1C7AXJ\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1174\">\u8d70\u8fd1Guava(\u4e09): \u96c6\u5408<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e09): \u96c6\u5408\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1174&#038;embed=true#?secret=GQZa8GP3LW#?secret=LjPG1C7AXJ\" data-secret=\"LjPG1C7AXJ\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"kKS7SbPyOK\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1175\">\u8d70\u8fd1Guava(\u56db): \u5e76\u53d1<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u56db): \u5e76\u53d1\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1175&#038;embed=true#?secret=m84jvJ21lv#?secret=kKS7SbPyOK\" data-secret=\"kKS7SbPyOK\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"anmITtnNQH\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1176\">\u8d70\u8fd1Guava(\u4e94): \u7f13\u5b58<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e94): \u7f13\u5b58\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1176&#038;embed=true#?secret=DnvW2Jlutd#?secret=anmITtnNQH\" data-secret=\"anmITtnNQH\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Jn9hRZ73gL\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1177\">\u8d70\u8fd1Guava(\u516d): \u4e8b\u4ef6\u603b\u7ebfEventBus<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u516d): \u4e8b\u4ef6\u603b\u7ebfEventBus\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1177&#038;embed=true#?secret=0SKCGAsGX2#?secret=Jn9hRZ73gL\" data-secret=\"Jn9hRZ73gL\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<div class=\"video-container\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"MCAIcuuj8M\"><p><a href=\"https:\/\/www.yeetrack.com\/?p=1178\">\u8d70\u8fd1Guava(\u4e03): \u6587\u4ef6\u64cd\u4f5c<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"\u300a\u8d70\u8fd1Guava(\u4e03): \u6587\u4ef6\u64cd\u4f5c\u300b\u2014\u7a7a\u7a7a\u535a\u5ba2\" src=\"https:\/\/www.yeetrack.com\/?p=1178&#038;embed=true#?secret=K1TC99pp8X#?secret=MCAIcuuj8M\" data-secret=\"MCAIcuuj8M\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>EventBus: \u521b\u5efaEventBus\u5b9e\u4f8b\uff1a EventBus eventBus = new EventBus(); \/\/\u6216\u8005 EventBus eventBus = new EventBus(Tr&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[65,8],"class_list":["post-1177","post","type-post","status-publish","format-standard","hentry","category-coding","tag-guava","tag-java"],"views":5112,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1177"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1177\/revisions"}],"predecessor-version":[{"id":1190,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1177\/revisions\/1190"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}