001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007:
008: /* JFox, the OpenSource J2EE Application Server
009: *
010: * Distributable under GNU LGPL license by gun.org
011: * more details please visit http://www.huihoo.org/jfox
012: */
013:
014: package org.jfox.jms;
015:
016: import java.util.HashMap;
017: import java.util.Map;
018: import java.util.UUID;
019: import javax.jms.Connection;
020: import javax.jms.JMSException;
021: import javax.jms.Message;
022: import javax.jms.MessageListener;
023: import javax.jms.Queue;
024: import javax.jms.QueueConnection;
025: import javax.jms.QueueReceiver;
026: import javax.jms.QueueSender;
027: import javax.jms.QueueSession;
028: import javax.jms.Session;
029: import javax.jms.TopicConnection;
030: import javax.jms.XAConnection;
031: import javax.jms.XAQueueConnection;
032: import javax.jms.XATopicConnection;
033:
034: import org.jfox.jms.destination.JMSDestination;
035: import org.jfox.jms.destination.JMSQueue;
036: import org.jfox.jms.destination.JMSTopic;
037: import org.jfox.jms.message.JMSMessage;
038:
039: /**
040: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
041: */
042:
043: public class JMSConnectionFactory implements MessageService {
044:
045: private Map<String, JMSDestination> destinationMap = new HashMap<String, JMSDestination>();
046:
047: public Connection createConnection() throws JMSException {
048: return createConnection(null, null);
049: }
050:
051: public Connection createConnection(String userName, String password)
052: throws JMSException {
053: return createConnection(userName, password, false);
054: }
055:
056: public QueueConnection createQueueConnection() throws JMSException {
057: return (QueueConnection) createConnection();
058: }
059:
060: public QueueConnection createQueueConnection(String userName,
061: String password) throws JMSException {
062: return (QueueConnection) createConnection(userName, password);
063: }
064:
065: public TopicConnection createTopicConnection() throws JMSException {
066: return (TopicConnection) createConnection();
067: }
068:
069: public TopicConnection createTopicConnection(String userName,
070: String password) throws JMSException {
071: return (TopicConnection) createConnection(userName, password);
072: }
073:
074: public XAConnection createXAConnection() throws JMSException {
075: return createXAConnection(null, null);
076: }
077:
078: public XAConnection createXAConnection(String userName,
079: String password) throws JMSException {
080: return createConnection(userName, password, true);
081: }
082:
083: public XAQueueConnection createXAQueueConnection()
084: throws JMSException {
085: return (XAQueueConnection) createXAConnection();
086: }
087:
088: public XAQueueConnection createXAQueueConnection(String userName,
089: String password) throws JMSException {
090: return (XAQueueConnection) createXAConnection(userName,
091: password);
092: }
093:
094: public XATopicConnection createXATopicConnection()
095: throws JMSException {
096: return (XATopicConnection) createXAConnection();
097: }
098:
099: public XATopicConnection createXATopicConnection(String userName,
100: String password) throws JMSException {
101: return (XATopicConnection) createXAConnection(userName,
102: password);
103: }
104:
105: public void close() {
106: for (JMSDestination destination : destinationMap.values()) {
107: destination.stop();
108: }
109: }
110:
111: /**
112: * create queue with given name
113: *
114: * MDBBucket 通过该方法创建Queue
115: *
116: * @param name queue name
117: */
118: public JMSQueue createQueue(String name) throws JMSException {
119: if (!destinationMap.containsKey(name)) {
120: JMSQueue queue = new JMSQueue(name);
121: destinationMap.put(name, queue);
122: return queue;
123: } else {
124: JMSDestination destination = destinationMap.get(name);
125: if (destination.isTopic()) {
126: throw new JMSException("Destination " + name
127: + " exists, but its type is Topic.");
128: }
129: return (JMSQueue) destination;
130: }
131: }
132:
133: public JMSTopic createTopic(String name) throws JMSException {
134: if (!destinationMap.containsKey(name)) {
135: JMSTopic queue = new JMSTopic(name);
136: destinationMap.put(name, queue);
137: return queue;
138: } else {
139: JMSDestination destination = destinationMap.get(name);
140: if (!destination.isTopic()) {
141: throw new JMSException("Destination " + name
142: + " exists, but its type is Queue.");
143: }
144: return (JMSTopic) destination;
145: }
146: }
147:
148: protected synchronized JMSConnection createConnection(
149: String userName, String password, boolean isXA) {
150: //TODO: support username & password
151: return new JMSConnection(UUID.randomUUID().toString(), this ,
152: true);
153: }
154:
155: public void sendMessage(JMSDestination destination,
156: JMSMessage message) {
157:
158: }
159:
160: public static void main(String[] args) throws Exception {
161: JMSConnectionFactory connectionFactory = new JMSConnectionFactory();
162: QueueConnection connection = connectionFactory
163: .createQueueConnection();
164: QueueSession session = connection.createQueueSession(false,
165: Session.AUTO_ACKNOWLEDGE);
166: Queue queue = session.createQueue("defaultQ");
167: QueueSender sender = session.createSender(queue);
168: sender.send(session.createTextMessage("Hello, JMS!"));
169:
170: connection.start();
171: QueueReceiver receiver = session.createReceiver(queue);
172: Message message = receiver.receive();
173: System.out.println("Received Message: " + message);
174:
175: Thread.sleep(1000);
176: receiver.setMessageListener(new MessageListener() {
177: public void onMessage(Message message) {
178: System.out.println("onMessage: " + message);
179: }
180: });
181: sender.send(session.createTextMessage("Hello, JMS2!"));
182:
183: Thread.sleep(1000);
184: connectionFactory.close();
185: }
186: }
|