【发布时间】:2015-10-05 18:13:53
【问题描述】:
我正在使用以下技术在 Java 中做一个服务器-客户端群聊应用程序:JMS、EJB(远程无状态、消息驱动 bean)、Spring(依赖注入)、Hibernate(数据库内容)。我在 mysql 数据库中有一个用户(用户名,密码)表。问题是它需要:登录后用户可以邀请其他用户与他聊天,我不知道该怎么做?因此,如果我邀请某人,我必须在 glassfish 中创建一个新主题,或者无论如何要使用 1 个主题?如何在运行时在 glassfish 中创建主题?有人建议我使用选择器来过滤消息,这样我就不必创建多个主题,可以这样做吗?以及如何在我的应用程序中与其他用户通信以向他们发送邀请?因此,任何人都可以向我提出任何想法、代码、示例......关于如何做到这一点将不胜感激。到目前为止。我已经设法让用户在我在 glassfish Web 控制台中预定义的主题中互相聊天。这是我的客户端代码的一部分。
Context ctx;
try {
Properties props = new Properties();
ctx = new InitialContext(props);
tcf = (TopicConnectionFactory) ctx.lookup("p2scon");
tpConnection = tcf.createTopicConnection(); // anomyous access
tpConnection.setClientID(tfName.getText());
// false means the TopicSession will not be transacted
// acknowledgment mode used by the JMS client.
// An acknowledgment is a notification to the message server that the client has received the messag
// AUTO_ACKNOWLEDGE means the message is automatically acknowledged after it is received by the client
tpSession = tpConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
// look up JMS topic
topic = (Topic) ctx.lookup("p2sdes");
tpPublisher = tpSession.createPublisher(topic);
/* Nondurable subscribers are temporary subscriptions that receive messages only when
they are actively listening on the topic. Durable subscribers, on the other hand, will
receive a copy of every message published, even if they are “offline” when the message
is published.*/
tpSubscribe = tpSession.createDurableSubscriber(topic, tfName.getText());
tpSubscribe.setMessageListener(MainFrame1.this);
tpConnection.start();
/*t = new Thread(MainFrame1.this);
t.start();*/
btnConnect.setVisible(false);
} catch (NamingException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Can't connect");
} catch (JMSException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Can't connect");
}
【问题讨论】:
-
您知道,尽管 JMS 代表
Java Messaging Service,但它并不是真正用于聊天应用程序的东西? -
是的!我知道!但仍然。这是我的导师要求我做的一个项目,所以我无能为力。只需要处理它。
标签: java spring glassfish ejb jms