組み込みの EJB コンテナーとクライアントの MDB を使用すると、簡単に実行できます。まさにそれを行う例があります。
この例monitor
のモジュールを確認してください。
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