KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > util > BridgePubSubModule


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - Bull SA
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Frederic Maistre (Bull SA)
21  * Contributor(s):
22  */

23 package org.objectweb.joram.mom.util;
24
25 import javax.jms.*;
26 import javax.jms.IllegalStateException JavaDoc;
27
28 import java.util.Vector JavaDoc;
29
30
31 /**
32  * The <code>BridgePubSubModule</code> class is a bridge module based on the
33  * JMS Publish/Subscribe semantics and classes.
34  */

35 public class BridgePubSubModule extends BridgeUnifiedModule
36 {
37   /** Specific Pub/Sub producer resource. */
38   private transient TopicPublisher publisher;
39
40
41   /** Constructs a <code>BridgePubSubModule</code> module. */
42   public BridgePubSubModule()
43   {
44     super();
45   }
46
47  
48   /**
49    * Sends a message to the foreign JMS topic.
50    *
51    * @exception javax.jms.IllegalStateException If the module's state does
52    * not permit message sendings.
53    * @exception javax.jms.MessageFormatException If the MOM message could not
54    * be converted into a foreign JMS message.
55    */

56   public void send(org.objectweb.joram.shared.messages.Message message)
57               throws JMSException
58   {
59     if (! usable)
60       throw new IllegalStateException JavaDoc(notUsableMessage);
61
62     try {
63       publisher.publish(MessageConverterModule.convert(producerSession,
64                                                        message));
65       acknowledge(message);
66     }
67     catch (javax.jms.MessageFormatException JavaDoc exc) {
68       throw exc;
69     }
70     // Connection failure? Keeping the message for later delivery.
71
catch (javax.jms.JMSException JavaDoc exc) {
72       qout.add(message);
73     }
74   }
75
76   /**
77    * Opens a connection with the foreign JMS server and creates the
78    * JMS Pub/Sub resources for interacting with the foreign JMS topic.
79    *
80    * @exception JMSException If the needed JMS resources could not be created.
81    */

82   protected void doConnect() throws JMSException
83   {
84     TopicConnectionFactory topicCnxFact = (TopicConnectionFactory) cnxFact;
85     Topic topic = (Topic) dest;
86
87     if (userName != null && password != null)
88       cnx = topicCnxFact.createTopicConnection(userName, password);
89     else
90       cnx = topicCnxFact.createTopicConnection();
91     cnx.setExceptionListener(this);
92
93     if (clientID != null)
94       cnx.setClientID(clientID);
95
96     producerSession =
97       ((TopicConnection) cnx).createTopicSession(false,
98                                                  Session.AUTO_ACKNOWLEDGE);
99     publisher = ((TopicSession) producerSession).createPublisher(topic);
100
101     consumerSession = ((TopicConnection) cnx).createTopicSession(true, 0);
102     consumer = consumerSession.createDurableSubscriber((Topic) dest,
103                                                        agentId.toString(),
104                                                        selector,
105                                                        false);
106   }
107 }
108
Popular Tags