一,介绍
本文介绍一个简单的聊天应用程序:生产者将消息发送到Topic上,然后由ActiveMQ将该消息Push给订阅了该Topic的消费者。示例程序来自于《JAVA 消息服务--第二版 Mark Richards著》
二,项目开发环境搭建
①使用Eclipse新建一个JAVA工程:jms_activemq
②添加依赖包:activemq-all-5.13.2.jar
③创建JNDI文件: jndi.properties,并将之放到classpath下(即eclipse项目的 src 目录)。 文件内容如下:
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=tcp://192.168.121.35:61616
java.naming.security.principal=system
java.naming.security.credentials=manager # use the following property to specify the JNDI name the connection factory
# should appear as.
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry
connectionFactoryNames = TopicCF
#TopicConnectionFactory conFactory = (TopicConnectionFactory)ctx.lookup(topicFactory) # register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
## queue.MyQueue = example.MyQueue # register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
## topic.MyTopic = example.MyTopic
topic.topic1=jms.topic1
#Topic chatTopic = (Topic)ctx.lookup(topicName);
注意:jndi.properties文件中的值不要拼写错误了(此外,等号右边的值的末尾,不能带有空格!!!)。否则会抛出:java.lang.ClassNotFoundException
④新建源文件。编写程序。项目截图如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAckAAADPCAIAAAARc72PAAAgAElEQVR4nO2dW3AU14GG9eJ3PaQq8VMSX1gqqXVsfNlk4shOVSrYm9Tau1WqAmq963UqsybZGIjtcIkIgww2lqA8tgDhRJFFbF3AVswlyOK2ICEDjikiYxBaCwRCYKQZjTRISJqRIL0PLU2fe5+Z6bn09P9VF57pOX1Od8v6dPp0z/mLWg6dMgAAACRJy6FTfV9OJJbE+lgsFovFiuBWAABIAbgVAACcJwduHRqOvv3uh1W1O6tqd9a89+HYzXGmwNzntjreKAAAZJMcuDX4+8aBaxcjg31Dg5e7z3VWvf0uUwBuBQC4nay6dTAUKX+t6vyRV8YvvBG/sGni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Zd7tbNVnzm3/hF2/EaHsuL8tuUCKrCB/+0SCzD9rrXOEXJKn+ONKiwN3qYbzmViRR2+NZt+bkK3Nwa6HiMbemc7WfN2S6e+Vdt+YCuLVQ8YpbkUStD9yaTeBWAABwHrgVAACcR+3W/wczEZmuHNZUSwAAAABJRU5ErkJggg==" alt="" />
⑤配置activemq.xml文件,在broker下 添加一个 destinations,并启动ActiveMQ
配置如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABUcAAACSCAIAAADpduyWAAAgAElEQVR4nO3dPXKjzNbAcWJXOZjAVZNMKZpJVE/kcjIbuN6AMseObnkBSq6WMBtQoOAm7wKccrWOCSithDdASP3dh6ahkfx/6hfMIyM4NA3i0B9U1foFAAAAAADcpPIRAAAAAACANOUjAAAAAAAAacpHAAAAAAAA0pSPAAAAAAAApCkfAQAAAAAASFM+AgAAAAAAkKZ8BAAAAAAAIM2oL9dt1bZVe5wp1s2hatuqbav9W/mCy+rH/nHXVmf1r+LxVKtjd2g/Vy/Vz8Pfqm2r9u/3vtj7Tz5XpeMc7ff3U/twGL/M9HafVdt+25UO4x7dUX2WmKQ+30gZLuNcDgpfewWe6+6n5Ol5noA3q4+22rXVx/6f8qW3TIJj+vv7qT3f3yziOh+JZ5nn++hzx6m/PXt83ZTewbhfbwu5h7xFQ+rP+7cMVWted34P2V+yTn9+lg/Ga5prlGbs9zeH+bL6Tn2HWf3Fj/3jIq7I59rWnR67T9fv958H0S+65NpX8Poo2QvhnqYR7/udX5GLm/QoL8p0e7r8Mlx+hJJrb9xm9TFbVr9+qdYvz/XkWX3/tKLaNStlN6tlPRB3GnJMf38/Leo6H4hn5rMp/luZ5dxx+/WWlNWPPy9eG6WG9/zrXFBWP8M1IbMh9ef9W3t57BWoY38eJn/4xT2kvo8pWX22HKRL2gOFPOE1qjf4C12Deb1T/pesPpuFZfXH9/VLdb4wmafKPWT1q+NlH0ctM8INPvG9TzeQ72UxZX1eehlOfC7nIbj2xt1jVt/vV7Vrq7et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fHePJpD7Nfv5TM6p1bKT6uPhyzvAzXzIEPAAAAAJbyEYzhG1kdzVqNlvz9yZoD35qbff1idg4/d3ePzZ+3tuai7x5JSNq6wztlLLw/aQFfeuPbaXCueIw/2f0jRFm9oAx5Xz0AAAAAOJWPYKQu34s3/1rUkfPNoe/a3Zp5cmCMerch453toRnslVeyq38y1mBvThst3313p3yiPIzQtnKqNv1uOqflT47HvRI9gRfGLClD37MbAAAAAED5CLJztpYDAAAAAHB/ykeQ1+ZAuy4AAAAA4KsoHwEAAAAAAEhTPgIAAAAAAJCmfAQAAAAAACBN+QgAAAAAAECa8hEAAAAAAIA05SMAAAAAAABpykcAAAAAAADSlI8AAAAAAACkKR8BAAAAAABIUz4CAAAAAACQpnwEAAAAAAAgTfkIAAAAAABAmvIRAAAAAACAJP8PLHBxwdlm7ecAAAAASUVORK5CYII=" alt="" />
这里详细说下如下启动ActiveMQ,一种方式是在前台启动,另一个方式是在后台启动。如果在前台终端中启动的话,当关闭终端后程序也就终止了。而在后台启动,关闭终端不影响它的启动。
前台启动: ./bin/activemq start 或者 ./bin/activemq console
后台启动:参考
nohup ./bin/activemq start &
在后台启动后,如何关闭呢?首先找到相应的进程号: ps -ef | grep activemq,然后 kill pid 即可。
三,JNDI解释
JNDI是一个与具体实现无关的API,不管你用的是ActiveMQ还是WebSphereMQ还是Microsoft Message Queuing,它都可用作目录服务。JMS Client使用目录服务来访问ConnectionFactory和Destination(即主题和队列对象)。
ConnectionFactory及Destination 与JMS中的Producer、Consumer、Session、Connection不同,后者是用JMS API通过工厂模式直接生成的。而前者则需要JNDI查询获得。
在JMS中,JNDI主要用于命名服务来定位受管对象。受管对象就是由系统管理员创建和配置的JMS对象。受管对象包括JMS ConnectionFactory 和 Destination 对象 受管对象和命名服务中的一个名称相互绑定在一起。一个命名服务将名称和分布式对象、文件、设施关联起来,以便它们可以使用简单的名称而不是密码似的网络地址实现在网络上的定位。
如,DNS,它将域名www.cnblogs.com 转换成一个网络地址(ip),而浏览器使用该ip连接到web服务器上 命名服务将分布式对象、JMS受管对象……和名称相互绑定,并按照和文件系统相似的层次进行组织 JNDI提供了一个隐藏命名服务细节的抽象。使用JNDI,JMS Client 能够浏览一个命名服务,并在不知道命名服务细节的情况下引用受管对象。 通常,JMS服务器会和一个标准的JNDI驱动器(也称为服务提供者)以及类似于LDAP(轻量级目录访问协议)这样的目录服务 结合使用。或者,JMS 服务器提供一个私有的JNDI服务提供者 和 目录服务。
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=tcp://192.168.121.35:61616
//other jndi config......
四,建立连接的详细过程
Chat类的构造函数通过连接JMS服务器使用的JNDI命名服务来建立连接
public Chat(String topicFactory, String topicName, String userName)throws Exception {
InitialContext ctx = new InitialContext();//使用jndi.properties 获得一个JNDI连接
InitialContex就是所有JNDI查找的起始点,相当于文件系统的根目录。它提供了一条到目录服务的网络连接,这个目录服务就发挥访问JMS受管对象的根目录的作用。
在我们的示例中,ActiveMQ就是JMS服务器,JMS受管对象就是在 conf/activemq.xml 文件中配置的destionations 和 ConnectionFactory
通过new一个InitialContex对象,就建立起了一个到ActiveMQ目录服务的连接,然后在ActiveMQ的命名服务中查找TopicConnectionFactory
TopicConnectionFactory conFactory = (TopicConnectionFactory)ctx.lookup(topicFactory);
受管对象TopicConnectionFactory的属性和行为则由系统管理员配置。比如,一个连接工厂可配置为使用特定的协议、安全模式...连接。
有了TopicConnectionFactory之后,就可以创建TopicConnection了。
TopicConnection connection = conFactory.createTopicConnection();
连接的认证则可以在jndi.properties文件中配置
java.naming.security.principal=system
java.naming.security.credentials=manager
TopicConnection表示和消息服务器(ActiveMQ)的一个连接。一个JMS Client 可以选择从同一连接工厂创建多个连接,但是由于创建连接开销很大,一般是从同一连接中创建多个Session对象。
TopicSession pubSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSession subSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
关闭TopicConnection将关闭和该连接有关的所有对象,包括TopicSession,TopicPublisher、TopicSubscriber。
TopicSession是创建Message、TopicPublisher、TopicSubscriber对象的工厂,用它来创建TopicPublisher和TopicSubscriber。TopicPublisher和TopicSubscriber对象都有一定特定的Topic标识,且这两个对象专属于创建它们的TopicSession。
TopicPublisher publisher = pubSession.createPublisher(chatTopic);
TopicSubscriber subscriber = pubSession.createSubscriber(chatTopic, null, true);
TopicPublisher向Topic发送消息,TopicSubscriber从Topic订阅消息。JNDI用于定位一个Topic对象,它是类似于TopicConnectionFactory的一个受管对象。
Topic chatTopic = (Topic)ctx.lookup(topicName);
Topic对象是消息服务器上的一个实际主题的句柄,该主题称为物理主题(physical topic)。一个物理主题就是多个Client订阅和发布消息的一条电子通道。
这样,发布者和订阅者就知道往哪里发送/接收消息了。
TopicPublisher是将消息异步发送到Topic,TopicPublisher不会阻塞直到所有的订阅者接收到该消息为止,而是只要消息服务器一接收到消息,TopicPublisher就会从publish()方法返回。
它依赖消息服务器将消息传送给该主题的所有订阅者,消息服务器将消息异步推送给TopicSubscriber。
整个完整程序代码:
import java.io.BufferedReader;
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; 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 Exception {
InitialContext ctx = new InitialContext();//使用jndi.properties 获得一个JNDI连接 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); Topic chatTopic = (Topic)ctx.lookup(topicName); TopicPublisher publisher = pubSession.createPublisher(chatTopic);
TopicSubscriber subscriber = pubSession.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 e){e.printStackTrace();}
} public 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) {
try{
if(args.length != 3)
System.out.println("Factory, Topic or userName missing");
Chat chat = new Chat(args[0], args[1], args[2]); BufferedReader commandLine = new java.io.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(Exception e){e.printStackTrace();}
}
}