我正在使用这些技术在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");
}
答案 0 :(得分:0)
如果您想在进程和计算机之间进行通信,则需要一个消息服务,例如Apache's ActiveMQ。 要邀请用户加入主题,您需要另一种通信方法。明亮的两种模式:
一般情况下,如果您想了解聊天服务的模式,最好查看XMPP。