?? queuebrowse.java
字號:
//聲明這個類定義在包examples.jms.queue中
package examples.jms.queue;
//聲明這個類引入的其它類和包
import java.io.*;
import java.util.*;
import java.util.Date;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
/**
* 這個實例演示怎樣建立一個JMS隊列連接并瀏覽隊列消息。
這個目錄中的例子操作同一個JMS隊列。一起運行這些類觀察消息發送和接收。
* 瀏覽隊列中的消息。
*/
public class QueueBrowse
{
/**
* 定義JNDI上下文構造器
*/
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
/**
* 定義JMS上下文構造器
*/
public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory";
/**
* 定義消息隊列
*/
public final static String QUEUE="weblogic.examples.jms.exampleQueue";
//聲明隊列連接構造器
private QueueConnectionFactory qconFactory;
//聲明隊列連接
private QueueConnection qcon;
//聲明隊列會話
private QueueSession qsession;
//聲明隊列瀏覽器
private QueueBrowser qbrowser;
//聲明隊列
private Queue queue;
/**
* 創建所有發送隊列消息所需的對象
*
* @參數 ctx JNDI上下文
* @參數 queueName 隊列名
* @異常 NamingException 如果操作不能執行拋出的異常
* @異常 JMSException 由于內部錯誤,JMS 發送消息失敗拋出的異常
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
//查找連接構造器
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
//創建隊列連接
qcon = qconFactory.createQueueConnection();
//創建隊列會話
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//查找消息隊列
queue = (Queue) ctx.lookup(queueName);
//創建消息瀏覽器
qbrowser = qsession.createBrowser(queue);
//啟動連接
qcon.start();
}
/**
* 顯示隊列的所有當前內容
*
* @異常 JMSException 由于內部錯誤,JMS 不能顯示消息隊列中的消息拋出的異常
*/
public void displayQueue()
throws JMSException
{
//獲得瀏覽器屬性枚舉
Enumeration e = qbrowser.getEnumeration();
Message m = null;
if (! e.hasMoreElements()) {
System.out.println("There are no messages on this queue.");
} else {
System.out.println("Queued JMS Messages: ");
while (e.hasMoreElements()) {
m = (Message) e.nextElement();
//打印消息的所有屬性
System.out.println("Message ID " + m.getJMSMessageID() +
" delivered " + new Date(m.getJMSTimestamp()) +
" to " + m.getJMSDestination());
System.out.print("\tExpires ");
if (m.getJMSExpiration() > 0) {
System.out.println( new Date( m.getJMSExpiration()));
}
else
System.out.println("never");
System.out.println("\tPriority " + m.getJMSPriority());
System.out.println("\tMode " + (
m.getJMSDeliveryMode() == DeliveryMode.PERSISTENT ?
"PERSISTENT" : "NON_PERSISTENT"));
System.out.println("\tCorrelation ID " + m.getJMSCorrelationID());
System.out.println("\tReply to " + m.getJMSReplyTo());
System.out.println("\tMessage type " + m.getJMSType());
if (m instanceof TextMessage) {
System.out.println("\tTextMessage \"" + ((TextMessage)m).getText() + "\"");
}
}
}
}
/**
* 關閉JMS對象
*
* @異常 JMSException 由于內部錯誤,JMS不能關閉對象拋出的異常
*/
public void close()
throws JMSException
{
//關閉對象
qbrowser.close();
qsession.close();
qcon.close();
}
/**
* main() 方法
*
* @參數 args WebLogic服務器URL
* @異常 Exception 如果執行失敗
*/
public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueBrowse WebLogicURL");
return;
}
//初始化
InitialContext ic = getInitialContext(args[0]);
//本類實例
QueueBrowse qb = new QueueBrowse();
//調用本類初始化方法
qb.init(ic, QUEUE);
//顯示隊列
qb.displayQueue();
//關閉
qb.close();
}
//初始化上下文
private static InitialContext getInitialContext(String url)
throws NamingException
{
//屬性對象
Hashtable env = new Hashtable();
//設置屬性
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -