?? queuereceive.java
字號:
//聲明這個類定義在包examples.jms.queue中
package examples.jms.queue;
//聲明這個類引入的其它類和包
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
/** 這個實例演示怎樣建立一個連接并到一個JMS隊列接收消息。
* 這個目錄中的例子操作同一個JMS隊列。一起運行這些類觀察消息發送和接收。
* 瀏覽隊列中的消息。
*/
public class QueueReceive
implements MessageListener
{
/**
* 定義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 QueueReceiver qreceiver;
//聲明隊列
private Queue queue;
private boolean quit = false;
/**
* 消息監聽接口
* @參數 msg 消息
*/
public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
//獲取文本消息的文本
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}
System.out.println("Message Received: "+ msgText );
if (msgText.equalsIgnoreCase("quit")) {
//退出運行消息
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/**
* 創建所有從JMS隊列接收消息必須的對象
*
* @參數 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);
//創建消息接收
qreceiver = qsession.createReceiver(queue);
//設置消息監聽器
qreceiver.setMessageListener(this);
//啟動連接
qcon.start();
}
/**
* 關閉JMS對象
* @異常 JMSException 由于內部錯誤,JMS不能關閉對象拋出的異常
*/
public void close()
throws JMSException
{
//關閉所有對象
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() 方法
*
* @參數s args WebLogic服務器URL
* @異常 Exception 如果執行錯誤
*/
public static void main(String[] args)
throws Exception
{
if (args.length != 1) {
//打印用法提示
System.out.println("用法: java examples.jms.queue.QueueReceive WebLogicURL");
return;
}
//上下文初始化方法
InitialContext ic = getInitialContext(args[0]);
//本類實例
QueueReceive qr = new QueueReceive();
//調用初始化方法
qr.init(ic, QUEUE);
System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
// 等待,直到接收到"quit"消息
synchronized(qr) {
while (! qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {}
}
}
qr.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 + -