AsyncQueueReceiver.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- package bible.jms;
- import javax.jms.*;
- import javax.naming.*;
- import java.util.*;
- /**
- * Class AsyncQueueReceiver
- *
- *
- * @author
- * @version %I%, %G%
- */
- public class AsyncQueueReceiver implements MessageListener {
- // Instance variables.
- private boolean finished = false;
- private Context ctx = null;
- private Hashtable ht = null;
- private QueueConnectionFactory qConnectionFactory = null;
- private QueueConnection qConnection = null;
- private QueueSession qSession = null;
- private QueueReceiver qReceiver = null;
- private Queue q = null;
- /**
- * Method init
- *
- *
- */
- public void init() {
- try {
- // Obtain references to JMS queue components.
- ht = new Hashtable();
- 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);
- qReceiver.setMessageListener(this);
- // Must start the connection in order to listen.
- qConnection.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Method close
- *
- *
- * @throws JMSException
- *
- */
- public void close() throws JMSException {
- try {
- // Release JMS resources.
- qReceiver.close();
- qSession.close();
- qConnection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Method onMessage
- *
- *
- * @param message
- *
- */
- public void onMessage(Message message) {
- if (message instanceof BytesMessage) {
- BytesMessage bytesMessage = (BytesMessage) message;
- // Process bytesMessage here
- } else {
- if (message instanceof MapMessage) {
- MapMessage mapMessage = (MapMessage) message;
- // Process mapMessage here
- } else {
- if (message instanceof ObjectMessage) {
- ObjectMessage objectMessage = (ObjectMessage) message;
- // Process objectMessage here
- } else {
- if (message instanceof StreamMessage) {
- StreamMessage streamMessage = (StreamMessage) message;
- // Process streamMessage here
- } else {
- if (message instanceof TextMessage) {
- try {
- TextMessage textMessage = (TextMessage) message;
- // Process textMessage here
- String msg = textMessage.getText();
- System.out.println("Received message: " + msg);
- if (msg.equals("Stop")) {
- synchronized (this) {
- finished = true;
- this.notifyAll(); // Notify main thread to quit
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- }
- /**
- * Method main
- *
- *
- * @param args
- *
- */
- public static void main(String[] args) {
- try {
- AsyncQueueReceiver qr = new AsyncQueueReceiver();
- qr.init();
- System.out.println("Ready to receive messages....");
- // Receive text messages until one is equal to "Stop"
- synchronized (qr) {
- while (!qr.finished) {
- try {
- qr.wait();
- } catch (InterruptedException ie) {}
- }
- }
- qr.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 ------*/