| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 619 人关注过本帖
标题:请教!!!
只看楼主 加入收藏
追求理想
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-4-22
收藏
 问题点数:0 回复次数:0 
请教!!!

请教java 高手些,帮我写一下,以下的一个测试程序吧,我们今天考试了哦。 package com.forlink.formq.client;

import com.forlink.fopub.cp.ConnectionPoolManager; import com.forlink.formq.message.Message; import com.forlink.formq.subject.Subject; import com.forlink.formq.message.InstantMessage; import java.sql.Connection;

/** * @author 王怡东 * @version 1.0 */ public class AsyncReceiver {

public AsyncReceiver() {

}

/** * 接收即时消息 * * @param subj * @param receiverID * @return com.forlink.formq.message.Message * @throws java.lang.Exception */ public static Message receiveMessage(Subject subj, String receiverID) throws Exception { //获取连接 Connection conn = ConnectionPoolManager.getInstance().getConnection(); if (conn == null) throw new Exception("建立与数据库连接出错"); try { //调用get方法得到subject的name String name = subj.getName(); //构造内部receiverID,即subjectName_receiverID String innerReceiverID = name + "_" + receiverID; //调用subject中的load方法,判断是否存在该主题 Subject subject = Subject.load(name, conn);

if (subject == null) {//不存在抛出异常 throw new Exception("该主题不存在"); } else if (!subject.checkSubscriberExistance(innerReceiverID)) {//调用subject的checkSubscriberExistance方法,判断该主题是否已存在该订阅者,存在返回异常 throw new Exception("该主题尚未被该订阅者订阅"); } else {//若存在调用静态方法InstantMessage.receiveFromJMSTopic(name) InstantMessage imsg = new InstantMessage(); imsg.setContextString("java:comp/env/jms/ConnectionFactory"); return imsg.receiveFromJMSTopic(name, -1, innerReceiverID); } } catch (Exception ex) { ex.printStackTrace(); throw new Exception("取消订阅主题出错", ex); } finally { if(conn != null) ConnectionPoolManager.getInstance().freeConnection(conn); } } /** * 接收即时消息 * * @param subj * @param receiverID * @return com.forlink.formq.message.Message * @throws java.lang.Exception */ public static Message receiveMessage(Subject subj, String receiverID, long timeout) throws Exception { //获取连接 Connection conn = ConnectionPoolManager.getInstance().getConnection(); if (conn == null) throw new Exception("建立与数据库连接出错"); try { //调用get方法得到subject的name String name = subj.getName(); //构造内部receiverID,即subjectName_receiverID String innerReceiverID = name + "_" + receiverID; //调用subject中的load方法,判断是否存在该主题 Subject subject = Subject.load(name, conn);

if (subject == null) {//不存在抛出异常 throw new Exception("该主题不存在"); } else if (!subject.checkSubscriberExistance(innerReceiverID)) {//调用subject的checkSubscriberExistance方法,判断该主题是否已存在该订阅者,存在返回异常 throw new Exception("该主题尚未被该订阅者订阅"); } else {//若存在调用InstantMessage.receiveFromJMSTopic(name) InstantMessage imsg = new InstantMessage(); imsg.setContextString("java:comp/env/jms/ConnectionFactory"); Message message = imsg.receiveFromJMSTopic(name, timeout, innerReceiverID); return message; } } catch (Exception ex) { ex.printStackTrace(); throw new Exception("取消订阅主题出错", ex); } finally { if(conn != null) ConnectionPoolManager.getInstance().freeConnection(conn); } }

/** * @param subj * @param receiverID * @return com.forlink.formq.message.Message * @throws java.lang.Exception */ public Message receiveXAMessage(Subject subj, String receiverID) throws Exception { return null; }

/** * 订阅主题 * * @param subj * @param subscriberID * @throws java.lang.Exception */ public static void subscribe(Subject subj, String subscriberID) throws Exception { //获取连接 Connection conn = ConnectionPoolManager.getInstance().getConnection(); if (conn == null) throw new Exception("建立与数据库连接出错"); try { //调用get方法得到subject的name String name = subj.getName(); String innerSubscriberID = name + "_" + subscriberID;

//调用subject中的load方法,判断是否存在该主题 Subject subject = Subject.load(name, conn);

if (subject == null) {//不存在抛出异常 throw new Exception("该主题不存在"); } else if (subject.checkSubscriberExistance(innerSubscriberID)) {//调用subject的checkSubscriberExistance方法,判断该主题是否已存在该订阅者,存在返回异常 throw new Exception("该主题已经被该订阅者订阅"); } else {//若不存在调用subject的addSubscriber方法 subject.addSubscriber(innerSubscriberID, conn); } conn.commit(); } catch (Exception ex) { conn.rollback(); ex.printStackTrace(); throw new Exception("订阅主题出错", ex); } finally { if(conn != null) ConnectionPoolManager.getInstance().freeConnection(conn); } }

/** * 取消订阅主题 * * @param subj * @param subscriberID * @throws java.lang.Exception */ public static void unSubscribe(Subject subj, String subscriberID) throws Exception { //获取连接 Connection conn = ConnectionPoolManager.getInstance().getConnection(); if (conn == null) throw new Exception("建立与数据库连接出错"); try { //调用get方法得到subject的name String name = subj.getName(); String innerSubscriberID = name + "_" + subscriberID;

//调用subject中的load方法,判断是否存在该主题 Subject subject = Subject.load(name, conn);

if (subject == null) {//不存在抛出异常 throw new Exception("该主题不存在"); } else if (!subject.checkSubscriberExistance(innerSubscriberID)) {//调用subject的checkSubscriberExistance方法,判断该主题是否已存在该订阅者,存在返回异常 throw new Exception("该主题尚未被该订阅者订阅"); } else {//若存在调用subject的delSubscriber方法 subject.delSubscriber(innerSubscriberID, conn); } conn.commit(); } catch (Exception ex) { conn.rollback(); ex.printStackTrace(); throw new Exception("取消订阅主题出错", ex); } } }

搜索更多相关主题的帖子: java 测试 package public import 
2005-04-27 11:43
快速回复:请教!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023719 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved