Skip to content

Commit

Permalink
feat:使用guava eventBus 处理生产消费问题
Browse files Browse the repository at this point in the history
  • Loading branch information
maxy19 committed Sep 16, 2019
1 parent a5145aa commit f72be5c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<log4j-over-slf4j.version>1.7.21</log4j-over-slf4j.version>
<guava.version>18.0</guava.version>
<guava.version>23.0</guava.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mxy.design.observer.event.guava.eventbus;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class WeChatOfficialAccount {

private static List<WeChatUser> users = new ArrayList<>();

public static List<WeChatUser> getUsers() {
return users;
}

public static void setUsers(List<WeChatUser> users) {
WeChatOfficialAccount.users = users;
}

public static void notifyObServer(String msg) {
Preconditions.checkArgument(StringUtils.isNotBlank(msg));
users.stream().forEach(u -> {
u.update(u.getUserName() + "-已收到文章-" + msg + ".");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.mxy.design.observer.event.guava.eventbus;


import com.google.common.base.Objects;

import java.io.Serializable;

/**
* 微信用户 实现观察接口 随时接受更新的信息
*/
public class WeChatUser implements Serializable {

private int id;
private String userName;
private String type;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public WeChatUser(int id, String userName,String type) {
this.id = id;
this.type = type;
this.userName = userName;
}


public void update(String receiveMsg) {
System.out.println(receiveMsg);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WeChatUser that = (WeChatUser) o;
return id == that.id &&
Objects.equal(userName, that.userName);
}

@Override
public int hashCode() {
return Objects.hashCode(id, userName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.mxy.design.observer.event.guava.eventbus;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.junit.Test;

import java.util.Objects;

public class EventBusTest {

@Test
public void logicTest() {
EventBus eventBus = new EventBus();
eventBus.register(new Object() {
@Subscribe
public void followUser(WeChatUser weChatUser) {
if (!Objects.equals(weChatUser.getType(), "followUser")) {
return;
}
System.out.println(weChatUser.getUserName() + "-->关注了该公众号.");
WeChatOfficialAccount.getUsers().add(weChatUser);
}

@Subscribe
public void cancelFollowUser(WeChatUser weChatUser) {
if (!Objects.equals(weChatUser.getType(), "cancelFollowUser")) {
return;
}
System.out.println(weChatUser.getUserName() + "-->取消了关注.");
WeChatOfficialAccount.getUsers().remove(weChatUser);
}
});
eventBus.post(new WeChatUser(1, "张三", "followUser"));
eventBus.post(new WeChatUser(2, "李四", "followUser"));
eventBus.post(new WeChatUser(3, "王五", "followUser"));

WeChatOfficialAccount.notifyObServer("C#学习入门");

//王五不关注了 觉得该公众号很烂 ^_^
eventBus.post(new WeChatUser(3, "王五","cancelFollowUser"));
//继续推送文章给其他两位
WeChatOfficialAccount.notifyObServer("spring学习入门");
}

}

0 comments on commit f72be5c

Please sign in to comment.