001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JMSEventConnection.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.proxy.jms;
030:
031: import com.sun.jbi.binding.proxy.LocalStringKeys;
032: import com.sun.jbi.binding.proxy.RegistrationInfo;
033:
034: import com.sun.jbi.binding.proxy.util.Translator;
035:
036: import com.sun.jbi.binding.proxy.connection.EventInfo;
037: import com.sun.jbi.binding.proxy.connection.EventInfoFactory;
038:
039: import com.sun.jbi.binding.proxy.connection.EventConnection;
040: import com.sun.jbi.binding.proxy.connection.Event;
041:
042: import javax.jms.Connection;
043: import javax.jms.MessageConsumer;
044: import javax.jms.Message;
045: import javax.jms.MessageProducer;
046: import javax.jms.Queue;
047: import javax.jms.Topic;
048: import javax.jms.Session;
049: import javax.jms.StreamMessage;
050:
051: import java.util.logging.Logger;
052:
053: import javax.xml.namespace.QName;
054:
055: /**
056: * Encapsulates the logic of a Event connection. The event connection is assumed to be
057: * called from a single thread using pull-style processing. The producer and consumer are
058: * attached to the same session which allows messages sent by this producer to not be seen
059: * by this consumer.
060: *
061: * @author Sun Microsystems, Inc
062: */
063: public class JMSEventConnection implements EventConnection {
064: private Logger mLog;
065: private Session mSession;
066: private Topic mTopic;
067: private MessageProducer mProducer;
068: private MessageConsumer mConsumer;
069:
070: JMSEventConnection(Connection connection, Topic topic)
071: throws com.sun.jbi.binding.proxy.connection.ConnectionException {
072: mLog = Logger.getLogger("com.sun.jbi.binding.proxy.jms");
073: mTopic = topic;
074:
075: try {
076: mSession = connection.createSession(false,
077: Session.AUTO_ACKNOWLEDGE);
078:
079: /* Publisher used to send registration info. */
080: mProducer = mSession.createProducer(mTopic);
081:
082: /* Subscriber used to receive registration info (true = ignore local messages) */
083: mConsumer = mSession.createConsumer(mTopic, null, true);
084: } catch (javax.jms.JMSException jEx) {
085: throw new com.sun.jbi.binding.proxy.connection.ConnectionException(
086: jEx);
087: }
088: }
089:
090: public void sendEvent(EventInfo info)
091: throws com.sun.jbi.binding.proxy.connection.EventException {
092: JMSEvent e;
093:
094: try {
095: e = new JMSEvent(mSession.createStreamMessage(), info
096: .getEventName());
097: info.encodeEvent(e);
098: mProducer.send(e.getMessage());
099: } catch (javax.jms.JMSException jmsEx) {
100: throw new com.sun.jbi.binding.proxy.connection.EventException(
101: jmsEx);
102: }
103: }
104:
105: public EventInfo receiveEvent(long timeout)
106: throws com.sun.jbi.binding.proxy.connection.EventException {
107: EventInfo ei = null;
108:
109: try {
110: JMSEvent e;
111: StreamMessage m;
112: String t;
113:
114: m = (StreamMessage) mConsumer.receive(timeout);
115: if (m != null) {
116: e = new JMSEvent(m);
117: ei = EventInfoFactory.getInstance().newInstance(e);
118: if (ei == null) {
119: mLog.info("Received unknown event type ("
120: + e.getEventName() + ")");
121: }
122: }
123: return (ei);
124: } catch (javax.jms.JMSException jEx) {
125: throw new com.sun.jbi.binding.proxy.connection.EventException(
126: jEx);
127: }
128: }
129:
130: }
|