KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > JMSConnection


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms;
8
9 import java.rmi.RemoteException JavaDoc;
10 import java.rmi.server.UnicastRemoteObject JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import javax.jms.Connection JavaDoc;
16 import javax.jms.ConnectionConsumer JavaDoc;
17 import javax.jms.ConnectionMetaData JavaDoc;
18 import javax.jms.Destination JavaDoc;
19 import javax.jms.ExceptionListener JavaDoc;
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.Queue JavaDoc;
22 import javax.jms.QueueConnection JavaDoc;
23 import javax.jms.QueueSession JavaDoc;
24 import javax.jms.ServerSessionPool JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.Topic JavaDoc;
27 import javax.jms.TopicConnection JavaDoc;
28 import javax.jms.TopicSession JavaDoc;
29 import javax.jms.XAConnection JavaDoc;
30 import javax.jms.XAQueueConnection JavaDoc;
31 import javax.jms.XAQueueSession JavaDoc;
32 import javax.jms.XASession JavaDoc;
33 import javax.jms.XATopicConnection JavaDoc;
34 import javax.jms.XATopicSession JavaDoc;
35
36 import org.jfox.jms.connector.JMSContainer;
37 import org.jfox.jms.message.JMSMessage;
38
39 /**
40  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
41  */

42
43 public class JMSConnection implements Connection JavaDoc,
44                                       QueueConnection JavaDoc,
45                                       TopicConnection JavaDoc,
46                                       XAConnection JavaDoc,
47                                       XAQueueConnection JavaDoc,
48                                       XATopicConnection JavaDoc,
49         JMSConnectionRemote {
50
51     protected JMSContainer container = null;
52     protected boolean started = false;
53     protected boolean closed = false;
54
55     protected String JavaDoc clientId = null;
56
57     protected boolean isXA = false;
58
59     /*
60      * sessions created by this connection
61      * sessionId => session
62      */

63     protected transient Map JavaDoc<String JavaDoc, JMSSession> sessions = new HashMap JavaDoc<String JavaDoc, JMSSession>();
64
65     public JMSConnection(String JavaDoc clientId, JMSContainer container, boolean isXA) {
66         this.clientId = clientId;
67         this.container = container;
68         this.isXA = isXA;
69     }
70
71     /**
72      * If a <CODE>Session</CODE> is transacted, the acknowledgement mode
73      * is ignored.
74      *
75      * @param transacted
76      * @param acknowledgeMode
77      * @return
78      * @throws JMSException
79      */

80     public synchronized Session JavaDoc createSession(boolean transacted, int acknowledgeMode) throws JMSException JavaDoc {
81         checkClosed();
82         if (transacted) {
83             acknowledgeMode = Session.SESSION_TRANSACTED;
84         }
85         JMSSession session = new JMSSession(this, transacted, acknowledgeMode, false);
86         container.registerSession(clientId, session.getSessionId());
87         synchronized (sessions) {
88             sessions.put(session.getSessionId(), session);
89         }
90         return session;
91     }
92
93     public String JavaDoc getClientID() throws JMSException JavaDoc {
94         return clientId;
95     }
96
97     public void setClientID(String JavaDoc clientId) throws JMSException JavaDoc {
98         checkClosed();
99         if (started) {
100             throw new IllegalStateException JavaDoc("connection has already started, can not set client identifier.");
101         }
102         throw new IllegalStateException JavaDoc("client identifier has already been setted by system.");
103     }
104
105     public ConnectionMetaData JavaDoc getMetaData() throws JMSException JavaDoc {
106         throw new JMSException JavaDoc("not support now!");
107     }
108
109     public ExceptionListener JavaDoc getExceptionListener() throws JMSException JavaDoc {
110         throw new JMSException JavaDoc("not support now!");
111     }
112
113     public void setExceptionListener(ExceptionListener JavaDoc listener) throws JMSException JavaDoc {
114         throw new JMSException JavaDoc("not support now!");
115     }
116
117     /**
118      * start to receive messages
119      *
120      * @throws JMSException
121      */

122     public synchronized void start() throws JMSException JavaDoc {
123         if (!started) {
124             container.startConnection(getClientID());
125             this.started = true;
126         }
127     }
128
129     public synchronized void stop() throws JMSException JavaDoc {
130         if (started) {
131             container.stopConnection(getClientID());
132             started = false;
133         }
134     }
135
136     public synchronized void close() throws JMSException JavaDoc {
137         if (closed) return;
138         this.stop();
139         closed = true;
140
141         List JavaDoc<JMSSession> list = new ArrayList JavaDoc<JMSSession>(sessions.values());
142         for (JMSSession session : list) {
143             session.close();
144         }
145
146         try {
147             container.unregisterConnection(this.getClientID());
148             UnicastRemoteObject.unexportObject(this, false);
149         } catch (Exception JavaDoc e) {
150             e.printStackTrace();
151         }
152
153     }
154
155     public ConnectionConsumer JavaDoc createConnectionConsumer(Destination JavaDoc destination,
156                                                        String JavaDoc messageSelector,
157                                                        ServerSessionPool JavaDoc sessionPool,
158                                                        int maxMessages) throws JMSException JavaDoc {
159         checkClosed();
160         throw new JMSException JavaDoc("not support now!");
161     }
162
163     public ConnectionConsumer JavaDoc createDurableConnectionConsumer(Topic JavaDoc topic,
164                                                               String JavaDoc subscriptionName,
165                                                               String JavaDoc messageSelector,
166                                                               ServerSessionPool JavaDoc sessionPool,
167                                                               int maxMessages) throws JMSException JavaDoc {
168         checkClosed();
169         throw new JMSException JavaDoc("not support now!");
170     }
171
172     public QueueSession JavaDoc createQueueSession(boolean transacted, int acknowledgeMode) throws JMSException JavaDoc {
173         return (QueueSession JavaDoc) createSession(transacted, acknowledgeMode);
174     }
175
176     public ConnectionConsumer JavaDoc createConnectionConsumer(Queue JavaDoc queue,
177                                                        String JavaDoc messageSelector,
178                                                        ServerSessionPool JavaDoc sessionPool,
179                                                        int maxMessages) throws JMSException JavaDoc {
180         throw new JMSException JavaDoc("not support now!");
181     }
182
183     public TopicSession JavaDoc createTopicSession(boolean transacted, int acknowledgeMode) throws JMSException JavaDoc {
184         return (TopicSession JavaDoc) createSession(transacted, acknowledgeMode);
185     }
186
187     public ConnectionConsumer JavaDoc createConnectionConsumer(Topic JavaDoc topic,
188                                                        String JavaDoc messageSelector,
189                                                        ServerSessionPool JavaDoc sessionPool,
190                                                        int maxMessages) throws JMSException JavaDoc {
191         throw new JMSException JavaDoc("not support now!");
192     }
193
194     public XASession JavaDoc createXASession() throws JMSException JavaDoc {
195         checkClosed();
196         if (!isXA) {
197             throw new JMSException JavaDoc("current connection " + this + " is not an xa connection");
198         }
199
200         JMSSession session = new JMSSession(this, true, Session.SESSION_TRANSACTED, true);
201         synchronized (sessions) {
202             sessions.put(session.getSessionId(), session);
203         }
204         return session;
205     }
206
207     public XAQueueSession JavaDoc createXAQueueSession() throws JMSException JavaDoc {
208         return (XAQueueSession JavaDoc) createXASession();
209     }
210
211     public XATopicSession JavaDoc createXATopicSession() throws JMSException JavaDoc {
212         return (XATopicSession JavaDoc) createXASession();
213     }
214
215     public void onMessage(String JavaDoc sessionId, String JavaDoc consumerId, JMSMessage msg) throws RemoteException JavaDoc, JMSException JavaDoc {
216         JMSSession session = sessions.get(sessionId);
217         session.onMessage(consumerId, msg);
218     }
219
220     JMSContainer getContainer() {
221         return container;
222     }
223
224     protected void checkClosed() throws javax.jms.IllegalStateException JavaDoc {
225         if (closed) {
226             throw new javax.jms.IllegalStateException JavaDoc("connection closed");
227         }
228     }
229
230     boolean isStarted() {
231         return started;
232     }
233
234     void closeSession(String JavaDoc sessionId) throws JMSException JavaDoc {
235         sessions.remove(sessionId);
236         container.closeSession(clientId, sessionId);
237     }
238
239     public static void main(String JavaDoc[] args) {
240
241     }
242 }
243
Popular Tags