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

请教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); } } }

搜索更多相关主题的帖子: 代码 解答 
2005-04-30 10:03
疯子java
Rank: 4
等 级:贵宾
威 望:13
帖 子:328
专家分:0
注 册:2004-9-19
收藏
得分:0 
测试类我不会写
望其他高手解答,期待ing

疯言疯语 疯部可及 ---世界法第1章第1节第1条规定 此人由于说话太过于疯!!!其余人等切不可与其交谈! 此人由于说话太过于疯!!!此人的话100%的不能相信
2005-04-30 10:32
断线的风筝
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2005-6-4
收藏
得分:0 
你这不是给我们出考题吗
2005-06-05 10:35
快速回复:[求助]请教代码分析与解答
数据加载中...
 
   



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

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