import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.Session; import javax.jms.TextMessage; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicPublisher; import javax.jms.TopicSession; import javax.jms.TopicSubscriber; import javax.naming.InitialContext; import javax.naming.NamingException; public class Chat implements MessageListener{ private TopicSession pubSession; private TopicPublisher publisher; private TopicConnection connection; private String username; public Chat(String topicFactory,String topicName,String username) throws NamingException, JMSException { InitialContext ctx = new InitialContext(); TopicConnectionFactory conFactory = (TopicConnectionFactory)ctx.lookup(topicFactory); TopicConnection connection = conFactory.createTopicConnection(); TopicSession pubSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); TopicSession subSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); //查找一个JMS主题 Topic chatTopic =(Topic)ctx.lookup(topicName); TopicPublisher publisher = pubSession.createPublisher(chatTopic); TopicSubscriber subscriber = subSession.createSubscriber(chatTopic,null,true); subscriber.setMessageListener(this); this.connection = connection; this.pubSession = pubSession; this.publisher = publisher; this.username = username; connection.start(); } @Override public void onMessage(Message message) { try { TextMessage textMessage =(TextMessage)message; System.out.println(textMessage.getText()); } catch(JMSException jmse){ jmse.printStackTrace(); } } protected void writeMessage(String text)throws JMSException { TextMessage message = pubSession.createTextMessage(); message.setText(username + ":" + text); publisher.publish(message); } public void close()throws JMSException { connection.close(); } public static void main(String[] args) { if (args.length != 3) { System.out.println("Factory,topic or username missing!"); try { Chat chat = new Chat(args[0],args[1],args[2]); BufferedReader commandLine = new BufferedReader(new InputStreamReader(System.in)); while(true) { String s = commandLine.readLine(); if (s.equalsIgnoreCase("exit")) { chat.close(); System.exit(0); }else{ chat.writeMessage(s); } } } catch (NamingException e) { e.printStackTrace(); } catch (JMSException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
上面这个就是主类:
在工程里需要把jms包加进来,我用的是activeMq5.4 包名-activemq-all-5.4.0.jar
jndi.properties文件内容如下:
java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory java.naming.provider.url=tcp://localhost:61616 java.naming.security.principal=system java.naming.security.credentials=manager connectionFactoryNames=TopicCF topic.topic1=jms.topic1
在提示符下输入:
D:\eclipse\workspace\jms\bin>java Chat TopicCF topic1 TOM -classpath=D:\eclipse\
workspace\jms\lib\activemq-all-5.4.0.jar
打开另一个窗口输入:
D:\eclipse\workspace\jms\bin>java Chat TopicCF topic1 jack -classpath=D:\eclipse\
workspace\jms\lib\activemq-all-5.4.0.jar
但别忘了启动 activeMq bin下的activemq.bat
这时在提示符下输入信息,就会在窗口中看到了