请教!!!
请教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); } } } |
[求助]请教代码分析与解答