OPENJMS学习笔记二

2 篇文章 0 订阅

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();
	  }
  }
}

其他类的结构图:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值