我正在开发一个小型Java项目,这是我想要实现的基本想法:我在netty-socketio上有一个层,它接受来自浏览器的socket.io请求,并且我使用JPA 2.1/hibernate 将请求的更改持久保存到数据库中。不同的是,我也有流请求的概念,因为用户将请求集合的当前状态和所有 future 的更新。为了从数据库获取实时更新,我使用实体监听器。我正在寻找一种将实体监听器方法连接到 socketio 连接顶部的处理程序的可靠方法,即当流处理程序感兴趣的数据发生更改时,应该通知它,以便它可以将更新发送到管道中。我试图想出一种单例主持人,实体监听器可以向其发布更新,订阅的处理程序可以使用它,所有这些都基于 String topic
,非常像一个 pubsub。我遇到的问题是:让我们以 POJO User
为例。 。当新的user
插入后,UserEntityListener#PostInsert
启动后,它会转发 user
到Notifier
通过.publish
称呼。 Notifier
使用<?>
对于数据类型,它通过 Callable
调用感兴趣的各方类似界面:
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());
}
}));
最佳答案
如果您想避免显式转换,请进行以下更改:
一,使您的 Notabilized 界面变得通用:
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) {
...
}
}
三、适配Notifier类
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());
}
}));
关于java - Hibernate 实体监听器和 netty-socketio 之间的中介,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708622/