gpt4 book ai didi

java - EJB 服务器端能否向 EJB 客户端发送事件?

转载 作者:搜寻专家 更新时间:2023-11-01 02:31:34 28 4
gpt4 key购买 nike

我想知道是否可以按照这些思路做一些事情:

1)服务器端(EJB类)

@Statefull
public class SomeEJB implements SomeEJBRemote {

@Resource
private SessionContext sessionContext;

//(...)
public void someMethod(Record record){
//(...)
//Situation that requires the server to alert all registered listeners
if(specialSituation){
sessionContext.fireEventToListeners(new SomeEvent());
}

//Do something else...
}
}

2) 客户端

//(...) 
SomeEJBRemote ejb = initialContext.lookup("SomeEJBRemote");
ejb.addListener(this);

void EJBEventListener(SomeEvent event){
System.out.println("EJB server has sent an event!");
}

A) Java EE 规范中是否有任何内容允许这样做?

B) 我知道 JBoss 允许某种双向通信,但我可以像那个例子那样做吗?

C) OpenEJB(或 TOMEE)中有类似的东西吗?

最佳答案

在客户端中使用嵌入式 EJB 容器和 MDB 很容易做到。我们有一个例子可以做到这一点。

查看this examplemonitor模块| .

此示例在 10,000 英尺处执行以下操作:

服务器端:

  • 包装对 EntityManager 的访问的@Stateless bean
  • JMS 消息发送到关于所有添加/删除操作的主题

客户端:

  • 嵌入式 EJB 容器/MDB 接收消息
  • 收到通知后,通过 java.awt.SystemTray
  • 向用户发出通知

所以这项技术的有趣之处在于它是完全事务性的——EntityManager 更新发送的 JMS 消息都是事务的一部分。如果数据库更新失败,则不会发送 JMS 消息。

这是该示例中 100% 的客户端代码。执行所描述的操作并不需要太多。

客户端“主”类

import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

public class NotificationMonitor {
private static TrayIcon trayIcon;

public static void main(String[] args) throws NamingException, InterruptedException, AWTException, MalformedURLException {
addSystemTrayIcon();

// Boot the embedded EJB Container
new InitialContext();

System.out.println("Starting monitor...");
}

private static void addSystemTrayIcon() throws AWTException, MalformedURLException {
SystemTray tray = SystemTray.getSystemTray();

URL moviepng = NotificationMonitor.class.getClassLoader().getResource("movie.png");
Image image = Toolkit.getDefaultToolkit().getImage(moviepng);

ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting monitor...");
System.exit(0);
}
};

PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);

trayIcon = new TrayIcon(image, "Notification Monitor", popup);
trayIcon.setImageAutoSize(true);
tray.add(trayIcon);
}

public static void showAlert(String message) {
synchronized (trayIcon) {
trayIcon.displayMessage("Alert received", message, TrayIcon.MessageType.WARNING);
}
}
}

客户端 MDB

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "notifications")})
public class NotificationsBean implements MessageListener {

public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();

NotificationMonitor.showAlert(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}

客户端jndi.properties文件

这会配置嵌入式 EJB 容器。您也可以在代码中执行此操作。

java.naming.factory.initial=org.apache.openejb.client.LocalInitialContextFactory
Default\ JMS\ Resource\ Adapter=new://Resource?type=ActiveMQResourceAdapter
Default\ JMS\ Resource\ Adapter.BrokerXmlConfig=broker:vm://localhost
Default\ JMS\ Resource\ Adapter.ServerUrl=tcp://localhost:61616

关于java - EJB 服务器端能否向 EJB 客户端发送事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8154768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com