我正在做一个小的Java项目,下面是我试图实现的基本思想:我在netty-socketio上有一个层,它接受来自浏览器的socket.io请求,我使用JPA2.1/hibernate将所请求的更改持久化到DB中。不同之处在于,我也有流请求的概念,因为用户将请求集合的当前状态和所有未来的更新。为了从数据库获取实时更新,我使用了实体侦听器。我正在寻找一种可靠的方法,将实体侦听器方法连接到socketio连接之上的处理程序,即,当流处理程序感兴趣的数据发生更改时,它应该得到通知,以便它可以将更新发送到管道。我试着想出一种单一的版主,实体监听器可以向其发布更新,订阅的处理程序可以使用它,所有这些都基于String topic,就像发布订阅一样。我遇到的问题是:让我们以POJO User为例。当插入新的user时,UserEntityListener#PostInsert开始工作,并通过.publish调用将user转发给Notifier。Notifier使用<?>作为数据类型,并通过Callable-like接口调用相关方:
public interface Notifiable {
public <T> void onEvent(T data);
}所以现在在正确的处理程序中调用了它的实现,但它具有泛型类型,我必须手动转换它(处理程序知道它应该接收的类型)。我的问题是,我可以在没有显式强制转换的情况下做到这一点吗?有没有一个好的框架可以让所有这些低级的修补工作变得无用?我想要一个集中的解决方案来弥补这个差距,否则所有的样板都会要了我的命。
编辑添加的相关来源。
通知程序类:
class Subscriber {
public String topic;
public Notifiable notifiable;
public Subscriber(String topic, Notifiable n) {
this.topic = topic;
this.notifiable = n;
}
}
public class Notifier {
private static Notifier instance = null;
private List<Subscriber> subscribers = new ArrayList<Subscriber>();
public Notifier() {};
public void subscribe(String topic, Notifiable n) {
if (!this.hasSubscriber(topic, n)) {
this.subscribers.add(new Subscriber(topic, n));
}
}
public <T> void publish(String topic, T data) {
for (Subscriber s : this.subscribers) {
if (s.topic.equals(topic)) {
s.notifiable.onEvent(data);
}
}
}
public Boolean hasSubscriber (String topic, Notifiable n) {
for (Subscriber s : this.subscribers) {
if (s.topic.equals(topic) && s.notifiable == n) {
return true;
}
}
return false;
}
public static Notifier getInstance() {
if (instance == null) {
instance = new Notifier();
}
return instance;
}
}实体监听器:
@PostPersist
public void PostInsert(User u) {
Notifier.getInstance().publish("user/new", u);
}Socketio处理程序:
Notifier.getInstance().subscribe("user/new", (new Notifiable() {
@Override
public <T> void onEvent(T data) {
User u = (User) data;
logger.info("User name: " + u.getUsername());
}
}));发布于 2016-08-02 14:47:44
如果要避免显式强制转换,请进行以下更改:
其一,让你的Notifiable接口泛型:
public interface Notifiable<T> {
public void onEvent(T data);
}第二,使Subscriber类也是泛型的:
public class Subscriber<T> {
public String topic;
public Notifiable<T> notifiable;
public Subscriber(String topic, Notifiable<T> n) {
...
}
}三、自适应通告类
public class Notifier {
private static Notifier instance = null;
@SuppressWarnings("rawtypes")
private List<Subscriber> subscribers = new ArrayList<Subscriber>();
public Notifier() {};
public <T> void subscribe(String topic, Notifiable<T> n) {
if (!this.hasSubscriber(topic, n)) {
this.subscribers.add(new Subscriber<T>(topic, n));
}
}
@SuppressWarnings("unchecked")
public <T> void publish(String topic, T data) {
for (Subscriber<T> s : this.subscribers) {
if (s.topic.equals(topic)) {
s.notifiable.onEvent(data);
}
}
}
@SuppressWarnings("unchecked")
public <T> Boolean hasSubscriber (String topic, Notifiable<T> n) {
for (Subscriber<T> s : this.subscribers) {
/* XXX: Beware, is safe to compare two notifiable
* instances by their memory addresses??
*/
if (s.topic.equals(topic) && s.notifiable == n) {
return true;
}
}
return false;
}
public static Notifier getInstance() {
if (instance == null) {
instance = new Notifier();
}
return instance;
}
}第四,Socketio Handler:
Notifier.getInstance().subscribe("user/new", (new Notifiable<User>() {
@Override
public void onEvent(User data) {
logger.info("User name: " + u.getUsername());
}
}));https://stackoverflow.com/questions/38708622
复制相似问题