SyncQueueReceiver.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package bible.jms;
- import javax.jms.*;
- import javax.naming.*;
- import java.util.*;
- /**
- * Class SyncQueueReceiver
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class SyncQueueReceiver {
- /**
- * Method main
- *
- *
- * @param args
- *
- */
- public static void main(String[] args) {
- Context ctx = null;
- Hashtable ht = new Hashtable();
- QueueConnectionFactory qConnectionFactory = null;
- QueueConnection qConnection = null;
- QueueSession qSession = null;
- QueueReceiver qReceiver = null;
- Queue q = null;
- TextMessage textMsg = null;
- String msg;
- try {
- // Obtain references to JMS queue components.
- ht.put(Context.INITIAL_CONTEXT_FACTORY,
- "weblogic.jndi.WLInitialContextFactory");
- ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
- ctx = new InitialContext(ht);
- qConnectionFactory =
- (QueueConnectionFactory) ctx.lookup("BibleJMSFactory");
- qConnection = qConnectionFactory.createQueueConnection();
- qSession = qConnection.createQueueSession(false,
- javax.jms.QueueSession.AUTO_ACKNOWLEDGE);
- q = (Queue) ctx.lookup("BibleJMSQueue");
- qReceiver = qSession.createReceiver(q);
- // Start a connection to the queue, retrieve all
- // messages in the queue, then stop the connection.
- System.out.println("nReceiving messages...");
- qConnection.start();
- textMsg = (TextMessage) qReceiver.receiveNoWait();
- while (textMsg != null) {
- System.out.println(" Receiving message: " + textMsg.getText());
- textMsg = (TextMessage) qReceiver.receiveNoWait();
- }
- qConnection.stop();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- // Release JMS resources.
- qReceiver.close();
- qSession.close();
- qConnection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/