openjms在server中新增消息队列使用JmsAdminServerIfc类(接口)。这里我只是调用了addDestination方法在创建queue和topic
/**
* Add a specific destination with the specified name
*
* @param name destination name
* @param queue whether it is queue or a topic
* @return boolean true if successful
* @exception JMSException
*/
boolean addDestination(String destination, Boolean queue)
throws JMSException;
在看一段addDestination的实现类代码:
/**
* Add an administered destination with the specified name.
*
* @param name destination name
* @param queue whether it is queue or a topic
* @return boolean true if successful
*/
public boolean addDestination(String name, Boolean queue) {
boolean success = false;
// create the appropriate destination object
JmsDestination destination = (queue.booleanValue())
? (JmsDestination) new JmsQueue(name)
: (JmsDestination) new JmsTopic(name);
destination.setPersistent(true);
// create the administered destination
try {
if (_destinations.getDestination(name) == null) {
_destinations.createDestination(destination);
success = true;
}
} catch (JMSException exception) {
_log.error("Failed to add destination=" + name, exception);
}
return success;
}
添加已经存在的名称的queue或是topic的时候会直接返回false 测试代码中未判断只是测试而已不比追究业务逻辑问题:)
测试代码:(示例代码地址:http://download.csdn.net/source/1857751)
/*
*author:a276202460
*Create Date:2009-11-29
*
*/
package com.rich.jmstest;
import javax.naming.NamingException;
import com.rich.jmstest.start.JmsContextHelper;
import com.rich.jmstest.start.JmsStater;
public class Test {
public static void main(String[] s){
try{
QueueProduct queueproduct = new QueueProducter();
TopicProduct topicproduct = new TopicProducter();
queueproduct.createQueue("richqin");
queueproduct.createQueue("a276202460");
topicproduct.creatTopic("csdn");
topicproduct.addSubscriber("csdn", "richqin");
topicproduct.addSubscriber("csdn", "a276202460");
queueproduct.sendMsg("richqin", "Hi Rich qin");
queueproduct.sendMsg("a276202460", "Hi a276202460");
topicproduct.publishMsg("csdn", "welcome to csdn");
System.out.println(queueproduct.receiveMsg("richqin"));
System.out.println(queueproduct.receiveMsg("a276202460"));
System.out.println(topicproduct.receiveMsg("csdn", "richqin"));
System.out.println(topicproduct.receiveMsg("csdn", "a276202460"));
}catch(Exception e){
e.printStackTrace();
}finally{
try {
JmsContextHelper.closeContext();
} catch (NamingException e) {
e.printStackTrace();
}
JmsStater.closeJmsAdmin();
}
}
}
其他类的结构图: