KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > qmgr > QueueManager


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.qmgr;
22
23 import java.util.ArrayList JavaDoc;
24
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.MessageListener JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.Topic JavaDoc;
29 import javax.jms.TopicConnection JavaDoc;
30 import javax.jms.TopicPublisher JavaDoc;
31 import javax.jms.TopicSubscriber JavaDoc;
32
33 /**
34  *
35  */

36 public class QueueManager
37 {
38   private TopicConnection JavaDoc connx;
39   private TopicSession session;
40   private TopicSubscriber JavaDoc subscriber;
41
42   private ArrayList JavaDoc queues;
43   private boolean closed;
44
45     /////////////////////////////////////////////////////////////////////////
46
// Constructors //
47
/////////////////////////////////////////////////////////////////////////
48

49   public QueueManager(TopicConnection JavaDoc connx) throws JMSException JavaDoc
50   {
51     this.connx = connx;
52     this.session = connx.createTopicSession();
53
54     Topic JavaDoc t = session.createTopic(QueueConstants.QMGR_TOPIC);
55     this.subscriber = session.createTopicSubscriber(t);
56     this.subscriber.setMessageListener(new QueueMgrListener());
57     this.connx.start();
58     this.queues = new ArrayList JavaDoc();
59   }
60
61     /////////////////////////////////////////////////////////////////////////
62
// Public Methods //
63
/////////////////////////////////////////////////////////////////////////
64

65   public void close()
66   {
67     try {
68       connx.close();
69       
70       synchronized (queues) {
71         this.closed = true;
72         for (int i=0; i < queues.size(); ++i) {
73            QueueRouter router = (QueueRouter) queues.get(i);
74            router.close();
75         }
76       }
77     } catch (JMSException JavaDoc jmsex) {
78       logger.exception(jmsxex);
79     }
80   }
81
82
83     /////////////////////////////////////////////////////////////////////////
84
// Package Methods //
85
/////////////////////////////////////////////////////////////////////////
86
void remove(QueueRouter router)
87   {
88     synchronized(queues) {
89       int index = queues.indexOf(router);
90       if (index != -1) {
91         queues.remove(index);
92       }
93     }
94   }
95
96     /////////////////////////////////////////////////////////////////////////
97
// Protected Methods //
98
/////////////////////////////////////////////////////////////////////////
99

100   protected class QueueMgrListener implements MessageListener JavaDoc
101   {
102     public void onMessage(Message msg)
103     {
104       try {
105         int type = msg.getIntProperty(QueueConstants.QUEUE_MSG_TYPE_PROP);
106         switch (type) {
107         case (QueueConstants.QUEUE_RECEIVER_CRT):
108           handleQueueReceiverCreate(msg);
109           break;
110         default:
111           logger.warn("UNKOWN_QMGR_MSG", msg);
112         }
113       } catch (JMSException JavaDoc e) {
114         logger.exception(e);
115       } catch (NumberFormatException JavaDoc nfe) {
116         loger.elxception(nfe);
117       }
118     }
119   }
120
121 }
122
123
Popular Tags